-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day17.jl
196 lines (162 loc) · 4.54 KB
/
Day17.jl
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
function getInputString()
open("day17input.txt") do f
return strip(read(f, String))
end
end
function getMinMaxXY(lines)
xs = []
ys = []
for line in lines
fixedCoord, coordRange = split(line)
fixedValue = parse(Int, fixedCoord[3:end-1])
if fixedCoord[1] == 'x'
push!(xs, fixedValue)
elseif fixedCoord[1] == 'y'
push!(ys, fixedValue)
end
rangeStart, rangeEnd = map(s -> parse(Int, s), split(coordRange[3:end], ".."))
if coordRange[1] == 'x'
push!(xs, rangeStart:rangeEnd...)
elseif coordRange[1] == 'y'
push!(ys, rangeStart:rangeEnd...)
end
end
return (min(xs...), min(ys...), max(xs...), max(ys...))
end
function parseInput(s)
lines = filter(!isempty, split(s, '\n'))
xMin, yMin, xMax, yMax = getMinMaxXY(lines)
grid = Array{Char}(undef, xMax+1, yMax)
fill!(grid, '.')
for line in lines
fixedCoord, coordRange = split(line)
fixedValue = parse(Int, fixedCoord[3:end-1])
rangeStart, rangeEnd = map(s -> parse(Int, s), split(coordRange[3:end], ".."))
if fixedCoord[1] == 'x'
grid[fixedValue, rangeStart:rangeEnd] .= '#'
elseif fixedCoord[1] == 'y'
grid[rangeStart:rangeEnd, fixedValue] .= '#'
end
end
return grid, yMin, yMax
end
function printGrid(grid)
println("Grid:")
minX = Inf
for y in 1:size(grid, 2)
for x in 1:size(grid, 1)
if x < minX && grid[x,y] != '.'
minX = x
end
end
end
for y in 1:size(grid, 2)
for x in minX:size(grid, 1)
print(grid[x,y])
end
print("\n")
end
end
function isPermeable(c::Char)
c != '#' && c != '~'
end
function floodRow!(loc, grid)
grid[loc...] = '~'
left = loc .+ (-1, 0)
while grid[left...] == '.' || grid[left...] == '|'
grid[left...] = '~'
left = left .+ (-1, 0)
end
right = loc .+ (1, 0)
while grid[right...] == '.' || grid[right...] == '|'
grid[right...] = '~'
right = right .+ (1, 0)
end
return loc .+ (0, -1)
end
function findPourOvers(loc, grid)
pourOvers = []
grid[loc...] = '|'
left = loc .+ (-1, 0)
while grid[left...] == '.' || grid[left...] == '|'
grid[left...] = '|'
if isPermeable(grid[(left .+ (0, 1))...])
push!(pourOvers, left)
break
end
left = left .+ (-1, 0)
end
right = loc .+ (1, 0)
while grid[right...] == '.' || grid[right...] == '|'
grid[right...] = '|'
if isPermeable(grid[(right .+ (0, 1))...])
push!(pourOvers, right)
break
end
right = right .+ (1, 0)
end
return pourOvers
end
function flowToPourOver(loc, grid)
if grid[loc...] == '~'
return []
end
# Mark this square as hit with water
grid[loc...] = '|'
# Flow down as far as possible
down = loc
while isPermeable(grid[down...])
grid[down...] = '|'
loc = down
down = down .+ (0, 1)
if down[2] > size(grid, 2)
return []
end
end
# we hit something, so find downspouts where we pour over
pourOvers = findPourOvers(loc, grid)
if length(pourOvers) > 0
return pourOvers
end
# We didn't find any down spouts, so we're in a well. Flood it until we find downspouts
while length(pourOvers) == 0
loc = floodRow!(loc, grid)
pourOvers = findPourOvers(loc, grid)
end
return pourOvers
end
function fillGrid!(grid)
# Keep track of the places where water is traveling
q = deque(Tuple{Int,Int})
processed = Set()
push!(q, (500, 1))
while length(q) > 0
square = popfirst!(q)
if square in processed
continue
end
toFlow = flowToPourOver(square, grid)
if length(toFlow) > 0
push!(q, toFlow...)
end
push!(processed, square)
n = length(q)
println("$n pourovers to process")
end
end
function countWetSquares(grid, minY, maxY)
length(filter(c -> c == '|' || c == '~', grid[:, minY:maxY]))
end
function countFilledSquares(grid, minY, maxY)
length(filter(c -> c == '~', grid[:, minY:maxY]))
end
function solvePartA()
grid, yMin, yMax = parseInput(getInputString())
fillGrid!(grid)
countWetSquares(grid, yMin, yMax)
end
function solvePartB()
grid, yMin, yMax = parseInput(getInputString())
fillGrid!(grid)
countFilledSquares(grid, yMin, yMax)
end