Advent of Code 2023 - December 5th
Tags: advent, adventofcode, python, december
Published: 2023-12-06 09:02:15

---
title: Advent of Code 2023 - December 5th
published: true
description: My progress with the 2023 version of Advent of Code
tags: advent, adventofcode, python, december
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36qayk14ewje7rvykeg5.jpg
series: Advent of Code 2023
# published_at: 2023-12-01 17:41 +0000
---

In this series, I'll share my progress with the 2023 version of [Advent of Code](https://adventofcode.com/2023).

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

You can also follow my progress [on GitHub](https://github.com/robvanderleek/adventofcode).

## December 5th

[The puzzle of day 5](https://adventofcode.com/2023/day/4) is absolute horror. A naive implementation of the logic does not work for part 2. So far, I've only solved the first part :disappointed:.

__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 :smile:

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

{% spoiler Solution here, do not click if you want to solve the puzzle first yourself %}

```python
#!/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)
```

{% endspoiler %}

That's it! See you again tomorrow!