#!/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 (w1, w0) = quantum_roll(state, p1, new_p0, s1, new_s0) res = (res[0] + w0, res[1] + w1) state[(p0, p1, s0, s1)] = res return res def part_1(input): result = 0 pos = [int(line.rstrip().split()[-1]) - 1 for line in input] 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, p1 = [int(line.rstrip().split()[-1]) - 1 for line in input] 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)