2024 day 6 new API.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2024-12-06 21:15:18 +01:00
parent ae4f42517c
commit 8c707c00ba
2 changed files with 46 additions and 57 deletions

View File

@ -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,26 +41,25 @@ 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():
v1, v2 = line.split("|") v1, v2 = line.split("|")
requirements[int(v2)].add(int(v1)) 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( 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}")

View File

@ -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,44 +92,31 @@ 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]:
): # read lines
chars = list(map(list, lines)) lines = input.splitlines()
for i, j in marked: # find and delete original position
chars[i][j] = "X" 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: # compute edges from the map
chars[current_pos[0]][current_pos[1]] = "T" edges = compute_graph(lines, (start_pos, (-1, 0)))
for line in chars: # part 1
print("".join(line)) marked: set[tuple[int, int]] = set()
print() current_node = START_NODE
while current_node[0] != FINAL_POS:
# read lines
lines = sys.stdin.read().splitlines()
# 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] 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}")