This commit is contained in:
Sebastian Seedorf
2020-12-29 17:26:41 +01:00
parent 259eb24308
commit 9e79644296
3 changed files with 111 additions and 0 deletions

53
day22/input.txt Normal file
View File

@@ -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

23
day22/part1.py Normal file
View File

@@ -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)))

35
day22/part2.py Normal file
View File

@@ -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])