Skip to content

Commit

Permalink
day05: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 24, 2023
1 parent cad219a commit 17fdf33
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
21 changes: 14 additions & 7 deletions day05/day5.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,25 @@ def seed_to_mapping_ranges(data: list[int]) -> list[MappingRange]:
return result


def main() -> None:
"""main function, solve all the problems"""
seeds, maps = grab_inputs(INPUT)
# q1
def part1(seeds: list[int], maps: list[NamedMap]) -> int:
locations = [get_location(seed, maps) for seed in seeds]
locations.sort()
print(locations[0])
return locations[0]

# q2

def part2(seeds: list[int], maps: list[NamedMap]) -> int:
start_ranges = seed_to_mapping_ranges(seeds)
end_locations = get_location_ranges(start_ranges, maps)
print(min(location.start for location in end_locations))
return min(location.start for location in end_locations)


def main() -> None:
"""main function, solve all the problems"""
seeds, maps = grab_inputs(INPUT)
# q1
print(part1(seeds, maps))
# q2
print(part2(seeds, maps))


if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions day05/lib/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Mapping:
dest_end: int = field(init=False, repr=False)
size: int

injected: bool = field(default=False)
injected: bool = False

def __post_init__(self) -> None:
self.src_end = self.src_start + self.size
Expand All @@ -33,7 +33,7 @@ def get_mapping(self, src_value: int) -> int:
if self.src_start <= src_value < self.src_end:
return src_value - self.src_start + self.dest_start

raise ValueError("Item not within mapping range")
raise ValueError(f"Item not within mapping range {src_value, self}")

def get_mappings(self, start: int, end: int) -> tuple[MappingRange, int]:
"""
Expand Down Expand Up @@ -108,11 +108,12 @@ def extend_mapping_range(self, mappings: list[Mapping]) -> list[Mapping]:
start = mappings[-1].src_end
injected_mapping = Mapping(start, start, INT_MAX - start, True)
mappings.append(injected_mapping)

return mappings

def get_mapping(self, value: int) -> int:
"""Uses binary search to grab the correct mapping, then apply it to one value"""
mapping_idx = bisect_left(self.mappings, value, key=lambda m: m.src_end)
mapping_idx = bisect_left(self.mappings, value, key=lambda m: m.src_end - 1)
mapping = self.mappings[mapping_idx]
return mapping.get_mapping(value)

Expand Down
41 changes: 41 additions & 0 deletions day05/tests/test_day5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import TYPE_CHECKING

from day05.day5 import INPUT_SMALL, part1, part2, seed_to_mapping_ranges
from day05.lib.classes import MappingRange
from day05.lib.parsers import grab_inputs

if TYPE_CHECKING:
from day05.lib.classes import NamedMap


def test_mapping() -> None:
seeds: list[int]
maps: list[NamedMap]
seeds, maps = grab_inputs(INPUT_SMALL)

assert len(seeds) == 4
assert len(maps) == 7

results = [79, 81, 81, 81, 74, 78, 78, 82]

for index, map in enumerate(maps):
assert map.get_mapping(results[index]) == results[index + 1]


def test_part1() -> None:
seeds, maps = grab_inputs(INPUT_SMALL)
assert part1(seeds, maps) == 35


def test_part2() -> None:
seeds, maps = grab_inputs(INPUT_SMALL)
assert part2(seeds, maps) == 46


def test_seed_to_mapping_ranges() -> None:
data = [79, 14, 55, 13]
# 79, len(14) -> 79, 93
# 55, len(13) -> 55, 68
ranges = seed_to_mapping_ranges(data)
assert ranges[0] == MappingRange(79, 93)
assert ranges[1] == MappingRange(55, 68)

0 comments on commit 17fdf33

Please sign in to comment.