DEV Community

Cover image for Advent of Code 2023 - December 5th
Rob van der Leek
Rob van der Leek

Posted on

Advent of Code 2023 - December 5th

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 5th

The puzzle of day 5 is absolute horror. A naive implementation of the logic does not work for part 2. So far, I've only solved the first part ๐Ÿ˜ž.

My pitfall for this puzzle: A naive implementation is not going to cut it but I'm struggling with a more elegant and optimized solution. I will update this post as soon as I do of course ๐Ÿ˜„

UPDATE: In the end a brute-force approach with the naive implementation did work, it took quite some time on my laptop to compute.

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

with open('input-small.txt') as infile:
    lines = infile.readlines()

seeds = [int(s) for s in lines[0].strip().split(': ')[1].split(' ')]
line_nr = 3

def parse_mapping(lines, line_nr):
    result = []
    while lines[line_nr].strip():
        mapping = lines[line_nr].strip().split(' ')
        start = int(mapping[1])
        end = start + int(mapping[2])
        shift = int(mapping[0]) - start
        result.append((start, end, shift))
        if line_nr < len(lines) - 1:
            line_nr += 1
        else:
            break
    if line_nr < len(lines) - 1:
        line_nr += 2
    return line_nr, result

def do_mappings(number, mappings):
    for mapping in mappings:
        for m in mapping:
            if number >= m[0] and number < m[1]:
                number += m[2]
                break
    return number

[line_nr, seed_to_soil] = parse_mapping(lines, line_nr)
[line_nr, soil_to_fertilizer] = parse_mapping(lines, line_nr)
[line_nr, fertilizer_to_water] = parse_mapping(lines, line_nr)
[line_nr, water_to_light] = parse_mapping(lines, line_nr)
[line_nr, light_to_temperature] = parse_mapping(lines, line_nr)
[line_nr, temperature_to_humidity] = parse_mapping(lines, line_nr)
[line_nr, humidity_to_location] = parse_mapping(lines, line_nr)

lowest_location = None

while seeds:
    start = int(seeds.pop(0))
    end = start + int(seeds.pop(0))
    for s in range(start, end):
        location = do_mappings(s, [seed_to_soil, soil_to_fertilizer, 
            fertilizer_to_water, water_to_light, light_to_temperature,
            temperature_to_humidity, humidity_to_location])

        if not lowest_location or location < lowest_location:
            lowest_location = location
            print(f'New lowest: {lowest_location}')

print(lowest_location)
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

Top comments (0)