2023 - Day 15 (Lens Library)

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

#Advent of Code
#2023 Day 15, 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[str]) -> int:
    def HASH(s) -> int:
        current_value = 0
        for c in s:
            current_value = ((current_value + ord(c)) * 17) % 256
        return current_value

    return sum(map(HASH, puzzle_input))

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

    return "The sum of the hashes is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["rn=1", "cm-", "qp=3", "cm=2", "qp-", "pc=4", "ot=9", "ab=5", "pc-", "pc=6", "ot=7"]), 1320)

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
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3

#Advent of Code
#2023 Day 15, 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 functools import cache
import typing as t

def solve(puzzle_input: list[str]) -> int:
    class Box:
        number: int
        lenses: list[tuple[str, int]]

        def __init__(self: t.Self, number: int) -> None:
            self.number = number
            self.lenses = []

        def remove_lens(self: t.Self, label: str) -> None:
            for i, lens in enumerate(self.lenses):
                if lens[0] == label:
                    self.lenses.pop(i)
                    break

        def add_lens(self: t.Self, label: str, focal_length: int) -> None:
            for i, lens in enumerate(self.lenses):
                if lens[0] == label:
                    self.lenses[i] = (label, focal_length)
                    break
            else:
                self.lenses.append((label, focal_length))

        def focussing_power_of_lenses(self: t.Self) -> int:
            return sum(
                (self.number + 1) * slot * focal_length
                for slot, (_, focal_length) in enumerate(self.lenses, 1)
            )

    @cache
    def HASH(s) -> int:
        current_value = 0
        for c in s:
            current_value = ((current_value + ord(c)) * 17) % 256
        return current_value

    boxes = [Box(n) for n in range(256)]

    for instruction in puzzle_input:
        match instruction[-1]:
            case "-":
                boxes[HASH(instruction[:-1])].remove_lens(instruction[:-1])
            case _:
                boxes[HASH(instruction[:-2])].add_lens(instruction[:-2], int(instruction[-1]))

    return sum(box.focussing_power_of_lenses() for box in boxes)

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

    return "The focussing power of the resulting lens configuration is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["rn=1", "cm-", "qp=3", "cm=2", "qp-", "pc=4", "ot=9", "ab=5", "pc-", "pc=6", "ot=7"]), 145)

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