-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_01.ex
38 lines (33 loc) · 961 Bytes
/
day_01.ex
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
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule AdventOfCode.Y2023.Day01 do
@moduledoc """
--- Day 1: Trebuchet?! ---
Problem Link: https://adventofcode.com/2023/day/1
Difficulty: xs
Tags: regex rust
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2023, 1)
def run(input \\ input()) do
input
|> Transformers.lines()
|> then(
&{solve(&1, ~r"\d"), solve(&1, ~r/(?=(one|two|three|four|five|six|seven|eight|nine|\d))/)}
)
end
defp solve(input, regex) do
for line <- input, reduce: 0 do
acc ->
regex
|> Regex.scan(line)
|> Enum.map(&List.last/1)
|> then(&(10 * tr(List.first(&1)) + tr(List.last(&1))))
|> Kernel.+(acc)
end
end
~w/one two three four five six seven eight nine/
|> Enum.with_index(1)
|> Enum.each(fn {word, index} ->
defp tr(unquote(word)), do: unquote(index)
end)
defp tr(numeric), do: String.to_integer(numeric)
end