aoc24/7.jl

24 lines
606 B
Julia

const equations::Vector{Vector{Int}} = [ map(x -> parse(Int, x), split(line, Regex("[:\\s]+"))) for line in readlines("input/7") ]
partOne, partTwo = 0, 0
concat(a::Number, b::Number) = a * 10^ndigits(b) + b
for (target,nums) in Iterators.peel.(equations)
nums = collect(nums)
function testCase(funcs::Tuple, p = 1, sum = 0)::Bool
if p > length(nums)
return sum == target
end
return any(testCase(funcs, p+1, fn(sum, nums[p])) for fn in funcs)
end
if testCase((+, *))
global partOne += target
end
if testCase((+, *, concat))
global partTwo += target
end
end
@show partOne, partTwo