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

Advent of Code 2024/3

2024/3.livemd

Advent of Code 2024/3

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

Section

input = Kino.Input.textarea("input")
defmodule AoC2024_3 do
  def parse(input) do
    Regex.scan(~r/(mul\((\d{1,3})\,(\d{1,3})\)+)/, Kino.Input.read(input))
  end

  def parse_2(input) do
    [["do()", "do()"]] ++ Regex.scan(~r/(do\(\)|don\'t\(\)|mul\((\d{1,3})\,(\d{1,3})\)+)/, Kino.Input.read(input))
  end

  def part_1(input) do
    input
    |> parse
    |> Enum.map(fn mul -> String.to_integer(Enum.at(mul, 2)) * String.to_integer(Enum.at(mul,3)) end)
    |> Enum.sum
  end

  def part_2(input) do
    input
    |> parse_2
    |> Enum.chunk_by(fn el -> Enum.member?(["do()", "don't()"], Enum.at(el, 0)) end)
    |> Enum.chunk_every(2)
    |> Enum.filter(fn row -> Enum.at(Enum.at(Enum.at(row, 0), 0), 0) == "do()" end)
    |> Enum.flat_map(fn [_head | tail] -> tail end)
    |> Enum.concat
    |> dbg()
    |> Enum.map(fn mul -> String.to_integer(Enum.at(mul, 2)) * String.to_integer(Enum.at(mul,3)) end)
    |> Enum.sum
  end
end
AoC2024_3.part_1(input)
AoC2024_3.part_2(input)