diff options
-rw-r--r-- | src/day03.exs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/day03.exs b/src/day03.exs new file mode 100644 index 0000000..6305e5b --- /dev/null +++ b/src/day03.exs @@ -0,0 +1,22 @@ +data = IO.read(:stdio, :eof) +|> String.trim() + +mul = fn [a, b] -> String.to_integer(a) * String.to_integer(b) end + +ans1 = Regex.scan(~r/mul\((\d+),(\d+)\)/, data) +|> Enum.map(&tl/1) +|> Enum.map(mul) +|> Enum.sum() + +IO.puts(ans1) + +ans2 = Regex.scan(~r/do\(\)|don\'t\(\)|mul\((\d+),(\d+)\)/, data) +|> Enum.reduce({true, 0}, fn + ["do()"], {_, acc} -> {true, acc} + ["don't()"], {_, acc} -> {false, acc} + _, {false, acc} -> {false, acc} + mulop, {true, acc} -> {true, acc + mul.(tl(mulop))} +end) +|> elem(1) + +IO.puts(ans2) |