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

Advent of Code - 2024 Day 3

2024/day-03.livemd

Advent of Code - 2024 Day 3

Mix.install([
  {:kino_aoc, "~> 0.1.7"}
])

Section

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2024", "3", System.fetch_env!("LB_AOC_SESSION"))

Part 1

~r/
  mul\(  # prefixed by "mul("
  (\d{1,3})   # 3-digit number      (captured)
  ,           # matches a comma     (not captured)
  (\d{1,3})   # 3-digit number      (captured)
  \)      # suffixed by ")"
/x
|> Regex.scan(puzzle_input, capture: :all_but_first)
|> List.flatten()
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(2)
|> Enum.map(fn [a, b] -> a * b end)
|> Enum.sum()

Part 2

~r/
  (do\(\))     # matches "do()" (captured)
  |            # or
  (don't\(\))  # matches "don't()" (captured)
  |            # or
  mul\((\d{1,3}),(\d{1,3})\)  # matches the same pattern as in Part 1, captures only the numbers
/x
|> Regex.scan(puzzle_input, capture: :all_but_first)
|> Enum.map(fn
  ["do()"] -> :on
  ["", "don't()"] -> :off
  ["", "", a, b] -> {String.to_integer(a), String.to_integer(b)}
end)
|> Enum.reduce({0, :on}, fn
  :on, {sum, _} -> {sum, :on}
  :off, {sum, _} -> {sum, :off}
  {a, b}, {sum, :on} -> {sum + a * b, :on}
  _, acc -> acc
end)
|> elem(0)