Day 8
file:test/Day08Spec.jl
# add tests
file:src/Day08.jl
module Day08
using .Iterators: filter, flatten, map, takewhile, countfrom
function group(a::A; skip=()) where {A}
= Dict{valtype(A),Vector{keytype(A)}}()
g for (k, v) ∈ pairs(a)
∈ skip && continue
v if v ∉ keys(g)
= []
g[v] end
push!(g[v], k)
end
return g
end
all_pairs(x) = ((i, j) for i in x for j in x if (i != j))
function antinodes_1(inbounds)
f(a::CartesianIndex{2}, b::CartesianIndex{2}) =
2a - b, 2b - a)
(
f(x::Vector{CartesianIndex{2}}) =
filter(inbounds, flatten(map(splat(f), all_pairs(x))))
return f
end
function antinodes_2(inbounds)
function f(a::CartesianIndex{2}, b::CartesianIndex{2})
= b - a
d = (a - i*d for i in countfrom(0))
one_way = (b + i*d for i in countfrom(0))
other_way return flatten((takewhile(inbounds, one_way),
takewhile(inbounds, other_way)))
end
f(x::Vector{CartesianIndex{2}}) =
flatten(map(splat(f), all_pairs(x)))
return f
end
function main(io::IO)
= io |> readlines |> stack
input = group(input, skip=('.',))
g inbounds(i) = checkbounds(Bool, input, i)
= length(
part1 values(g) .|> antinodes_1(inbounds) |> flatten |> unique)
= length(
part2 values(g) .|> antinodes_2(inbounds) |> flatten |> unique)
return part1, part2
end
end