From 36b850a0a28a7e57799d5d201d9043432d1815ac Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 18 Dec 2023 05:23:56 +0100 Subject: day01 julia --- AoC2023/day01/solver.jl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 AoC2023/day01/solver.jl (limited to 'AoC2023/day01/solver.jl') diff --git a/AoC2023/day01/solver.jl b/AoC2023/day01/solver.jl new file mode 100644 index 0000000..faedc7b --- /dev/null +++ b/AoC2023/day01/solver.jl @@ -0,0 +1,25 @@ +using Test + +function captures(pat, str) + [m[1] for m in eachmatch(pat, str)] +end + +function f(x) + if x in n + string(findfirst(isequal(x), n)) + else + x + end +end + +join_first_last(digits)=parse(Int, digits[1] * digits[end]) + +n = split("one two three four five six seven eight nine") +p = Regex("(?=(" * join(n, "|") * "|\\d))") + +solve(pat, file) = map(x-> f.(captures(pat, x)) |> join_first_last, eachline(file)) |> sum + +@testset "solutions" begin + @test solve(r"(\d)","input") == 55172 + @test solve(p, "input") == 54925 +end -- cgit v1.2.3