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