Refactor code for API #3
@ -1,5 +1,7 @@
|
|||||||
import sys
|
|
||||||
from collections import defaultdict
|
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:
|
def in_correct_order(update: list[int], requirements: dict[int, set[int]]) -> bool:
|
||||||
@ -39,7 +41,9 @@ def to_correct_order(
|
|||||||
return update
|
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)
|
requirements: dict[int, set[int]] = defaultdict(set)
|
||||||
for line in part1.splitlines():
|
for line in part1.splitlines():
|
||||||
@ -48,17 +52,14 @@ for line in part1.splitlines():
|
|||||||
|
|
||||||
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(
|
yield sum(
|
||||||
update[len(update) // 2]
|
update[len(update) // 2]
|
||||||
for update in updates
|
for update in updates
|
||||||
if in_correct_order(update, requirements)
|
if in_correct_order(update, requirements)
|
||||||
)
|
)
|
||||||
|
|
||||||
answer_2 = sum(
|
yield sum(
|
||||||
to_correct_order(update, requirements, len(update) // 2 + 1)[-1]
|
to_correct_order(update, requirements, len(update) // 2 + 1)[-1]
|
||||||
for update in updates
|
for update in updates
|
||||||
if not in_correct_order(update, requirements)
|
if not in_correct_order(update, requirements)
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"answer 1 is {answer_1}")
|
|
||||||
print(f"answer 2 is {answer_2}")
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import itertools as it
|
import itertools as it
|
||||||
import sys
|
from typing import Any, Iterator, TypeAlias
|
||||||
from typing import TypeAlias
|
|
||||||
|
from ..base import BaseSolver
|
||||||
|
|
||||||
NodeType: TypeAlias = tuple[tuple[int, int], tuple[int, int]]
|
NodeType: TypeAlias = tuple[tuple[int, int], tuple[int, int]]
|
||||||
EdgesType: TypeAlias = dict[NodeType, tuple[NodeType, set[tuple[int, int]]]]
|
EdgesType: TypeAlias = dict[NodeType, tuple[NodeType, set[tuple[int, int]]]]
|
||||||
@ -91,28 +92,17 @@ def is_loop(lines: list[str], edges: EdgesType, position: tuple[int, int]):
|
|||||||
return current_node in found
|
return current_node in found
|
||||||
|
|
||||||
|
|
||||||
def print_grid(
|
class Solver(BaseSolver):
|
||||||
lines: list[str], marked: set[tuple[int, int]], current_pos: tuple[int, int] | None
|
def solve(self, input: str) -> Iterator[Any]:
|
||||||
):
|
|
||||||
chars = list(map(list, lines))
|
|
||||||
|
|
||||||
for i, j in marked:
|
|
||||||
chars[i][j] = "X"
|
|
||||||
|
|
||||||
if current_pos:
|
|
||||||
chars[current_pos[0]][current_pos[1]] = "T"
|
|
||||||
|
|
||||||
for line in chars:
|
|
||||||
print("".join(line))
|
|
||||||
print()
|
|
||||||
|
|
||||||
|
|
||||||
# read lines
|
# read lines
|
||||||
lines = sys.stdin.read().splitlines()
|
lines = input.splitlines()
|
||||||
|
|
||||||
# find and delete original position
|
# find and delete original position
|
||||||
start_pos = next(
|
start_pos = next(
|
||||||
(i, j) for i, row in enumerate(lines) for j, col in enumerate(row) if col == "^"
|
(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("^", ".")
|
lines[start_pos[0]] = lines[start_pos[0]].replace("^", ".")
|
||||||
|
|
||||||
@ -127,8 +117,6 @@ while current_node[0] != FINAL_POS:
|
|||||||
current_node, current_marked = edges[current_node]
|
current_node, current_marked = edges[current_node]
|
||||||
marked = marked.union(current_marked)
|
marked = marked.union(current_marked)
|
||||||
|
|
||||||
answer_1 = len(marked)
|
yield 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)
|
yield sum(is_loop(lines, edges, pos) for pos in marked if pos != start_pos)
|
||||||
print(f"answer 2 is {answer_2}")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user