From 973eee6810311f77c2907976fe40d68a393347ab Mon Sep 17 00:00:00 2001 From: Alexandre Jesus Date: Tue, 3 Dec 2024 19:12:23 +0000 Subject: Day 3 --- src/day03.exs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/day03.exs (limited to 'src/day03.exs') 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) -- cgit v1.2.3