-
Notifications
You must be signed in to change notification settings - Fork 4
/
day-16.py
184 lines (161 loc) · 9.64 KB
/
day-16.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
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
'''
2023 Advent of Code - Day 16 (https://adventofcode.com/2023/day/16)
Solution by Sav Bell (https://github.com/savbell)
Challenge: Solve every day in a single line of Python code.
See full progress at https://github.com/savbell/advent-of-code-one-liners
'''
# Input files not included in repo per copyright & Eric Wastl's request.
# https://www.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs/
# Replace with path to your personal input file if you would like to run the code.
from sys import setrecursionlimit
import itertools as it
input_file = '2023/day-16.txt'
# To match the format of input files for the Basilisk.
q = { 16: open(input_file).read().strip() }
######################### PART 1: MULTI-LINE SOLUTION #########################
grid = [list(x) for x in q[16].strip().split('\n')]
def follow_beam(beam_coord, direction, visited_coords):
if (beam_coord, direction) not in visited_coords:
visited_coords.add((beam_coord, direction))
while True:
if grid[beam_coord[1]][beam_coord[0]] == '.':
pass
elif grid[beam_coord[1]][beam_coord[0]] == '/':
if direction == 'right':
direction = 'up'
elif direction == 'left':
direction = 'down'
elif direction == 'up':
direction = 'right'
elif direction == 'down':
direction = 'left'
elif grid[beam_coord[1]][beam_coord[0]] == '\\':
if direction == 'right':
direction = 'down'
elif direction == 'left':
direction = 'up'
elif direction == 'up':
direction = 'left'
elif direction == 'down':
direction = 'right'
elif grid[beam_coord[1]][beam_coord[0]] == '|':
if direction == 'right':
visited_coords = follow_beam(beam_coord, 'up', visited_coords)
direction = 'down'
elif direction == 'left':
visited_coords = follow_beam(beam_coord, 'up', visited_coords)
direction = 'down'
elif direction == 'up':
pass
elif direction == 'down':
pass
elif grid[beam_coord[1]][beam_coord[0]] == '-':
if direction == 'right':
pass
elif direction == 'left':
pass
elif direction == 'up':
visited_coords = follow_beam(beam_coord, 'left', visited_coords)
direction = 'right'
elif direction == 'down':
visited_coords = follow_beam(beam_coord, 'left', visited_coords)
direction = 'right'
if direction == 'right':
beam_coord = (beam_coord[0]+1, beam_coord[1])
elif direction == 'left':
beam_coord = (beam_coord[0]-1, beam_coord[1])
elif direction == 'up':
beam_coord = (beam_coord[0], beam_coord[1]-1)
elif direction == 'down':
beam_coord = (beam_coord[0], beam_coord[1]+1)
if (beam_coord, direction) in visited_coords or beam_coord[0] < 0 or beam_coord[0] >= len(grid[0]) or beam_coord[1] < 0 or beam_coord[1] >= len(grid):
break
else:
visited_coords.add((beam_coord, direction))
return visited_coords
setrecursionlimit(30000)
visited_coords = follow_beam((0,0), 'right', set())
print('Day 16 Part 1:',len(set([x[0] for x in visited_coords])))
########################## PART 1: ONE-LINE SOLUTION ##########################
print('Day 16 Part 1:',(sys.setrecursionlimit(30000),g:=[list(x) for x in q[16].strip().split('\n')],(f:=lambda c,d,v: ((c,d) not in v and v.add((c,d)),(end:=0)) and [((g[c[1]][c[0]]=='/') and ((d==4) and (d:=1) or (d==3) and (d:=2) or (d==1) and (d:=4) or (d==2) and (d:=3)) or (g[c[1]][c[0]]=='\\') and ((d==4) and (d:=2) or (d==3) and (d:=1) or (d==1) and (d:=3) or (d==2) and (d:=4)) or (g[c[1]][c[0]]=='|') and ((d==4) and (v:=f(c,1,v)) and (d:=2) or (d==3) and (v:=f(c,1,v)) and (d:=2)) or (g[c[1]][c[0]]=='-') and ((d==2) and (d:=4) and (v:=f(c,3,v)) or (d==1) and (d:=4) and (v:=f(c,3,v))),(c:=(c[0]+1,c[1]) if d==4 else (c[0]-1,c[1]) if d==3 else (c[0],c[1]-1) if d==1 else (c[0],c[1]+1)),(((c,d) in v or c[0] < 0 or c[0] >= len(g[0]) or c[1] < 0 or c[1] >= len(g)) and (end:=1)),not (c[0] < 0 or c[0] >= len(g[0]) or c[1] < 0 or c[1] >= len(g)) and v.add((c,d))) for _ in it.takewhile(lambda _: not end,it.count())] and v)) and len(set([x[0] for x in f((0,0),4,set())])))
######################## PART 2: MULTI-LINE SOLUTION ##########################
grid = [list(x) for x in q[16].strip().split('\n')]
def follow_beam(beam_coord, direction, visited_coords):
if (beam_coord, direction) not in visited_coords:
visited_coords.add((beam_coord, direction))
while True:
if grid[beam_coord[1]][beam_coord[0]] == '.':
pass
elif grid[beam_coord[1]][beam_coord[0]] == '/':
if direction == 'right':
direction = 'up'
elif direction == 'left':
direction = 'down'
elif direction == 'up':
direction = 'right'
elif direction == 'down':
direction = 'left'
elif grid[beam_coord[1]][beam_coord[0]] == '\\':
if direction == 'right':
direction = 'down'
elif direction == 'left':
direction = 'up'
elif direction == 'up':
direction = 'left'
elif direction == 'down':
direction = 'right'
elif grid[beam_coord[1]][beam_coord[0]] == '|':
if direction == 'right':
visited_coords = follow_beam(beam_coord, 'up', visited_coords)
direction = 'down'
elif direction == 'left':
visited_coords = follow_beam(beam_coord, 'up', visited_coords)
direction = 'down'
elif direction == 'up':
pass
elif direction == 'down':
pass
elif grid[beam_coord[1]][beam_coord[0]] == '-':
if direction == 'right':
pass
elif direction == 'left':
pass
elif direction == 'up':
visited_coords = follow_beam(beam_coord, 'left', visited_coords)
direction = 'right'
elif direction == 'down':
visited_coords = follow_beam(beam_coord, 'left', visited_coords)
direction = 'right'
if direction == 'right':
beam_coord = (beam_coord[0]+1, beam_coord[1])
elif direction == 'left':
beam_coord = (beam_coord[0]-1, beam_coord[1])
elif direction == 'up':
beam_coord = (beam_coord[0], beam_coord[1]-1)
elif direction == 'down':
beam_coord = (beam_coord[0], beam_coord[1]+1)
if (beam_coord, direction) in visited_coords or beam_coord[0] < 0 or beam_coord[0] >= len(grid[0]) or beam_coord[1] < 0 or beam_coord[1] >= len(grid):
break
else:
visited_coords.add((beam_coord, direction))
return visited_coords
max_energized = 0
for x in range(len(grid[0])):
visited_coords = follow_beam((x,0), 'down', set())
energized_coords = set([x[0] for x in visited_coords])
max_energized = len(energized_coords) if len(energized_coords) > max_energized else max_energized
for x in range(len(grid[0])):
visited_coords = follow_beam((x,len(grid)-1), 'up', set())
energized_coords = set([x[0] for x in visited_coords])
max_energized = len(energized_coords) if len(energized_coords) > max_energized else max_energized
for y in range(len(grid)):
visited_coords = follow_beam((0,y), 'right', set())
energized_coords = set([x[0] for x in visited_coords])
max_energized = len(energized_coords) if len(energized_coords) > max_energized else max_energized
for y in range(len(grid)):
visited_coords = follow_beam((len(grid[0])-1,y), 'left', set())
energized_coords = set([x[0] for x in visited_coords])
max_energized = len(energized_coords) if len(energized_coords) > max_energized else max_energized
print('Day 16 Part 2:',max_energized)
########################## PART 2: ONE-LINE SOLUTION ##########################
print('Day 16 Part 2:',(g:= [list(x) for x in q[16].strip().split('\n')],(f:=lambda c,d,v: ((c,d) not in v and v.add((c,d)),(end:=0)) and [((g[c[1]][c[0]]=='/') and ((d==4) and (d:=1) or (d==3) and (d:=2) or (d==1) and (d:=4) or (d==2) and (d:=3)) or (g[c[1]][c[0]]=='\\') and ((d==4) and (d:=2) or (d==3) and (d:=1) or (d==1) and (d:=3) or (d==2) and (d:=4)) or (g[c[1]][c[0]]=='|') and ((d==4) and (v:=f(c,1,v)) and (d:=2) or (d==3) and (v:=f(c,1,v)) and (d:=2)) or (g[c[1]][c[0]]=='-') and ((d==2) and (d:=4) and (v:=f(c,3,v)) or (d==1) and (d:=4) and (v:=f(c,3,v))),(c:=(c[0]+1,c[1]) if d==4 else (c[0]-1,c[1]) if d==3 else (c[0],c[1]-1) if d==1 else (c[0],c[1]+1)),(((c,d) in v or c[0] < 0 or c[0] >= len(g[0]) or c[1] < 0 or c[1] >= len(g)) and (end:=1)),not (c[0] < 0 or c[0] >= len(g[0]) or c[1] < 0 or c[1] >= len(g)) and v.add((c,d))) for _ in it.takewhile(lambda _: not end,it.count())] and v),(m:=0),[(v:=f((x,0),2,set())) and (m:=len(set([x[0] for x in v])) if len(set([x[0] for x in v])) > m else m) for x in range(len(g[0]))] and [(v:=f((x,len(g)-1),1,set())) and (m:=len(set([x[0] for x in v])) if len(set([x[0] for x in v])) > m else m) for x in range(len(g[0]))] and [(v:=f((0,y),4,set())) and (m:=len(set([x[0] for x in v])) if len(set([x[0] for x in v])) > m else m) for y in range(len(g))] and [(v:=f((len(g[0])-1,y),3,set())) and (m:=len(set([x[0] for x in v])) if len(set([x[0] for x in v])) > m else m) for y in range(len(g))] and m) and m)