-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.jl
47 lines (42 loc) · 1.25 KB
/
day14.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
module Day14
using AdventOfCode2017
function day14(input::String = readInput(joinpath(@__DIR__, "..", "data", "day14.txt")))
grid = build_grid(input)
p1 = grid |> count
p2 = part2!(grid)
return [p1, p2]
end
function build_grid(input::String)
input = rstrip(input)
grid = BitArray(undef, 128, 128)
for i = 0:127
s = input * "-$i"
lengths = vcat(Vector{UInt8}(rstrip(s)), Vector{UInt8}([17, 31, 73, 47, 23]))
hash = AdventOfCode2017.Day10.part2(lengths)
bitstr = join(bitstring.(hex2bytes(hash)))
grid[i+1, :] = [x == '1' for x in bitstr]
end
return grid
end
function part2!(grid::BitArray)
ngroups = 0
while true
f = findfirst(x -> x == true, grid)
f === nothing && break
current = [f]
ngroups += 1
while length(current) > 0
c = popfirst!(current)
grid[c] = false
for (x, y) in ((1, 0), (-1, 0), (0, 1), (0, -1))
ci, cj = c[1] + x, c[2] + y
!(ci >= 1 && cj >= 1 && all((ci, cj) .<= size(grid))) && continue
if grid[ci, cj]
push!(current, CartesianIndex(ci, cj))
end
end
end
end
return ngroups
end
end # module