From 98276d08549b8b9782dc06f6dbf8c2c03330a8fa78ef67522f7b4ab277e86339 Mon Sep 17 00:00:00 2001 From: nazrin Date: Sun, 8 Dec 2024 21:13:18 +0000 Subject: [PATCH] Day 7 --- 7.jl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 7.jl diff --git a/7.jl b/7.jl new file mode 100644 index 0000000..ad76aa5 --- /dev/null +++ b/7.jl @@ -0,0 +1,24 @@ +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 +