Skip to content

Commit b8baf95

Browse files
committedDec 22, 2020
2020 Day 22
1 parent e0c70d9 commit b8baf95

File tree

6 files changed

+205
-3
lines changed

6 files changed

+205
-3
lines changed
 

‎2020/22.txt

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Player 1:
2+
42
3+
29
4+
12
5+
40
6+
47
7+
26
8+
11
9+
39
10+
41
11+
13
12+
8
13+
50
14+
44
15+
33
16+
5
17+
27
18+
10
19+
25
20+
17
21+
1
22+
28
23+
22
24+
6
25+
32
26+
35
27+
28+
Player 2:
29+
19
30+
34
31+
38
32+
21
33+
43
34+
14
35+
23
36+
46
37+
16
38+
3
39+
36
40+
31
41+
37
42+
45
43+
30
44+
15
45+
49
46+
48
47+
24
48+
9
49+
2
50+
18
51+
4
52+
7
53+
20

‎2020/22_loop.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Player 1:
2+
43
3+
19
4+
5+
Player 2:
6+
2
7+
29
8+
14

‎2020/22_test.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Player 1:
2+
9
3+
2
4+
6
5+
3
6+
1
7+
8+
Player 2:
9+
5
10+
8
11+
4
12+
7
13+
10

‎2020/22a.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from aocd import submit
2+
from aoc import *
3+
from collections import defaultdict, deque
4+
from itertools import combinations
5+
from pprint import pprint
6+
from math import sqrt
7+
import re
8+
9+
10+
FILE = "22_test.txt"
11+
FILE = "22.txt"
12+
13+
14+
def main():
15+
inp = file(FILE)
16+
p1,p2 = inp.split('\n\n')
17+
p1 = p1.split('\n')[1:]
18+
p2 = p2.split('\n')[1:]
19+
print(p1)
20+
print()
21+
print(p2)
22+
p1 = [int(x) for x in p1]
23+
p2 = [int(x) for x in p2]
24+
hand1 = deque(p1)
25+
hand2 = deque(p2)
26+
out = 0
27+
while len(hand1) > 0 and len(hand2) > 0:
28+
if hand1[0] > hand2[0]:
29+
hand1.extend((hand1.popleft(), hand2.popleft()))
30+
else:
31+
hand2.extend((hand2.popleft(), hand1.popleft()))
32+
33+
if len(hand1) > 0:
34+
h = hand1
35+
else:
36+
h = hand2
37+
score = 0
38+
for i, c in enumerate(h):
39+
score += (len(h)-i) * c
40+
out = score
41+
print(out)
42+
return
43+
input()
44+
print("submitting")
45+
submit(out)
46+
47+
48+
main()

‎2020/22b.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from aocd import submit
2+
from aoc import *
3+
from collections import defaultdict, deque
4+
from itertools import combinations
5+
from pprint import pprint
6+
from math import sqrt
7+
import re
8+
import functools
9+
10+
11+
FILE = "22_test.txt"
12+
FILE = "22.txt"
13+
14+
15+
def recursive_combat(hand1, hand2):
16+
games = set()
17+
while len(hand1) > 0 and len(hand2) > 0:
18+
game = (tuple(hand1), tuple(hand2))
19+
if game in games:
20+
score = 0
21+
for i, c in enumerate(hand1):
22+
score += (len(hand1)-i) * c
23+
return 'player1'
24+
games.add(game)
25+
card1 = hand1.popleft()
26+
card2 = hand2.popleft()
27+
if card1 <= len(hand1) and card2 <= len(hand2):
28+
winner = recursive_combat(deque(list(hand1)[:card1]), deque(list(hand2)[:card2]))
29+
if winner == 'player1':
30+
hand1.extend((card1, card2))
31+
elif winner == 'player2':
32+
hand2.extend((card2, card1))
33+
else:
34+
hand1.extend((card1, card2))
35+
return 'instant'
36+
else:
37+
if card1 > card2:
38+
hand1.extend((card1, card2))
39+
else:
40+
hand2.extend((card2, card1))
41+
42+
if len(hand1) > 0:
43+
return 'player1'
44+
else:
45+
return 'player2'
46+
47+
48+
def main():
49+
inp = file(FILE).rstrip()
50+
p1,p2 = inp.split('\n\n')
51+
p1 = p1.split('\n')[1:]
52+
p2 = p2.split('\n')[1:]
53+
print(p1)
54+
print()
55+
print(p2)
56+
p1 = [int(x) for x in p1]
57+
p2 = [int(x) for x in p2]
58+
hand1 = deque(p1)
59+
hand2 = deque(p2)
60+
out = 0
61+
games = set()
62+
winner = recursive_combat(hand1, hand2, games)
63+
if winner == 'instant':
64+
winner = 'player1'
65+
if winner == 'player1':
66+
h = hand1
67+
else:
68+
h = hand2
69+
score = 0
70+
for i, c in enumerate(h):
71+
score += (len(h)-i) * c
72+
out = score
73+
print(out)
74+
return
75+
input()
76+
print("submitting")
77+
submit(out)
78+
79+
80+
main()

‎README.org

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
| 25 | | | | | | |
77
| 24 | | | | | | |
88
| 23 | | | | | | |
9-
| 22 | | | | | | |
10-
| 21 | | | | | | |
11-
| 20 | | | | | | |
9+
| 22 | 00:17:33 | 2337 | 0 | 01:50:06 | 2972 | 0 |
10+
| 21 | 01:09:20 | 2557 | 0 | 01:29:52 | 2620 | 0 |
11+
| 20 | 01:45:13 | 2354 | 0 | 13:18:21 | 3901 | 0 |
1212
| 19 | 02:48:48 | 3837 | 0 | 14:13:20 | 8175 | 0 |
1313
| 18 | 01:47:16 | 4744 | 0 | 03:35:09 | 5552 | 0 |
1414
| 17 | 00:33:19 | 1331 | 0 | 00:36:42 | 1106 | 0 |

0 commit comments

Comments
 (0)
Please sign in to comment.