Python
0.248 line-seconds (sixth simplest so far after days 6, 2, 1, 4 and 9).
import collections
import re
from .solver import Solver
def _hash(string: str) -> int:
result = 0
for c in string:
result = (result + ord(c)) * 17 % 256
return result
def _assert_full_match(pattern: str, string: str):
m = re.fullmatch(pattern, string)
if not m:
raise RuntimeError(f'pattern {pattern} does not match {string}')
return m
class Day15(Solver):
input: list[str]
def __init__(self):
super().__init__(15)
def presolve(self, input: str):
self.input = input.rstrip().split(',')
def solve_first_star(self) -> int:
return sum(_hash(string) for string in self.input)
def solve_second_star(self) -> int:
boxes = [collections.OrderedDict() for _ in range(256)]
for instruction in self.input:
label, op, value = _assert_full_match(r'([a-z]+)([=-])(\d*)', instruction).groups()
box = boxes[_hash(label)]
match op:
case '-':
if label in box:
del box[label]
case '=':
box[label] = value
return sum((1 + box_idx) * (1 + lens_idx) * int(value)
for box_idx, box in enumerate(boxes)
for lens_idx, (_, value) in enumerate(box.items()))
9ms * 35 LOC is ~0.350 tho