-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
90 lines (78 loc) · 2.99 KB
/
day16.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
def get_energized_tile_count(grid, initial_light):
LIGHTS = set()
height = len(grid)
width = len(grid[0])
lights = {initial_light}
energized = {(initial_light[0], initial_light[1])}
while lights:
lights_tpl = tuple(sorted(lights))
if lights_tpl in LIGHTS:
break
LIGHTS.add(lights_tpl)
new_lights = set()
for light in lights:
j = light[0]
i = light[1]
direction = light[2]
going = [direction]
match grid[j][i]:
case '-':
if direction in ['U', 'D']:
going = ['L', 'R']
case '|':
if direction in ['L', 'R']:
going = ['U', 'D']
case '/':
match direction:
case 'L':
going = ['D']
case 'R':
going = ['U']
case 'U':
going = ['R']
case 'D':
going = ['L']
case '\\':
match direction:
case 'L':
going = ['U']
case 'R':
going = ['D']
case 'U':
going = ['L']
case 'D':
going = ['R']
for new_direction in going:
match new_direction:
case 'L':
if i > 0:
new_lights.add((j, i - 1, 'L'))
case 'R':
if i < width - 1:
new_lights.add((j, i + 1, 'R'))
case 'U':
if j > 0:
new_lights.add((j - 1, i, 'U'))
case 'D':
if j < height - 1:
new_lights.add((j + 1, i, 'D'))
for new_light in new_lights:
energized.add((new_light[0], new_light[1]))
lights = new_lights
return len(energized)
def part1(task_input):
grid = [list(row) for row in task_input.split('\n')]
return get_energized_tile_count(grid, (0, 0, 'R'))
def part2(task_input):
print("Part 2 runs in ~10 minutes... But it does run! Could parallelize/optimize for faster time, meh\nPlease wait...")
grid = [list(row) for row in task_input.split('\n')]
height = len(grid)
width = len(grid[0])
maximum = 0
for j in range(height):
maximum = max(maximum, get_energized_tile_count(grid, (j, 0, 'R')))
maximum = max(maximum, get_energized_tile_count(grid, (j, width - 1, 'L')))
for i in range(width):
maximum = max(maximum, get_energized_tile_count(grid, (0, i, 'D')))
maximum = max(maximum, get_energized_tile_count(grid, (height - 1, i, 'U')))
return maximum