Day 8
This commit is contained in:
parent
27631b5043
commit
801bee8b3a
1 changed files with 42 additions and 0 deletions
42
8.jl
Normal file
42
8.jl
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
const grid = [ collect(line) for line in readlines("input/8") ]
|
||||||
|
const size = length(grid)
|
||||||
|
|
||||||
|
const Coords = Tuple{Int,Int}
|
||||||
|
|
||||||
|
ants::Dict{Char,Vector{Coords}} = Dict()
|
||||||
|
for (y,row) in enumerate(grid)
|
||||||
|
for (x,c) in enumerate(row)
|
||||||
|
if c != '.'
|
||||||
|
get!(ants, c, []) .= push!(ants[c], (x, y))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
isOnMap(n) = 1 <= n[1] <= size && 1 <= n[2] <= size
|
||||||
|
partOneGrid = deepcopy(grid)
|
||||||
|
|
||||||
|
partOneSet, partTwoSet = Set(), Set()
|
||||||
|
|
||||||
|
for (freq,coords) in ants
|
||||||
|
for (a,b) in Iterators.filter((x) -> x[1] != x[2], ( sort([a, b]) for a in coords, b in coords ))
|
||||||
|
local diff = a .- b
|
||||||
|
for n in (a .+ diff, b .- diff)
|
||||||
|
if isOnMap(n)
|
||||||
|
push!(partOneSet, n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for (n,fn) in ((a,.-), (b,.+))
|
||||||
|
while isOnMap(n)
|
||||||
|
n = fn(n, diff)
|
||||||
|
if isOnMap(n)
|
||||||
|
push!(partTwoSet, n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
const partOne, partTwo = length(partOneSet), length(partTwoSet)
|
||||||
|
|
||||||
|
@show partOne, partTwo
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue