Skip to content

Commit

Permalink
feat: add 2023 day 05 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelblijleven committed Dec 6, 2023
1 parent 831963a commit 5c894db
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/adventofcode/year_2023/day_05_2023.py
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions tests/adventofcode/year_2023/test_day_05_2023.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 5c894db

Please sign in to comment.