Skip to content

Commit abbab17

Browse files
committed
Solve Codyssi 14
1 parent 5d2b3ed commit abbab17

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

codyssi/problem14.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Codyssi Day N."""
2+
3+
import queue
4+
import logging
5+
6+
log = logging.info
7+
8+
def solve(part: int, data: str) -> int:
9+
"""Solve the parts."""
10+
rows = [[int(i) for i in line.split()]for line in data.splitlines()]
11+
cols = [list(i) for i in zip(*rows)]
12+
vals = {(x, y): val for y, row in enumerate(rows) for x, val in enumerate(row)}
13+
steps = [(0, 1), (1, 0)]
14+
if part == 1:
15+
return min(
16+
min(sum(i) for i in rows),
17+
min(sum(i) for i in cols),
18+
)
19+
if part == 2:
20+
target = (14, 14)
21+
else:
22+
target = (len(rows) - 1, len(cols) - 1)
23+
24+
safest = {}
25+
todo = queue.PriorityQueue()
26+
todo.put((0, (0, 0)))
27+
while not todo.empty():
28+
danger, pos = todo.get()
29+
danger += vals[pos]
30+
if pos == target:
31+
return danger
32+
if pos in safest and safest[pos] <= danger:
33+
continue
34+
safest[pos] = danger
35+
for step in steps:
36+
next_pos = tuple(i + j for i, j in zip(pos, step))
37+
if next_pos in vals:
38+
todo.put((danger, next_pos))
39+
raise RuntimeError("No solution found.")
40+
41+
42+
TEST_DATA = """\
43+
3 3 1 7 8 4 1 3 1 7 7 6 7 8 7 8 2 7 7 1
44+
9 9 7 6 3 6 9 4 9 2 6 4 5 7 3 9 3 7 5 6
45+
8 9 7 6 7 7 3 2 2 7 8 9 7 1 5 3 1 2 4 4
46+
9 2 8 2 3 5 9 2 6 5 7 8 1 6 7 3 6 7 9 6
47+
4 1 7 5 2 2 7 6 8 7 2 3 9 2 2 1 6 2 7 5
48+
2 9 1 2 9 9 1 2 2 9 3 7 4 5 3 3 7 1 9 4
49+
9 9 5 2 6 6 2 3 1 8 3 3 3 6 7 9 8 3 1 5
50+
8 4 8 7 2 1 7 9 8 7 3 7 9 1 8 5 2 5 2 8
51+
6 8 9 6 6 4 2 2 7 7 7 8 1 2 6 2 6 1 6 7
52+
3 8 8 6 9 9 2 7 8 5 4 1 8 8 5 8 3 5 6 6
53+
2 8 7 2 6 8 4 7 1 7 6 8 9 4 3 1 2 8 9 8
54+
6 2 9 7 7 2 8 7 9 5 6 6 8 2 8 4 4 8 2 2
55+
3 1 2 8 8 4 6 8 9 1 4 3 9 1 4 2 2 1 5 4
56+
5 2 6 7 2 7 3 9 2 1 7 6 1 2 4 2 1 1 5 9
57+
3 6 8 9 4 4 7 7 3 3 4 8 6 1 2 9 7 2 9 6
58+
9 4 5 5 7 4 1 7 7 1 3 2 3 8 1 7 6 3 1 9
59+
5 3 8 3 1 1 5 3 1 5 9 2 3 6 6 4 4 8 5 3
60+
6 3 8 2 9 7 3 6 4 3 2 8 6 9 8 1 2 7 1 5
61+
4 1 2 4 8 7 7 1 8 7 4 4 5 7 2 3 3 8 3 3
62+
1 5 7 3 3 5 1 5 4 1 1 1 9 2 1 4 6 5 6 3"""
63+
64+
TESTS = [
65+
(1, TEST_DATA, 73),
66+
(2, TEST_DATA, 94),
67+
(3, TEST_DATA, 120),
68+
]

codyssi/solutions.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@
3737
13.1 8543
3838
13.2 6130
3939
13.3 7726
40+
14.1 206
41+
14.2 98
42+
14.3 315

0 commit comments

Comments
 (0)