-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.py
104 lines (72 loc) · 1.89 KB
/
18.py
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
95
96
97
98
99
100
101
102
103
104
from collections import namedtuple
from dataclasses import dataclass
from enum import Enum
from typing import List
# from .17 import Dirn
DEBUG = 1
# N = [line.strip() for line in open('./in/18.test.txt').readlines()]
N = [line.strip() for line in open('./in/18.txt').readlines()]
class Dirn(Enum):
UP = (-1, 0)
DOWN = (+1, 0)
LEFT = (0, -1)
RIGHT = (0, +1)
def __repr__(self):
return self.name
LOOKUP = {
'U': Dirn.UP,
'D': Dirn.DOWN,
'L': Dirn.LEFT,
'R': Dirn.RIGHT
}
@dataclass
class Step:
dirn: Dirn
distance: int
rgb: str
steps: List[Step] = []
for dirn, distance, rgb in map(str.split, N):
steps.append(Step(
LOOKUP[dirn],
int(distance),
rgb[1:-1]
))
def pp(visited: set):
pts = {(v[0], v[1]) for v in visited}
max_y = max(v[0] for v in pts)
max_x = max(v[1] for v in pts)
for i in range(max_y + 1):
print("".join('.#'[(i, j) in pts] for j in range(max_x + 1)))
def part_1():
curr = (0, 0, None)
visited = {curr}
for step in steps:
dy, dx = step.dirn.value
for _ in range(step.distance):
i, j, _rgb = curr
curr = (i + dy, j + dx, step.rgb)
if DEBUG and curr in visited:
print(f'Already seen {curr}')
assert curr == (0, 0)
visited.add(curr)
# don't worry about rgbs yet
pts = {(v[0], v[1]) for v in visited}
curr = (1, 1)
stack = [curr]
while stack:
curr = stack.pop()
pts.add(curr)
i, j = curr
for dirn in Dirn:
dy, dx = dirn.value
if (ii := i + dy, jj := j + dx) not in pts:
stack.append((ii, jj))
return len(pts)
def part_2():
# God damnit
pass
if __name__ == '__main__':
print('--- Part 1 ---')
print(part_1())
print('\n--- Part 2 ---')
print(part_2())