/
day23.jl
202 lines (141 loc) · 4.49 KB
/
day23.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
197
198
199
200
201
202
println("Day 23")
mutable struct Elf
id::Int
prop_loc::Union{Tuple{Int, Int}, Nothing}
end
Elves = Matrix{Union{Elf, Nothing}}
Directions = Vector{Tuple{Int, Int}}
function parse_input(filepath)
file = readlines(filepath)
(m, n) = (length(file), length(file[1]))
elves = Elves(nothing, m, n)
id = 1
for i in eachindex(file)
for j in eachindex(file[i])
if file[i][j] == '#'
elves[i, j] = Elf(id, nothing)
id += 1
end
end
end
directions = [(-1,0), (1,0), (0,-1), (0,1)]
return (elves, directions)
end
function neighbors(i::Int, j::Int)
return ((r, s) for r in i-1:i+1 for s in j-1:j+1 if (r, s) != (i, j))
end
function neighbors2(i::Int, j::Int)
return ((r, s) for r in i-2:i+2 for s in j-2:j+2 if (r, s) != (i, j))
end
function adjacent(i::Int, j::Int, dir::Tuple{Int, Int})
return ((i, j) .+ dir .+ a for a in ((0,0), reverse(dir), -1 .* reverse(dir)))
end
function resize_elves(elves::Elves, margin::Int)
(m, n) = size(elves)
left = reshape(elves[:, 1:2], :)
right = reshape(elves[:, n-1:n], :)
top = reshape(elves[1:2, :], :)
bottom = reshape(elves[m-1:m, :], :)
if any(!isnothing(e) for e in [left; right; top; bottom])
new_elves = Elves(nothing, m + 2 * margin, n + 2 * margin)
new_elves[1+margin:m+margin, 1+margin:n+margin] .= elves
elves = new_elves
end
return elves
end
function first_half!(elves::Elves, directions::Directions)
(m, n) = size(elves)
for i in 1:m, j in 1:n
if !isnothing(elves[i,j])
elf = elves[i,j]
elf.prop_loc = (i, j)
if all(isnothing(elves[r,s]) for (r, s) in neighbors(i, j))
elf.prop_loc = (i, j)
else
for dir in reverse(directions)
if all(isnothing(elves[r,s]) for (r, s) in adjacent(i, j, dir))
elf.prop_loc = (i, j) .+ dir
end
end
end
end
end
end
function second_half!(elves::Elves, directions::Directions)
(m, n) = size(elves)
for i in 1:m, j in 1:n
elf = elves[i,j]
if !isnothing(elf) && !isnothing(elf.prop_loc)
prop_loc = elf.prop_loc
if all(isnothing(elves[r,s]) || elves[r,s].prop_loc != prop_loc
for (r, s) in neighbors2(i, j))
elves[i, j] = nothing
elves[prop_loc[1], prop_loc[2]] = Elf(elf.id, nothing)
end
end
end
direction = popfirst!(directions)
push!(directions, direction)
end
function get_bounding_rectangle(elves::Elves)
(m, n) = size(elves)
locs = [(i, j) for i in 1:m for j in 1:n if !isnothing(elves[i, j])]
lo_i = minimum(i for (i, j) in locs)
hi_i = maximum(i for (i, j) in locs)
lo_j = minimum(j for (i, j) in locs)
hi_j = maximum(j for (i, j) in locs)
return (lo_i, hi_i, lo_j, hi_j)
end
function count_empty(elves::Elves)
(lo_i, hi_i, lo_j, hi_j) = get_bounding_rectangle(elves)
n_empty = 0
for i in lo_i:hi_i
for j in lo_j:hi_j
isnothing(elves[i,j]) ? n_empty += 1 : nothing
end
end
return n_empty
end
function is_terminated(elves::Elves)
(m, n) = size(elves)
for i in 1:m, j in 1:n
elf = elves[i,j]
if !isnothing(elf)
if any(!isnothing(elves[r,s]) for (r, s) in neighbors(i, j))
return false
end
end
end
return true
end
function iterate_rounds!(elves::Elves, directions::Directions, n_rounds::Int)
for round in 1:n_rounds
elves = resize_elves(elves, 10)
first_half!(elves, directions)
second_half!(elves, directions)
end
return (elves, directions)
end
function iterate_to_termination!(elves::Elves, directions::Directions)
terminated = false
round = 0
while !terminated
round += 1
elves = resize_elves(elves, 10)
first_half!(elves, directions)
second_half!(elves, directions)
terminated = is_terminated(elves)
end
return (elves, directions, round)
end
# part 1
filepath = "day23.txt"
(elves, directions) = parse_input(filepath)
(elves, directions) = iterate_rounds!(elves, directions, 10)
n_empty = count_empty(elves)
println("Part 1: ", n_empty)
# part 2
(elves, directions) = parse_input(filepath)
(elves, directions, round) = iterate_to_termination!(elves, directions)
println("Part 2: ", round + 1)
println()