From 9ba38eac9e3540e7c430c16b21729672f0badb15 Mon Sep 17 00:00:00 2001 From: trwnh Date: Wed, 23 Dec 2020 09:07:12 -0600 Subject: [PATCH] day 23 --- 2020/23/23.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2020/23/input.txt | 1 + 2 files changed, 46 insertions(+) create mode 100644 2020/23/23.py create mode 100644 2020/23/input.txt diff --git a/2020/23/23.py b/2020/23/23.py new file mode 100644 index 0000000..64cdec6 --- /dev/null +++ b/2020/23/23.py @@ -0,0 +1,45 @@ +def read_file(): + with open("input.txt","r") as f: + return list(map(int,list(f.read()))) + +def play(cups, rounds): + current = cups[0] + next_cup = {c:n for c, n in zip(cups, cups[1:] + [cups[0]])} + for _ in range(rounds): + # pickup next 3 cups + c1 = next_cup[current] + c2 = next_cup[c1] + c3 = next_cup[c2] + # select a destination cup (-1 until found, wraps around) + destination = current - 1 if current > 1 else len(cups) + while destination in [c1, c2, c3]: + destination = destination - 1 if destination > 1 else len(cups) + # add cups back + next_cup[current] = next_cup[c3] + next_cup[c3] = next_cup[destination] + next_cup[destination] = c1 + # select a new current cup (next) + current = next_cup[current] + # rebuild cups from next_cup + cup = 1 + new_cups = [] + for _ in range(len(cups)): + cup = next_cup[cup] + new_cups.append(cup) + return new_cups + +def part1(cups): + cups = play(cups, 100) + return "".join(str(c) for c in cups[:-1]) + +def part2(cups): + cups += list(range(10,1_000_001)) + cups = play(cups,10_000_000) + return cups[0] * cups[1] + +def main(): + cups = read_file() + print(part1(cups), part2(cups)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/2020/23/input.txt b/2020/23/input.txt new file mode 100644 index 0000000..c881536 --- /dev/null +++ b/2020/23/input.txt @@ -0,0 +1 @@ +653427918 \ No newline at end of file