Skip to content

Instantly share code, notes, and snippets.

@jechol
Last active December 9, 2023 02:44
Show Gist options
  • Save jechol/1acfae40f98ea6f967c83a79b488889b to your computer and use it in GitHub Desktop.
Save jechol/1acfae40f98ea6f967c83a79b488889b to your computer and use it in GitHub Desktop.
Advent of Code 2023, Day1 Part2
digits =
(~w(one two three four five six seven eight nine)
|> Enum.with_index(1) ) ++ ( ~w(1 2 3 4 5 6 7 8 9)
|> Enum.with_index(1) ) |> Enum.into(%{})
digits = ~w(1 2 3 4 5 6 7 8 9)
|> Enum.with_index(1) |> Enum.into(digits)
input
|> String.split()
|> Enum.map(fn line ->
matches =
Regex.scan(~r/(one|two|three|four|five|six|seven|eight|nine|[1-9])/, line)
[x, y] =
[matches |> List.first(), matches |> List.last()]
|> Enum.map(fn [s, s] -> digits[s] end)
x * 10 + y
end)
|> Enum.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment