Advent of Code 2023 - December 15th

robvanderleek

Rob van der Leek

Posted on December 15, 2023

Advent of Code 2023 - December 15th

In this series, I'll share my progress with the 2023 version of Advent of Code.

Check the first post for a short intro to this series.

You can also follow my progress on GitHub.

December 15th

The puzzle of day 15 was surprisingly easy. The first part was so trivial it made me nervous for the complexity of the second part. But also part two was really straightforward. A nice start of the weekend 😄

My pitfall for this puzzle: Not really, this day was way easier than previous days.

Solution here, do not click if you want to solve the puzzle first yourself
#!/usr/bin/env python3
import re

with open('input.txt') as infile:
    lines = infile.readlines()
parts = lines[0].strip().split(',')

def calc_hash(l):
    result = 0
    for c in l:
        result += ord(c)
        result = result * 17
        result = result % 256
    return result

boxes = []
for _ in range(256):
    boxes.append([])

for p in parts:
    label = re.split('[=-]', p)[0]
    box = calc_hash(label)
    if p[-1] == '-':
        boxes[box] = [b for b in boxes[box] if not b.startswith(label)]
    else:
        focal_length = p[-1]
        for idx, lens in enumerate(boxes[box]):
            if lens.startswith(label):
                boxes[box][idx] = f'{label} {focal_length}'
                break
        else:
            boxes[box].append(f'{label} {focal_length}')

total = 0
for idx_b, b in enumerate(boxes):
    for idx_l, lens in enumerate(b):
        total += (idx_b + 1) * (idx_l + 1) * int(lens[-1])
print(total)
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

💖 💪 🙅 🚩
robvanderleek
Rob van der Leek

Posted on December 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of Code 2023 - December 16th
advent Advent of Code 2023 - December 16th

December 18, 2023

Advent of Code 2023 - December 15th
advent Advent of Code 2023 - December 15th

December 15, 2023

Advent of Code 2023 - December 13th
advent Advent of Code 2023 - December 13th

December 13, 2023

Advent of Code 2023 - December 8th
advent Advent of Code 2023 - December 8th

December 8, 2023

Advent of Code 2023 - December 7th
advent Advent of Code 2023 - December 7th

December 7, 2023