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)