-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
94 lines (65 loc) · 1.9 KB
/
__init__.py
File metadata and controls
94 lines (65 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from typing import Any, List, Tuple, Dict
from aocpy import BaseChallenge
CUCUMBER_EAST = ">"
CUCUMBER_SOUTH = "v"
EMPTY = "."
Point = Tuple[int, int]
SeaBed = Dict[Point, str]
def parse(instr: str) -> SeaBed:
lines = instr.strip().splitlines()
o: SeaBed = {}
for y, line in enumerate(lines):
for x, char in enumerate(line):
o[(x, y)] = char
return o
def iterate_once(sea_bed: SeaBed) -> bool:
# returns true if moves were made
moves_made = False
changes: SeaBed = {}
def get_point_state(p: Point) -> str:
# if p in changes:
# return changes[p]
return
# eastbound
for point in sea_bed:
point_content = sea_bed[point]
if point_content != CUCUMBER_EAST:
continue
x, y = point
next_point = (x + 1, y)
if next_point not in sea_bed:
next_point = (0, y)
if sea_bed[next_point] == EMPTY:
moves_made = True
changes[next_point] = point_content
changes[point] = EMPTY
for point in changes:
sea_bed[point] = changes[point]
changes = {}
# southbound
for point in sea_bed:
point_content = sea_bed[point]
if point_content != CUCUMBER_SOUTH:
continue
x, y = point
next_point = (x, y + 1)
if next_point not in sea_bed:
next_point = (x, 0)
if sea_bed[next_point] == EMPTY:
moves_made = True
changes[next_point] = point_content
changes[point] = EMPTY
for point in changes:
sea_bed[point] = changes[point]
return moves_made
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
sea_bed = parse(instr)
i = 1
while iterate_once(sea_bed):
i += 1
return i
@staticmethod
def two(instr: str) -> int:
return -1