Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 3: Mull It Over

day3.livemd

Day 3: Mull It Over

Mix.install([:kino])

Section

input = Kino.Input.textarea("input")
input = Kino.Input.read(input)

Part 1

Regex.scan(~r/mul\((\d{1,3}),(\d{1,3})\)/, input, capture: :all_but_first)
|> Enum.map(fn([left, right]) -> String.to_integer(left) * String.to_integer(right) end)
|> Enum.sum()

Part 2

Regex.scan(~r/do\(\)|don't\(\)|mul\((\d{1,3}),(\d{1,3})\)/U, input)
|> Enum.map(fn 
  [_op, left, right] -> String.to_integer(left) * String.to_integer(right)
  ["don't()"] -> :dont
  ["do()"] -> :do
end)
|> Enum.reduce({:do, 0}, fn 
    (:do, {_, total}) -> {:do, total}
    (:dont, {_, total}) -> {:dont, total}
    (num, {:do, total}) when is_integer(num) -> {:do, total + num}
    (num, {:dont, total}) when is_integer(num) -> {:dont, total}
end)
|> elem(1)