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

AoC 2024 - Day 3

2024/day3.livemd

AoC 2024 - Day 3

Mix.install([
  {:kino, "~> 0.14.2"}
])

Setup

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

Part 1

program = input
|> Kino.Input.read()

Regex.scan(~r/mul\((\d{1,3},\d{1,3})\)/, program)
|> Enum.map(fn [_, values] ->
  values
  |> String.split(",", trim: true)
  |> Enum.map(&String.to_integer/1)
end)
|> Enum.map(fn [val1, val2] ->
  val1 * val2
end)
|> Enum.sum()

Part 2

program = input
|> Kino.Input.read()

Regex.scan(~r/mul\((\d{1,3},\d{1,3})\)|do\(\)|don't\(\)/, program)
|> Enum.reduce(%{processing: true, values: []}, fn(val, acc) ->
  case val do
    ["don't()"] ->
      %{processing: false, values: acc.values }
    ["do()"] ->
      %{processing: true, values: acc.values }
    [_, nums] ->
      case acc do
        %{processing: false, values: val} -> 
          %{processing: false, values: val}
        %{processing: true, values: val} ->
          %{processing: true, values: [nums | val]}  
      end
  end
end)
|> then(fn %{processing: _, values: values} ->
  values
end)
|> Enum.map(fn operation ->
  operation
  |> String.split(",", trim: true)
  |> Enum.map(&String.to_integer/1)
  |> Enum.reduce(1, fn (val, acc) -> acc * val end)
end)
|> Enum.sum()