From 9e7964429645005931e254331b7b79998c7b2a71 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Tue, 29 Dec 2020 17:26:41 +0100 Subject: [PATCH] Day 22 --- day22/input.txt | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ day22/part1.py | 23 +++++++++++++++++++++ day22/part2.py | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 day22/input.txt create mode 100644 day22/part1.py create mode 100644 day22/part2.py diff --git a/day22/input.txt b/day22/input.txt new file mode 100644 index 0000000..4012b3a --- /dev/null +++ b/day22/input.txt @@ -0,0 +1,53 @@ +Player 1: +44 +47 +29 +31 +10 +40 +50 +27 +35 +30 +38 +11 +14 +9 +42 +1 +26 +24 +6 +13 +8 +15 +21 +18 +4 + +Player 2: +17 +22 +28 +34 +32 +23 +3 +19 +36 +12 +45 +37 +46 +39 +49 +43 +25 +33 +2 +41 +48 +7 +5 +16 +20 diff --git a/day22/part1.py b/day22/part1.py new file mode 100644 index 0000000..5dffcb9 --- /dev/null +++ b/day22/part1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +lines = (x.strip() for x in open("input.txt")) + +stats = [[], []] +player2 = False +for line in lines: + if line.startswith("Player"): + continue + elif line == "": + player2 = True + else: + stats[1 if player2 else 0].append(int(line)) + + +while all(len(x) > 0 for x in stats): + a, b = stats[0].pop(0), stats[1].pop(0) + if a > b: + stats[0].extend((a, b)) + else: + stats[1].extend((b, a)) + +print(list(map(lambda x: sum(i*v for i, v in zip(x, range(len(x), 0, -1))), stats))) diff --git a/day22/part2.py b/day22/part2.py new file mode 100644 index 0000000..08d486e --- /dev/null +++ b/day22/part2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +lines = (x.strip() for x in open("input.txt")) + +init_stats = [[], []] +player2 = False +for line in lines: + if line.startswith("Player"): + continue + elif line == "": + player2 = True + else: + init_stats[1 if player2 else 0].append(int(line)) + +def play_game(stats): + done = set() + while all(len(x) > 0 for x in stats): + state = (*stats[0], "+", *stats[1]) + if state in done: + return True, state + done.add(state) + a, b = stats[0].pop(0), stats[1].pop(0) + if len(stats[0]) >= a and len(stats[1]) >= b: + winner = play_game([stats[0][:a], stats[1][:b]])[0] + else: + winner = a > b + if winner: + stats[0].extend((a, b)) + else: + stats[1].extend((b, a)) + return len(stats[0]) > 0, stats + +winner, out_stats = play_game(init_stats) + +print(list(map(lambda x: sum(i*v for i, v in zip(x, range(len(x), 0, -1))), out_stats))[0 if winner else 1])