From 5c894db0cd81ae432e8d819d288a5a341f57bf39 Mon Sep 17 00:00:00 2001 From: Marcel Blijleven Date: Wed, 6 Dec 2023 20:03:02 +0100 Subject: [PATCH] feat: add 2023 day 05 part 1 --- src/adventofcode/year_2023/day_05_2023.py | 51 +++++++++++++++++++ .../year_2023/test_day_05_2023.py | 45 ++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/adventofcode/year_2023/day_05_2023.py create mode 100644 tests/adventofcode/year_2023/test_day_05_2023.py diff --git a/src/adventofcode/year_2023/day_05_2023.py b/src/adventofcode/year_2023/day_05_2023.py new file mode 100644 index 0000000..31a4f5a --- /dev/null +++ b/src/adventofcode/year_2023/day_05_2023.py @@ -0,0 +1,51 @@ +import re + +from adventofcode.util.exceptions import SolutionNotFoundError +from adventofcode.registry.decorators import register_solution +from adventofcode.util.input_helpers import get_input_for_day + + +def find_locations(data: list[str]) -> None: + """ + Parse input back to str, and split by \n\n to get the + mappings easier + """ + pattern = re.compile("\\d+") + as_text = "\n".join(data) + sections = [line.split("\n") for line in as_text.split("\n\n")] + seeds = {x: x for x in map(int, pattern.findall(sections[0][0]))} + + for mapping in sections[1:]: + for seed, value in seeds.items(): + for _range in mapping[1:]: + dest, src, length = list(map(int, pattern.findall(_range))) + if src <= value < (src + length): + seeds[seed] = dest + (value - src) + + return min(seeds.values()) + + +@register_solution(2023, 5, 1) +def part_one(input_data: list[str]): + answer = find_locations(input_data) + + if not answer: + raise SolutionNotFoundError(2023, 5, 1) + + return answer + + +@register_solution(2023, 5, 2) +def part_two(input_data: list[str]): + answer = ... + + if not answer: + raise SolutionNotFoundError(2023, 5, 2) + + return answer + + +if __name__ == '__main__': + data = get_input_for_day(2023, 5) + part_one(data) + part_two(data) diff --git a/tests/adventofcode/year_2023/test_day_05_2023.py b/tests/adventofcode/year_2023/test_day_05_2023.py new file mode 100644 index 0000000..4289353 --- /dev/null +++ b/tests/adventofcode/year_2023/test_day_05_2023.py @@ -0,0 +1,45 @@ +from adventofcode.year_2023.day_05_2023 import part_two, part_one, find_locations + +test_input = [ + "seeds: 79 14 55 13", + "", + "seed-to-soil map:", + "50 98 2", + "52 50 48", + "", + "soil-to-fertilizer map:", + "0 15 37", + "37 52 2", + "39 0 15", + "", + "fertilizer-to-water map:", + "49 53 8", + "0 11 42", + "42 0 7", + "57 7 4", + "", + "water-to-light map:", + "88 18 7", + "18 25 70", + "", + "light-to-temperature map:", + "45 77 23", + "81 45 19", + "68 64 13", + "", + "temperature-to-humidity map:", + "0 69 1", + "1 0 69", + "", + "humidity-to-location map:", + "60 56 37", + "56 93 4", +] + + +def test_part_one(): + assert part_one(test_input) == 35 + + +def test_part_two(): + assert part_two(test_input) == 'x'