aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day01/solver.jl
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2023/day01/solver.jl')
-rw-r--r--AoC2023/day01/solver.jl25
1 files changed, 25 insertions, 0 deletions
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