aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day01/solver.jl
blob: faedc7bc122c1798183092343b414a35b9b3ab6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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