Skip to content

Commit c7c27fd

Browse files
committed
Day 16
1 parent 5a6c0cb commit c7c27fd

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

day16/day16.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
def get_next_directions(tile, current_direction):
2+
if tile == '.':
3+
return [current_direction]
4+
5+
if tile == "/":
6+
if current_direction == "l":
7+
return ["d"]
8+
if current_direction == "r":
9+
return ["u"]
10+
if current_direction == "u":
11+
return ["r"]
12+
if current_direction == "d":
13+
return ["l"]
14+
15+
if tile == "\\":
16+
if current_direction == "l":
17+
return ["u"]
18+
if current_direction == "r":
19+
return ["d"]
20+
if current_direction == "u":
21+
return ["l"]
22+
if current_direction == "d":
23+
return ["r"]
24+
25+
if tile == "|":
26+
if current_direction in "ud":
27+
return [current_direction]
28+
return ["u", "d"]
29+
30+
if tile == "-":
31+
if current_direction in "lr":
32+
return [current_direction]
33+
return ["l", "r"]
34+
35+
raise Exception("Invalid tile!")
36+
37+
38+
def get_next_tile(i, j, dir, grid):
39+
if dir == "l":
40+
i_next, j_next = i, j-1
41+
if dir == "r":
42+
i_next, j_next = i, j+1
43+
if dir == "u":
44+
i_next, j_next = i-1, j
45+
if dir == "d":
46+
i_next, j_next = i+1, j
47+
48+
if i_next < 0 or i_next >= len(grid) or j_next < 0 or j_next >= len(grid[0]):
49+
return None, None
50+
return i_next, j_next
51+
52+
53+
def trace_light(grid, start_i, start_j, start_dir):
54+
r = len(grid)
55+
c = len(grid[0])
56+
57+
energized_count = 0
58+
energized = [[False for _ in range(c)] for _ in range(r)]
59+
dirs_applied_to_tile = [[[] for _ in range(c)] for _ in range(r)]
60+
61+
to_process = [(start_i, start_j, start_dir)]
62+
while len(to_process) > 0:
63+
i, j, dir = to_process.pop()
64+
if not energized[i][j]:
65+
energized[i][j] = True
66+
energized_count += 1
67+
if dir not in dirs_applied_to_tile[i][j]:
68+
dirs_applied_to_tile[i][j].append(dir)
69+
70+
next_dirs = get_next_directions(grid[i][j], dir)
71+
for next_dir in next_dirs:
72+
i_next, j_next = get_next_tile(i, j, next_dir, grid)
73+
if i_next != None and next_dir not in dirs_applied_to_tile[i_next][j_next]:
74+
to_process.append((i_next, j_next, next_dir))
75+
76+
return energized_count
77+
78+
79+
def part1(grid):
80+
energized_count = trace_light(grid, 0, 0, "r")
81+
print("Part 1 Result:", energized_count)
82+
83+
84+
def part2(grid):
85+
r = len(grid)
86+
c = len(grid[0])
87+
88+
max_top_row = max(trace_light(grid, 0, j, "d") for j in range(c))
89+
max_bottom_row = max(trace_light(grid, r-1, j, "u") for j in range(c))
90+
max_left_row = max(trace_light(grid, i, 0, "r") for i in range(r))
91+
max_right_row = max(trace_light(grid, i, c-1, "l") for i in range(r))
92+
93+
max_energized = max(max_top_row, max_bottom_row, max_left_row, max_right_row)
94+
print("Part 2 Result:", max_energized)
95+
96+
97+
def read_data_from_file(filename):
98+
with open(filename) as file:
99+
grid = [line.rstrip() for line in file]
100+
return grid
101+
102+
103+
grid = read_data_from_file("input.txt")
104+
# grid = read_data_from_file("testinput.txt")
105+
106+
part1(grid)
107+
part2(grid)

0 commit comments

Comments
 (0)