diff --git a/src/holt59/aoc/2024/day5.py b/src/holt59/aoc/2024/day5.py index 83d14b4..f601fa5 100644 --- a/src/holt59/aoc/2024/day5.py +++ b/src/holt59/aoc/2024/day5.py @@ -1,5 +1,7 @@ -import sys from collections import defaultdict +from typing import Any, Iterator + +from ..base import BaseSolver def in_correct_order(update: list[int], requirements: dict[int, set[int]]) -> bool: @@ -39,26 +41,25 @@ def to_correct_order( return update -part1, part2 = sys.stdin.read().strip().split("\n\n") +class Solver(BaseSolver): + def solve(self, input: str) -> Iterator[Any]: + part1, part2 = input.split("\n\n") -requirements: dict[int, set[int]] = defaultdict(set) -for line in part1.splitlines(): - v1, v2 = line.split("|") - requirements[int(v2)].add(int(v1)) + requirements: dict[int, set[int]] = defaultdict(set) + for line in part1.splitlines(): + v1, v2 = line.split("|") + requirements[int(v2)].add(int(v1)) -updates = [list(map(int, line.split(","))) for line in part2.splitlines()] + updates = [list(map(int, line.split(","))) for line in part2.splitlines()] -answer_1 = sum( - update[len(update) // 2] - for update in updates - if in_correct_order(update, requirements) -) + yield sum( + update[len(update) // 2] + for update in updates + if in_correct_order(update, requirements) + ) -answer_2 = sum( - to_correct_order(update, requirements, len(update) // 2 + 1)[-1] - for update in updates - if not in_correct_order(update, requirements) -) - -print(f"answer 1 is {answer_1}") -print(f"answer 2 is {answer_2}") + yield sum( + to_correct_order(update, requirements, len(update) // 2 + 1)[-1] + for update in updates + if not in_correct_order(update, requirements) + ) diff --git a/src/holt59/aoc/2024/day6.py b/src/holt59/aoc/2024/day6.py index dd55d89..34f1d51 100644 --- a/src/holt59/aoc/2024/day6.py +++ b/src/holt59/aoc/2024/day6.py @@ -1,6 +1,7 @@ import itertools as it -import sys -from typing import TypeAlias +from typing import Any, Iterator, TypeAlias + +from ..base import BaseSolver NodeType: TypeAlias = tuple[tuple[int, int], tuple[int, int]] EdgesType: TypeAlias = dict[NodeType, tuple[NodeType, set[tuple[int, int]]]] @@ -91,44 +92,31 @@ def is_loop(lines: list[str], edges: EdgesType, position: tuple[int, int]): return current_node in found -def print_grid( - lines: list[str], marked: set[tuple[int, int]], current_pos: tuple[int, int] | None -): - chars = list(map(list, lines)) +class Solver(BaseSolver): + def solve(self, input: str) -> Iterator[Any]: + # read lines + lines = input.splitlines() - for i, j in marked: - chars[i][j] = "X" + # find and delete original position + start_pos = next( + (i, j) + for i, row in enumerate(lines) + for j, col in enumerate(row) + if col == "^" + ) + lines[start_pos[0]] = lines[start_pos[0]].replace("^", ".") - if current_pos: - chars[current_pos[0]][current_pos[1]] = "T" + # compute edges from the map + edges = compute_graph(lines, (start_pos, (-1, 0))) - for line in chars: - print("".join(line)) - print() + # part 1 + marked: set[tuple[int, int]] = set() + current_node = START_NODE + while current_node[0] != FINAL_POS: + current_node, current_marked = edges[current_node] + marked = marked.union(current_marked) -# read lines -lines = sys.stdin.read().splitlines() + yield len(marked) -# find and delete original position -start_pos = next( - (i, j) for i, row in enumerate(lines) for j, col in enumerate(row) if col == "^" -) -lines[start_pos[0]] = lines[start_pos[0]].replace("^", ".") - -# compute edges from the map -edges = compute_graph(lines, (start_pos, (-1, 0))) - -# part 1 -marked: set[tuple[int, int]] = set() -current_node = START_NODE - -while current_node[0] != FINAL_POS: - current_node, current_marked = edges[current_node] - marked = marked.union(current_marked) - -answer_1 = len(marked) -print(f"answer 1 is {answer_1}") - -answer_2 = sum(is_loop(lines, edges, pos) for pos in marked if pos != start_pos) -print(f"answer 2 is {answer_2}") + yield sum(is_loop(lines, edges, pos) for pos in marked if pos != start_pos)