summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2024-12-03 19:12:23 +0000
committerAlexandre Jesus <adbjesus@gmail.com>2024-12-03 19:12:23 +0000
commit973eee6810311f77c2907976fe40d68a393347ab (patch)
tree6aa629b2315582e64c60274cbba14edb025b2f08
parent6da10b268dd639594cb5a8cc6e9bbb86ce4beb5e (diff)
downloadaoc2024-973eee6810311f77c2907976fe40d68a393347ab.tar.gz
aoc2024-973eee6810311f77c2907976fe40d68a393347ab.zip
Day 3
-rw-r--r--src/day03.exs22
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)