My advent of code solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

77 lines
1.9 KiB

"""
Advent of Code 2021 - Day 09
Run with:
python puzzles/day09.py inputs/day09.txt
"""
import pathlib
import sys
from typing import List, Tuple
class Heightmap:
low_point_values = None
def __init__(self, data: List[List[int]]) -> None:
self.heightmap = data
def find_low_points(self):
self.low_point_values = []
max_y = len(self.heightmap)-1
max_x = len(self.heightmap[0])-1
for y, row in enumerate(self.heightmap):
for x, value in enumerate(row):
if x > 0 and self.heightmap[y][x-1] <= value:
continue
elif x != max_x and self.heightmap[y][x+1] <= value:
continue
elif y > 0 and self.heightmap[y-1][x] <= value:
continue
elif y != max_y and self.heightmap[y+1][x] <= value:
continue
self.low_point_values.append((x,y))
def calculate_risk_score(self):
if self.low_point_values is None:
self.find_low_points()
return sum(self.heightmap[coords[1]][coords[0]]+1 for coords in self.low_point_values)
def part1(inputs: List[int]) -> int:
h = Heightmap(inputs)
return h.calculate_risk_score()
def part2(inputs: List[int]) -> int:
return False
def parse(inputs: str) -> List[int]:
"""Parse the input string"""
return [[int(num) for num in line] for line in inputs.split()]
def solve(path: str) -> Tuple[int, int]:
"""Solve the puzzle"""
puzzle_input = parse(pathlib.Path(path).read_text().strip())
part1_result = part1(puzzle_input)
part2_result = part2(puzzle_input)
return part1_result, part2_result
def main() -> None:
for path in sys.argv[1:]:
print(f"Input File: {path}")
part1_result, part2_result = solve(path)
print(f"Part 1 Result: {part1_result}")
print(f"Part 2 Result: {part2_result}")
if __name__ == "__main__":
main()