-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
831963a
commit 5c894db
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |