2021 - Day 21 (Dirac Dice)

View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3

#Advent of Code
#2021 Day 21, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    class Player:
        def __init__(self, description):
            self.num = int(description.split()[1])
            self.pos = int(description.split()[-1])
            self.score = 0

        def turn(self, die):
            rolls = [next(die) for i in range(3)]
            moves = sum(rolls)
            while moves > 10:
                moves -= 10

            self.pos += moves
            if self.pos > 10:
                self.pos -= 10

            self.score += self.pos
            return self.score >= 1000

    class Die:
        def __init__(self):
            self.rolls = 0
            self.last_val = 0

        def __next__(self):
            self.rolls += 1
            self.last_val += 1
            if self.last_val > 100:
                self.last_val = 1
            return self.last_val

    players = [Player(l) for l in puzzle_input]
    die = Die()

    while True:
        for player in players:
            if player.turn(die):
                return sorted(players, key=lambda p: p.score)[0].score * die.rolls

def main():
    puzzle_input = util.read.as_lines()

    losingscore_dicerolls = solve(puzzle_input)

    print("The product of the losing player's score and the number of dice rolls is " + str(losingscore_dicerolls) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["Player 1 starting position: 4",
                                       "Player 2 starting position: 8"]), 739785)

if __name__ == "__main__":
    run(main)
Part 2
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
#!/usr/bin/env python3

#Advent of Code
#2021 Day 21, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import Counter, defaultdict
from functools import cache

def solve(puzzle_input):
    possible_moves = [sum([a, b, c]) for a in range(1, 4) for b in range(1, 4) for c in range(1, 4)]
    move_quants = Counter(possible_moves)
    move_set = set(possible_moves)

    @cache
    def play(active_num, active_pos, active_score, inactive_num, inactive_pos, inactive_score, moves=0, start=True):
        if not start:
            if active_pos + moves <= 10:
                new_active_pos   = active_pos + moves
            else:
                new_active_pos   = active_pos + moves - 10
            new_active_score = active_score + new_active_pos

            if new_active_score >= 21:
                return {active_num: 1, inactive_num: 0}
        else:
            new_active_pos   = active_pos
            new_active_score = active_score

        results = defaultdict(int)
        for move in move_set:
            r = play(inactive_num, inactive_pos, inactive_score, active_num, new_active_pos, new_active_score, move, False)
            results[active_num] += r[active_num] * move_quants[move]
            results[inactive_num] += r[inactive_num] * move_quants[move]
        return results

    def make_player(description):
        return (int(description.split()[1]), int(description.split()[-1]), 0)

    player1 = make_player(puzzle_input[0])
    player2 = make_player(puzzle_input[1])
    return max([value for key, value in play(*player2, *player1).items()])

def main():
    puzzle_input = util.read.as_lines()

    wins = solve(puzzle_input)

    print("The number of universes that the most winning player wins in is " + str(wins) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["Player 1 starting position: 4",
                                       "Player 2 starting position: 8"]), 444356092776315)

if __name__ == "__main__":
    run(main)
Return to solution index