From 40f94b223b043d2ac2b50d5b9636189fa4c25b4d Mon Sep 17 00:00:00 2001 From: Frederik Schnack Date: Tue, 3 Dec 2024 10:58:52 +0100 Subject: [PATCH] Add solution for day 3 --- inputs | 2 +- solutions/2024/src/AdventOfCode24.jl | 2 +- solutions/2024/src/day03.jl | 39 ++++++++++++++++++++++++++++ solutions/2024/test/runtests.jl | 4 +++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 solutions/2024/src/day03.jl diff --git a/inputs b/inputs index 2c9945b..a47e3a1 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit 2c9945b0a4a8da2d0b0fe0e947dc8c270e6a0340 +Subproject commit a47e3a10f9dad4314fe61af40278c0567b07c165 diff --git a/solutions/2024/src/AdventOfCode24.jl b/solutions/2024/src/AdventOfCode24.jl index a856780..b04361e 100644 --- a/solutions/2024/src/AdventOfCode24.jl +++ b/solutions/2024/src/AdventOfCode24.jl @@ -8,7 +8,7 @@ module AdventOfCode24 readInput(day::Int) = AdventOfCode.readInput(day, year) export readInput - solvedDays = [1] + solvedDays = [1, 2, 3] # Include the source files: for day in solvedDays diff --git a/solutions/2024/src/day03.jl b/solutions/2024/src/day03.jl new file mode 100644 index 0000000..fd90289 --- /dev/null +++ b/solutions/2024/src/day03.jl @@ -0,0 +1,39 @@ +module Day03 + using ..AdventOfCode24 + + """ + day03() + + Solves the two puzzles of day 03. + """ + + function day03(input::String = readInput(03)) + + s0 = mul(input) + s1 = 0 + + for l in split(input, "do()") + + i = findfirst("don't()", l) + !isnothing(i) && (l = first(l, first(i))) + + s1 += mul(l) + end + + return [s0, s1] + end + + function mul(ops::AbstractString) + r = r"mul\((\d+),(\d+)\)" + s = 0 + for m in eachmatch(r, ops) + a = parse(Int, m.captures[1]) + b = parse(Int, m.captures[2]) + s += a*b + end + + return s + end + +end + diff --git a/solutions/2024/test/runtests.jl b/solutions/2024/test/runtests.jl index 4de0c80..16aecb7 100644 --- a/solutions/2024/test/runtests.jl +++ b/solutions/2024/test/runtests.jl @@ -8,3 +8,7 @@ end @test AdventOfCode24.Day02.day02() == [356 , 413] end +@testset "Day 03" begin + @test AdventOfCode24.Day03.day03() == [162813399 , 53783319] +end +