Last active
December 9, 2023 02:44
-
-
Save jechol/1acfae40f98ea6f967c83a79b488889b to your computer and use it in GitHub Desktop.
Advent of Code 2023, Day1 Part2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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