-
Notifications
You must be signed in to change notification settings - Fork 17
/
day21.py
executable file
·144 lines (112 loc) · 2.54 KB
/
day21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
from utils.all import *
advent.setup(2021, 21)
DEBUG = 'debug' in map(str.lower, sys.argv)
if not DEBUG:
fin = advent.get_input()
p1start = int(re.findall('\d+', next(fin))[1])
p2start = int(re.findall('\d+', next(fin))[1])
else:
p1start = 4
p2start = 8
timer_start()
p1 = p1start
p2 = p2start
p1score = 0
p2score = 0
die = 1
rolls = 0
while 1:
p1 += die
die += 1
if die > 100: die = 1
p1 += die
die += 1
if die > 100: die = 1
p1 += die
die += 1
if die > 100: die = 1
rolls += 3
while p1 > 10:
p1 -= 10
p1score += p1
# print('1:', die, ':', p1, ':', p1score)
if p1score >= 1000:
loser_score = p2score
break
p2 += die
die += 1
if die > 100: die = 1
p2 += die
die += 1
if die > 100: die = 1
p2 += die
die += 1
if die > 100: die = 1
rolls += 3
while p2 > 10:
p2 -= 10
p2score += p2
# print('2:', die, ':', p2, ':', p2score)
if p2score >= 1000:
loser_score = p1score
break
# print(rolls)
# print(loser_score)
ans = rolls * loser_score
advent.print_answer(1, ans)
# wait('Submit? ')
# advent.submit_answer(1, ans)
dicerolls = tuple(map(sum, product(range(1, 4), range(1, 4), range(1, 4))))
def doturn(game):
p1wins = 0
p2wins = 0
newgame = defaultdict(int)
# advance all universes
for key, n in game.items():
# eprint(key, n)
p1pos, p2pos, p1score, p2score, who = key
if who == 1:
# p1 plays
for roll in dicerolls:
p1newpos = p1pos + roll
while p1newpos > 10:
p1newpos -= 10
p1newscore = p1score + p1newpos
if p1newscore >= 21:
p1wins += n
else:
newgame[p1newpos, p2pos, p1newscore, p2score, 2] += n
else:
# p2 plays
for roll in dicerolls:
p2newpos = p2pos + roll
while p2newpos > 10:
p2newpos -= 10
p2newscore = p2score + p2newpos
if p2newscore >= 21:
p2wins += n
else:
newgame[p1pos, p2newpos, p1score, p2newscore, 1] += n
return newgame, p1wins, p2wins
ans = 0
# NOPE can't work
# p1 = defaultdict(int, {(6, 0): 1}) # (pos, score) -> n of universes
# p2 = defaultdict(int, {(4, 0): 1}) # (pos, score) -> n of universes
# (p1pos, p2pos, p1score, p2score, turn_of_who) -> n of universes
# game = defaultdict(int, {(4, 8, 0, 0, 1): 1}) # example
game = defaultdict(int, {(p1start, p2start, 0, 0, 1): 1})
p1tot = p2tot = 0
while game:
game, a, b = doturn(game)
p1tot += a
p2tot += b
ans = max(p1tot, p2tot)
# if DEBUG:
# eprint('got :', ans)
# eprint('expected:', 444356092776315)
# if ans != 444356092776315:
# sys.exit(0)
advent.print_answer(2, ans)
# wait('Submit? ')
# advent.submit_answer(2, ans)