Day 7: Bridge Repair
We need to try combinations of operators to satisfy an equation. I wanted to use function pointers to solve this, but this turns out to be really slow in Julia.
file:src/Day07.jl
module Day07
read_input(io::IO) =
parse(Int, a), parse.(Int, split(b)))
[(in split.(readlines(io), ':')]
for (a, b)
function good_equation_1(x::Int, n::AbstractVector{Int})
length(n) == 1 && return x == n[1]
% n[end] == 0 &&
x good_equation_1(x ÷ n[end], n[1:end-1]) && return true
return good_equation_1(x - n[end], n[1:end-1])
end
function good_equation_2(x::Int, n::AbstractVector{Int})
length(n) == 1 && return x == n[1]
% n[end] == 0 &&
x good_equation_2(x ÷ n[end], @view n[1:end-1]) && return true
= 10^ndigits(n[end])
d - n[end]) % d == 0 &&
(x good_equation_2((x - n[end]) ÷ d, @view n[1:end-1]) && return true
return good_equation_2(x - n[end], @view n[1:end-1])
end
function main(io::IO)
= read_input(io)
input = sum(r for (r, n) in input if good_equation_1(r, n))
part1 = sum(r for (r, n) in input if good_equation_2(r, n))
part2 return part1, part2
end
end
file:test/Day07Spec.jl
# add tests