-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.py
147 lines (135 loc) · 5.01 KB
/
day22.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Please don't judge the code - I was trying to get to leaderboard,but failed due to stupid off by one error
# Part2 is therefore input specific and not general
start = None
board = {}
dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]
with open('inputs/day22.txt') as data:
# ------------------------------------- INPUT PARSING
parts = data.read().split('\n\n')
for y, line in enumerate(parts[0].split('\n'), start=1):
for x, state in enumerate(line, start=1):
if not state.isspace():
board[(x, y)] = state
if start is None:
start = (x, y)
path = []
last = ''
for c in parts[1]:
if c in 'LR':
if last != '':
path.append(int(last))
last = ''
path.append(c)
else:
last += c
if last != '':
path.append(int(last))
# ------------------------------------- PART 1
dx = 1
dy = 0
crr = ''
(x, y) = start
for step in path:
if isinstance(step, int):
for _ in range(step):
nx, ny = x+dx, y+dy
cell = board.get((nx, ny))
if cell is None:
nnx, nny = x, y
while (nnx, nny) in board:
nx, ny = nnx, nny
nnx -= dx
nny -= dy
cell = board[(nx, ny)]
if cell == '#':
break # Hit a wall
x, y = nx, ny
elif step == 'R':
dy, dx = dx, -dy
elif step == 'L':
dx, dy = dy, -dx
print('Part1:', 1000 * y + 4 * x + dirs.index((dx,dy)))
# ------------------------------------- PART 2
dx = 1
dy = 0
crr = ''
(x, y) = start
sequence = []
for step in path:
if isinstance(step, int):
for _ in range(step):
bx, by = x, y
bdx, bdy = dx, dy
nx, ny = x+dx, y+dy
cell = board.get((nx, ny))
wrap = False
if cell is None:
wrap = True
if dy == -1 and 51 <= x <= 100 and y == 1: # 1-UP
y = 151 + x - 51
x = 1
dx, dy = 1, 0
elif dx == -1 and x == 1 and 151 <= y <= 200: #1-LEFT
x = y - 100
y = 1
dx, dy = 0, 1
elif dx == -1 and x == 51 and 1 <= y <= 50: #2-left
x = 1
y = 150 - y + 1
dx, dy = 1, 0
elif dx == -1 and x == 1 and 101 <= y <= 150: #2-left
x = 51
y = abs(y - 150) + 1
dx, dy = 1,0
elif dy == -1 and y == 1 and 101 <= x <= 150: #3-up
y = 200
x = x - 100
elif dy == 1 and y == 200 and 1 <= x <= 50: #3-down
y = 1
x = x + 100
elif dx == 1 and x == 150 and 1 <= y <= 50: #4-right
x = 100
y = 150 - y + 1
dx, dy = -1, 0
elif dx == 1 and x == 100 and 101 <= y <= 150: #4-right
x = 150
y = abs(y - 150) + 1
dx, dy = -1, 0
elif dy == 1 and y == 50 and 101 <= x <= 150: #5-down
y = x - 50
x = 100
dx, dy = -1, 0
elif dx == 1 and x == 100 and 51 <= y <= 100: #5-right
x = y + 50
y = 50
dx, dy = 0, -1
elif dx == -1 and x == 51 and 51 <= y <= 100: #6-left
x = y - 50
y = 101
dx, dy = 0, 1
elif dy == -1 and y == 101 and 1 <= x <= 50: #6-up
y = x + 50
x = 51
dx, dy = 1, 0
elif dy == 1 and y == 150 and 51 <= x <= 100: #7-down
y = x + 100
x = 50
dx, dy = -1, 0
elif dx == 1 and x == 50 and 151 <= y <= 200: #7-right
x = y - 100
y = 150
dx, dy = 0, -1
if cell == '#':
break # Hit a wall
if wrap and board.get((x, y)) == '#': # revert
x, y = bx, by
dx, dy = bdx, bdy
break
if not wrap:
x, y = nx, ny
sequence.append((x,y))
elif step == 'R':
dy, dx = dx, -dy
elif step == 'L':
dx, dy = dy, -dx
print('Part2:', 1000 * y + 4 * x + dirs.index((dx,dy)))