Advent of Code 2023 – December 5th

Advent of Code 2023 (25 Part Series)

1 Advent of Code 2023
2 Advent of Code 2023 – December 2nd
21 more parts…
3 Advent of Code 2023 – December 3rd
4 Advent of Code 2023 – December 4th
5 Advent of Code 2023 – December 5th
6 Advent of Code 2023 – December 6th
7 Advent of Code 2023 – December 7th
8 Advent of Code 2023 – December 8th
9 Advent of Code 2023 – December 9th
10 Advent of Code 2023 – December 10th
11 Advent of Code 2023 – December 11th
12 Advent of Code 2023 – December 12th
13 Advent of Code 2023 – December 13th
14 Advent of Code 2023 – December 14th
15 Advent of Code 2023 – December 15th
16 Advent of Code 2023 – December 16th
17 Advent of Code 2023 – December 17th
18 Advent of Code 2023 – December 18th
19 Advent of Code 2023 – December 19th
20 Advent of Code 2023 – December 20th
21 Advent of Code 2023 – December 21st
22 Advent of Code 2023 – December 22nd
23 Advent of Code 2023 – December 23rd
24 Advent of Code 2023 – December 24th
25 Advent of Code 2023 – December 25th

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!

Advent of Code 2023 (25 Part Series)

1 Advent of Code 2023
2 Advent of Code 2023 – December 2nd
21 more parts…
3 Advent of Code 2023 – December 3rd
4 Advent of Code 2023 – December 4th
5 Advent of Code 2023 – December 5th
6 Advent of Code 2023 – December 6th
7 Advent of Code 2023 – December 7th
8 Advent of Code 2023 – December 8th
9 Advent of Code 2023 – December 9th
10 Advent of Code 2023 – December 10th
11 Advent of Code 2023 – December 11th
12 Advent of Code 2023 – December 12th
13 Advent of Code 2023 – December 13th
14 Advent of Code 2023 – December 14th
15 Advent of Code 2023 – December 15th
16 Advent of Code 2023 – December 16th
17 Advent of Code 2023 – December 17th
18 Advent of Code 2023 – December 18th
19 Advent of Code 2023 – December 19th
20 Advent of Code 2023 – December 20th
21 Advent of Code 2023 – December 21st
22 Advent of Code 2023 – December 22nd
23 Advent of Code 2023 – December 23rd
24 Advent of Code 2023 – December 24th
25 Advent of Code 2023 – December 25th

原文链接:Advent of Code 2023 – December 5th

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容