This commit is contained in:
62
day-21/day-21.py
Normal file
62
day-21/day-21.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def quantum_roll(state, p0, p1, s0, s1):
|
||||
if s0 >= 21:
|
||||
return (1, 0)
|
||||
if s1 >= 21:
|
||||
return (0, 1)
|
||||
if (p0, p1, s0, s1) in state:
|
||||
return state[(p0, p1, s0, s1)]
|
||||
res = (0, 0)
|
||||
for d0 in [1, 2, 3]:
|
||||
for d1 in [1, 2, 3]:
|
||||
for d2 in [1, 2, 3]:
|
||||
new_p0 = (p0 + d0 + d1 + d2) % 10
|
||||
new_s0 = s0 + new_p0 + 1
|
||||
(x1, y1) = quantum_roll(state, p1, new_p0, s1, new_s0)
|
||||
res = (res[0] + y1, res[1] + x1)
|
||||
state[(p0, p1, s0, s1)] = res
|
||||
return res
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
pos = []
|
||||
pos.append(int(input[0].rstrip().split()[-1]) - 1)
|
||||
pos.append(int(input[1].rstrip().split()[-1]) - 1)
|
||||
score = [0, 0]
|
||||
p = 0
|
||||
dice = 1
|
||||
while True:
|
||||
pos[p] += sum(range(dice, dice + 3))
|
||||
pos[p] %= 10
|
||||
score[p] += pos[p] + 1
|
||||
dice += 3
|
||||
if 1000 <= score[p]:
|
||||
p += 1
|
||||
p %= 2
|
||||
break
|
||||
p += 1
|
||||
p %= 2
|
||||
result = score[p] * (dice - 1)
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
state = {}
|
||||
p0 = int(input[0].rstrip().split()[-1]) - 1
|
||||
p1 = int(input[1].rstrip().split()[-1]) - 1
|
||||
result = max(quantum_roll(state, p0, p1, 0, 0))
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
Reference in New Issue
Block a user