forked from lxylxy123456/AdventOfCode2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths.py
More file actions
230 lines (217 loc) · 5.15 KB
/
s.py
File metadata and controls
230 lines (217 loc) · 5.15 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Youtube: https://youtu.be/Mo8lxhzlCsI
import argparse, math, sys, re, functools, operator, itertools, heapq
from collections import defaultdict, Counter, deque
#sys.setrecursionlimit(100000000)
#A = list(map(int, input().split()))
#T = int(input())
def read_lines(f):
while True:
line = f.readline()
if not line:
break
assert line[-1] == '\n'
yield line[:-1]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-1', '--one', action='store_true', help='Only part 1')
parser.add_argument('-2', '--two', action='store_true', help='Only part 2')
parser.add_argument('input_file', nargs='?')
args = parser.parse_args()
if args.input_file is not None:
f = open(args.input_file)
else:
f = sys.stdin
lines = list(read_lines(f))
if not args.two:
print(part_1(lines))
if not args.one:
print(part_2(lines))
C2V = {
'>': (0, 1),
'v': (1, 0),
'<': (0, -1),
'^': (-1, 0),
}
def read_input1(lines):
m = []
l = iter(lines)
while True:
line = next(l)
if not line:
break
m.append(list(line))
a = []
for i in l:
for j in i:
a.append(C2V[j])
X = len(m)
Y = len(m[0])
rx = None
ry = None
for x in range(X):
for y in range(Y):
if m[x][y] == '@':
assert rx is None and ry is None
rx = x
ry = y
assert rx is not None and ry is not None
return m, a, X, Y, rx, ry
def part_1(lines):
s = 0
m, a, X, Y, rx, ry = read_input1(lines)
for dx, dy in a:
ox, oy = rx + dx, ry + dy
assert ox in range(X) and oy in range(Y)
while m[ox][oy] == 'O':
ox += dx
oy += dy
assert ox in range(X) and oy in range(Y)
if m[ox][oy] == '.':
# Move.
m[ox][oy] = 'O'
m[rx][ry] = '.'
rx += dx
ry += dy
m[rx][ry] = '@'
else:
# No move.
assert m[ox][oy] == '#'
for x in range(X):
for y in range(Y):
if m[x][y] == 'O':
s += 100 * x + y
#for i in m:
# print(*i, sep='')
return s
def read_input2(lines):
m = []
l = iter(lines)
while True:
line = next(l)
if not line:
break
f = {'@': '@.', '#': '##', 'O': '[]', '.': '..'}.__getitem__
m.append(list(itertools.chain.from_iterable(map(f, line))))
a = []
for i in l:
for j in i:
a.append(C2V[j])
X = len(m)
Y = len(m[0])
rx = None
ry = None
for x in range(X):
for y in range(Y):
if m[x][y] == '@':
assert rx is None and ry is None
rx = x
ry = y
assert rx is not None and ry is not None
return m, a, X, Y, rx, ry
def push2_buggy(m, X, Y, rx, ry, dx, dy):
# Returns (if moving, list of boxes pushed using GPS coordinates).
# List of boxes are sorted where furthest boxes first pushed appear first.
if m[rx + dx][ry + dy] == '#':
return False, []
elif m[rx + dx][ry + dy] == '.':
return True, []
elif m[rx + dx][ry + dy] == '[':
ox, oy = rx + dx, ry + dy
elif m[rx + dx][ry + dy] == ']':
ox, oy = rx + dx, ry + dy - 1
else:
raise ValueError
# Continuation of previous elif's.
assert m[rx + dx][ry + dy] in '[]'
if dx:
assert not dy
m1, s1 = push2(m, X, Y, ox, oy, dx, dy)
m2, s2 = push2(m, X, Y, ox, oy + 1, dx, dy)
if not m1 or not m2:
return False, []
return True, s1 + s2 + [(ox, oy)]
else:
assert dy
m, s = push2(m, X, Y, rx + dx, ry + dy * 2, dx, dy)
if not m:
return False, []
return True, s + [(ox, oy)]
def push2(m, X, Y, rx, ry, dx, dy):
'''
Compared my solution with a random solution on Reddit to find my error.
https://www.reddit.com/r/adventofcode/comments/1hele8m/comment/m24p2od/
https://gitlab.com/davidsharick/advent-of-code-2024/
-/blob/main/day15/day15.py
Minimal input that triggers the error:
######
#....#
#.OO.#
#.OO@#
#.OO.#
#....#
######
<>vv<<<^
The bug is that push2() does not return boxes layer by layer.
Also it does not deduplicate the list.
'''
# Returns (if moving, list of boxes pushed using GPS coordinates).
# List of boxes are not sorted. The caller should sort them.
if m[rx + dx][ry + dy] == '#':
return False, set()
elif m[rx + dx][ry + dy] == '.':
return True, set()
elif m[rx + dx][ry + dy] == '[':
ox, oy = rx + dx, ry + dy
elif m[rx + dx][ry + dy] == ']':
ox, oy = rx + dx, ry + dy - 1
else:
raise ValueError
# Continuation of previous elif's.
assert m[rx + dx][ry + dy] in '[]'
if dx:
assert not dy
s = {(ox, oy)}
m1, s1 = push2(m, X, Y, ox, oy, dx, dy)
m2, s2 = push2(m, X, Y, ox, oy + 1, dx, dy)
if not m1 or not m2:
return False, []
s.update(s1)
s.update(s2)
return True, s
else:
assert dy
m, s = push2(m, X, Y, rx + dx, ry + dy * 2, dx, dy)
if not m:
return False, []
s.add((ox, oy))
return True, s
def part_2(lines):
s = 0
m, a, X, Y, rx, ry = read_input2(lines)
for dx, dy in a:
move, boxes = push2(m, X, Y, rx, ry, dx, dy)
# Lines below are added after finding the bug.
boxes = sorted(boxes, key=lambda x: (-1) * (x[0] * dx + x[1] * dy))
# Lines above are added after finding the bug.
if move:
for ox, oy in boxes:
m[ox][oy] = '.'
m[ox][oy + 1] = '.'
m[ox + dx][oy + dy] = '['
m[ox + dx][oy + dy + 1] = ']'
m[rx][ry] = '.'
rx += dx
ry += dy
m[rx][ry] = '@'
if 0:
for i in m:
print(*i, sep='')
print('-')
#input()
for x in range(X):
for y in range(Y):
if m[x][y] == '[':
s += 100 * x + y
return s
if __name__ == '__main__':
main()