-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_16.py
132 lines (116 loc) · 3.96 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
import time
def part1():
t = time.time()
frontier = [(0, 0, "r")]
energized = [[0 for j in i] for i in inputVals]
visited = set()
while len(frontier) > 0:
row, col, direction = frontier.pop(0)
if (row, col, direction) in visited:
continue
visited.add((row, col, direction))
energized[row][col] = 1
if inputVals[row][col] == ".":
nextPlace = getNextEmpty(row, col, direction)
if nextPlace and nextPlace not in visited:
frontier.append(nextPlace)
elif inputVals[row][col] in "-|":
for i in getNextSplitter(row, col, direction):
if i not in visited:
frontier.append(i)
else:
nextPlace = getNextMirror(row, col, direction)
if nextPlace and nextPlace not in visited:
frontier.append(nextPlace)
total = 0
for i in energized:
for j in i:
if j != 0:
total += 1
print("Part 1:", total)
print(time.time() - t)
def part2():
maxVal = 0
for i in range(len(inputVals)):
maxVal = max(maxVal, getEnergized((i, 0, "r")))
maxVal = max(maxVal, getEnergized((i, len(inputVals[0]) - 1, "l")))
for i in range(len(inputVals[0])):
maxVal = max(maxVal, getEnergized((0, i, "d")))
maxVal = max(maxVal, getEnergized((len(inputVals) - 1, i, "u")))
print("Part 2:", maxVal)
def getEnergized(start):
frontier = [start]
energized = [[0 for j in i] for i in inputVals]
visited = set()
while len(frontier) > 0:
row, col, direction = frontier.pop(0)
if (row, col, direction) in visited:
continue
visited.add((row, col, direction))
energized[row][col] = 1
if inputVals[row][col] == ".":
nextPlace = getNextEmpty(row, col, direction)
if nextPlace and nextPlace not in visited:
frontier.append(nextPlace)
elif inputVals[row][col] in "-|":
for i in getNextSplitter(row, col, direction):
if i not in visited:
frontier.append(i)
else:
nextPlace = getNextMirror(row, col, direction)
if nextPlace and nextPlace not in visited:
frontier.append(nextPlace)
total = 0
for i in energized:
for j in i:
if j != 0:
total += 1
return total
def getNextEmpty(row, col, direction):
if direction == "l":
if col > 0:
return (row, col - 1, direction)
else:
return None
if direction == "r":
if col < len(inputVals[0]) - 1:
return (row, col + 1, direction)
else:
return None
if direction == "u":
if row > 0:
return (row - 1, col, direction)
else:
return None
if direction == "d":
if row < len(inputVals) - 1:
return (row + 1, col, direction)
else:
return None
def getNextSplitter(row, col, direction):
if (inputVals[row][col] == "|" and direction in "ud") or (inputVals[row][col] == "-" and direction in "lr"):
temp = getNextEmpty(row, col, direction)
if temp == None:
return []
else:
return [temp]
if inputVals[row][col] == "|":
return [(row, col, "u"), (row, col, "d")]
else:
return [(row, col, "l"), (row, col, "r")]
def getNextMirror(row, col, direction):
new = {"u":"l", "r":"d", "d":"r", "l":"u"}
if inputVals[row][col] == "\\":
newDir = new[direction]
return getNextEmpty(row, col, newDir)
new = {"u":"r", "r":"u", "d":"l", "l":"d"}
if inputVals[row][col] == "/":
newDir = new[direction]
return getNextEmpty(row, col, newDir)
inputVals = []
f = open("Day_16_input.txt")
for line in f:
line = line.replace("\n", "")
inputVals.append([i for i in line])
part1()
part2()