Advent of Code 2025 - Day 6

Even better. Thank you!

Catching up after the weekend. Not the best, but will do.

defmodule Aoc2025.Solutions.Y25.Day06 do
  alias AoC.Input

  def parse(input, _part) do
    Input.read!(input)
    |> String.split("\n", trim: true)
  end

  def part_one(problem) do
    problem
    |> Enum.flat_map(&(String.split(&1) |> Enum.with_index()))
    |> Enum.reverse()
    |> Enum.group_by(fn {x, y} -> y end, fn {x, y} -> x end)
    |> Map.values()
    |> Enum.map(fn [op | numbers] -> build_expr_and_calc(numbers, op) end)
    |> Enum.sum()
  end

  def part_two(rows) do
    col_sizes =
      rows
      |> Enum.map(&fetch_space_locations/1)
      |> Enum.zip_reduce([], &fetch_max_col_size/2)
      |> Enum.reverse()

    Enum.map(rows, fn row -> split_row(row, col_sizes) end)
    |> Enum.zip_with(&(&1 |> Enum.reverse()))
    |> Enum.map(fn [op | col] -> [op | Enum.zip_with(Enum.reverse(col), & &1)] end)
    |> Enum.map(fn [op | numbers] -> build_expr_and_calc(numbers, op) end)
    |> Enum.sum()
  end

  def build_expr_and_calc(numbers, op) do
    numbers |> Enum.intersperse(op) |> List.to_string() |> Code.eval_string() |> elem(0)
  end

  def fetch_space_locations(rows) do
    Regex.scan(~r/[\d\*\+]+/, rows, return: :index, trim: true) |> List.flatten()
  end

  def fetch_max_col_size(els, acc) do
    [Enum.max_by(els, fn {x, y} -> y end) | acc]
  end

  def split_row(row, col_sizes) do
    Enum.map(col_sizes, fn {start, size} ->
      String.slice(row, start, size) |> String.to_charlist()
    end)
  end
end