aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day01
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-18 05:23:56 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-18 05:23:56 +0100
commit36b850a0a28a7e57799d5d201d9043432d1815ac (patch)
treefaffe9e3b029f9515fa982e8cb8b06b5a737d784 /AoC2023/day01
parent9bccc0697cedbed1b0c888e26ba23daf53c29b56 (diff)
downloadscratch-36b850a0a28a7e57799d5d201d9043432d1815ac.tar.gz
scratch-36b850a0a28a7e57799d5d201d9043432d1815ac.tar.bz2
scratch-36b850a0a28a7e57799d5d201d9043432d1815ac.zip
day01 julia
Diffstat (limited to 'AoC2023/day01')
-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