2024 - Day 22 (Monkey Market)

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
#!/usr/bin/env python3

#Advent of Code
#2024 Day 22, 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: list[int]) -> int:
    # Steps to evolve a secret number:
    # Multiply by 64, mix, and prune   => left shift 6,  XOR with original, AND with 16777215
    # Divide by 32, mix, and prune     => right shift 5, XOR with original, AND with 16777215
    # Multiply by 2048, mix, and prune => left shift 11, XOR with original, AND with 16777215
    def evolve(original: int) -> int:
        working = ((original << 6) ^ original) & 16777215
        working = ((working >> 5) ^ working) & 16777215
        return ((working << 11) ^ working) & 16777215

    def generate(n: int) -> int:
        working = n
        for _ in range(2000):
            working = evolve(working)
        return working

    return sum(map(generate, puzzle_input))

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_int_list("\n")

    return "The sum of the 2000th secret number generated by each buyer is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([1, 10, 100, 2024]), 37327623)

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
#!/usr/bin/env python3

#Advent of Code
#2024 Day 22, 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 defaultdict

def solve(puzzle_input: list[int]) -> int:
    # Steps to evolve a secret number:
    # Multiply by 64, mix, and prune   => left shift 6,  XOR with original, AND with 16777215
    # Divide by 32, mix, and prune     => right shift 5, XOR with original, AND with 16777215
    # Multiply by 2048, mix, and prune => left shift 11, XOR with original, AND with 16777215
    def evolve(original: int) -> int:
        working = ((original << 6) ^ original) & 16777215
        working = ((working >> 5) ^ working) & 16777215
        return ((working << 11) ^ working) & 16777215

    def generate(n: int) -> list[int]:
        output: list[int] = [n]
        for _ in range(2000):
            output.append(evolve(output[-1]))
        return list(map(lambda x: x % 10, output))

    deltas: defaultdict[tuple[int, int, int, int], int] = defaultdict(int)

    def find_deltas_to_bananas(n: int) -> None:
        l: list[int] = generate(n)

        seen: set[tuple[int, int, int, int]] = set()
        for i in range(0, len(l) - 4):
            # Scans the entire list such that we can extract 5 numbers at a time (for 4 deltas at a time)
            if (delta_pattern := tuple(l[j+1] - l[j] for j in range(i, i+4))) not in seen:
                # Save the number of bananas that would be awarded if using this pattern
                # (so if deltas is in seen, we've already seen the pattern before and wouldn't reach this stage)
                deltas[delta_pattern] += l[i + 4]
                seen.add(delta_pattern)

    for monkey in puzzle_input:
        find_deltas_to_bananas(monkey)
    return max(deltas.values())

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_int_list("\n")

    return "The most bananas you can get is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([1, 2, 3, 2024]), 23)

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