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

Day 3

day3.livemd

Day 3

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

Part 1

https://adventofcode.com/2024/day/3

input = Kino.FS.file_path("day3_input.txt") |> File.read!()

do_math = fn input_ -> 
  Regex.scan(~r/mul\((\d+)(?:,)(\d+)\)/, input_, capture: :all_but_first)
  |> Enum.map(fn match -> Enum.map(match, &String.to_integer/1) end)
  |> Enum.map(&Enum.product/1)
  |> Enum.sum()
end

result = do_math.(input)

Part 2

removable_sections = Regex.scan(~r/don\'t\(\).+?do\(\)/s, input)

defmodule Remover do
  def remove_all(string, []), do: string
  def remove_all(string, [substring | remaining]) do
    new = String.replace(string, substring, "")
    remove_all(new, remaining)
  end
end

result = Remover.remove_all(input, removable_sections)
  |> do_math.()