bjorng

bjorng

Erlang Core Team

Part 1 took much more time than part 2. I started out by reusing my grid parsing function from day 4 and start coding before I had fully understood the splitting rules and how to count splits. It ended up in a mess.

After having finished part 2, I rewrote part 1 using the new parsing routine that I wrote for part 2.

defmodule Day07 do
  def part1(input) do
    {start, splitters} = parse_splitters(input)
    beams = [start]
    split_beams(beams, splitters, 0)
  end

  defp split_beams(_beams, [], num_splits), do: num_splits
  defp split_beams(beams, [splits | splitters], num_splits) do
    {beams, num_splits} = split_beams(beams, splits, [], num_splits)
    split_beams(beams, splitters, num_splits)
  end

  defp split_beams([], _splitters, new, num_splits) do
    {Enum.uniq(new), num_splits}
  end
  defp split_beams([column | beams], splits, new, num_splits) do
    case Enum.member?(splits, column) do
      true ->
        new = [column - 1, column + 1 | new]
        split_beams(beams, splits, new, num_splits + 1)
      false ->
        new = [column | new]
        split_beams(beams, splits, new, num_splits)
    end
  end

  def part2(input) do
    {start, splitters} = parse_splitters(input)
    {timelines, _} = count_timelines(start, splitters, %{})
    timelines
  end

  defp count_timelines(column, splitters, worlds) do
    key = {column, length(splitters)}
    case worlds do
      %{^key => timelines} ->
        {timelines, worlds}
      %{} ->
        {timelines, worlds} = count_timelines_split(column, splitters, worlds)
        {timelines, Map.put(worlds, key, timelines)}
    end
  end

  defp count_timelines_split(_column, [], worlds) do
    {1, worlds}
  end
  defp count_timelines_split(column, [splits | splitters], worlds) do
    case Enum.member?(splits, column) do
      true ->
        {timelines1, worlds} = count_timelines(column - 1, splitters, worlds)
        {timelines2, worlds} = count_timelines(column + 1, splitters, worlds)
        timelines = timelines1 + timelines2
        {timelines, worlds}
      false ->
        count_timelines(column, splitters, worlds)
    end
  end

  defp parse_splitters(input) do
    splitters = input
    |> Enum.map(fn line ->
      String.to_charlist(line)
      |> Enum.with_index
      |> Enum.flat_map(fn {char, col} ->
        case char do
          ?. -> []
          ?S -> [{:start, col}]
          ?^ -> [col]
        end
      end)
    end)
    [[{:start, start}] | splitters] = splitters
    {start, splitters}
  end
end

EDIT: Further simplified part 1 by removing vestiges of my messy first solution.

Showing Posts 1 to 10

lkuty

lkuty

#!/usr/bin/env elixir

# Advent of Code 2025. Day 7

defmodule M do
  def go_downward_1(_map, row, _cols, max_row, n_split) when row > max_row, do: n_split
  def go_downward_1(map, row, cols, max_row, n_split) do
    {cols, n_split} = Enum.reduce(cols, {cols, n_split}, fn col, {cols, n_split} = acc ->
      case Map.get(map, {row+1, col}) do
        nil  -> acc
        true -> {cols |> MapSet.delete(col) |> MapSet.put(col-1) |> MapSet.put(col+1), n_split+1}
      end
    end)
    go_downward_1(map, row+1, cols, max_row, n_split)
  end

  def go_downward_2(_map, row, cols, max_row) when row > max_row, do: cols
  def go_downward_2(map, row, cols, max_row) do
    cols = Enum.reduce(cols, cols, fn {col, n}, cols ->
      case Map.get(map, {row+1, col}) do
        nil  -> cols
        true -> cols |> Map.delete(col) |> Map.update(col-1, n, & &1+n) |> Map.update(col+1, n, & &1+n)
      end
    end)
    go_downward_2(map, row+1, cols, max_row)
  end
end

# Setup
{map, {start_row, start_col}, max_row} = File.read!("../day07.txt")
  |> String.split("\n")
  |> Enum.with_index(1)
  |> Enum.reduce({%{}, nil, 0}, fn {line, row}, {map, start, max_row} ->
    line
    |> String.codepoints()
    |> Enum.with_index(1)
    |> Enum.reduce({map, start, max_row}, fn
      {"S", col}, {map, nil, max_row} -> {map, {row,col}, (if row>max_row, do: row, else: max_row)}
      {"^", col}, {map, start, max_row} -> {Map.put(map, {row,col}, true), start, (if row>max_row, do: row, else: max_row)}
      {".", _col}, {map, start, max_row} -> {map, start, (if row>max_row, do: row, else: max_row)}
    end)
  end)

# Part 1
n_split = M.go_downward_1(map, start_row, MapSet.new([start_col]), max_row, 0)
IO.puts "Day 7. Part 1: #{n_split}"

# Part 2
cols = M.go_downward_2(map, start_row, %{start_col => 1}, max_row)
IO.puts "Day 7. Part 2: #{Enum.reduce(cols, 0, fn {_col,n}, total -> total+n end)}"
dompdv

dompdv

Simplified after doing Part2 because Part2 solves also Part1.

defmodule AdventOfCode.Solution.Year2025.Day07 do
  def part1(input), do: input |> fire_beam() |> take_counter()
  def part2(input), do: input |> fire_beam() |> take_timelines() |> Map.values() |> Enum.sum()
  def take_counter(a), do: elem(a, 1)
  def take_timelines(a), do: elem(a, 0)

  def fire_beam(input) do
    [[start] | rows] = parse(input)
    beam(Map.new([{start, 1}]), 0, rows)
  end

  def beam(in_flight, hits_counter, []), do: {in_flight, hits_counter}

  def beam(in_flight, hits_counter, [next_row_splitters | rows]) do
    # in_flight %{column => number of timelines leading to this point}
    # hits_counter : number of hits on a splitter
    {new_in_flight, new_hits} =
      Enum.reduce(in_flight, {%{}, 0}, fn {col, n_timelines}, {next_positions, hits} ->
        if col in next_row_splitters,
          do: {
            next_positions
            |> Map.update(col - 1, n_timelines, &(&1 + n_timelines))
            |> Map.update(col + 1, n_timelines, &(&1 + n_timelines)),
            hits + 1
          },
          else: {Map.update(next_positions, col, n_timelines, &(&1 + n_timelines)), hits}
      end)

    beam(new_in_flight, hits_counter + new_hits, rows)
  end

  def parse(input) do
    input
    |> String.split("\n", trim: true)
    |> Enum.reduce([], fn line, lines ->
      splitter_cols =
        to_charlist(line)
        |> Enum.with_index()
        |> Enum.reduce([], fn
          {?., _}, acc -> acc
          {?^, col}, l -> [col | l]
          {?S, col}, l -> [col | l]
        end)

      if splitter_cols == [], do: lines, else: [splitter_cols | lines]
    end)
    |> Enum.reverse()
  end
end

vkryukov

vkryukov

Nice problem - you can solve both part simultaneously if you keep track of everything.

defmodule Y2025.Day07 do
  def parse(s) do
    {[first], rest} = s |> String.split("\n") |> Enum.split(1)

    start = Enum.find_index(first |> String.graphemes(), &(&1 == "S"))

    n = String.length(first)

    rest =
      rest
      |> Enum.map(fn line ->
        line
        |> String.graphemes()
        |> Enum.with_index()
        |> Enum.filter(&(elem(&1, 0) == "^"))
        |> Enum.map(&elem(&1, 1))
        |> MapSet.new()
      end)
      |> Enum.reject(&Enum.empty?/1)

    {start, n, rest}
  end

  def run(s) do
    {start, n, splitters} = parse(s)

    splitters
    |> Enum.reduce({[{start, 1}], 0}, fn splitter, {current_beams, count} ->
      {new_beams, inc} = current_beams |> split(splitter, n)
      {new_beams, inc + count}
    end)
  end

  def split(beams, splitter, n) do
    {beams, count} =
      beams
      |> Enum.reduce({[], 0}, fn {beam, beam_count}, {current_beams, n_splits} ->
        {new_beams, inc} =
          if MapSet.member?(splitter, beam) do
            {
              if(beam < 0, do: [], else: [{beam - 1, beam_count}]) ++
                if(beam >= n, do: [], else: [{beam + 1, beam_count}]),
              1
            }
          else
            {[{beam, beam_count}], 0}
          end

        {Enum.concat(new_beams, current_beams), n_splits + inc}
      end)

    {beams |> compress, count}
  end

  def compress(list) do
    list
    |> Enum.sort()
    |> Enum.chunk_by(&elem(&1, 0))
    |> Enum.flat_map(fn l ->
      beam = l |> List.first() |> elem(0)
      sum = l |> Enum.map(&elem(&1, 1)) |> Enum.sum()
      [{beam, sum}]
    end)
  end

  def part1(s) do
    run(s) |> elem(1)
  end

  def part2(s) do
    run(s) |> elem(0) |> Enum.map(&elem(&1, 1)) |> Enum.sum()
  end
end
hauleth

hauleth

Parse

[start | rest] = String.split(puzzle_input)

start_col = byte_size(start) - byte_size(String.trim_leading(start, "."))

splitters =
  Enum.map(rest, fn row ->
    row
    |> String.to_charlist()
    |> Enum.with_index()
    |> Enum.filter(&(elem(&1, 0) == ?^))
    |> MapSet.new(&elem(&1, 1))
  end)

Part 1

Enum.reduce(splitters, {MapSet.new([start_col]), 0}, fn splits, {beams, count} ->
  import MapSet, only: [intersection: 2, difference: 2, union: 2]
  
  hits = intersection(beams, splits)
  new_beams = for hit <- hits, dx <- [-1, 1], into: MapSet.new(), do: hit + dx
  beams = beams |> difference(hits) |> union(new_beams)

  {beams, MapSet.size(hits) + count}
end)

Part 2

Enum.reduce(splitters, %{start_col => 1}, fn splits, beams ->
  Enum.reduce(splits, beams, fn s, acc ->
    case Map.pop(acc, s) do
      {nil, map} ->
        map

      {count, map} ->
        Map.merge(
          map,
          %{
            (s + 1) => count,
            (s - 1) => count
          },
          fn _k, a, b -> a + b end
        )
    end
  end)
end)
|> Enum.sum_by(&elem(&1, 1))

EDIT

I decided to draw the resulting image

lud

lud

Part 1 took me more time too :slight_smile:

defmodule AdventOfCode.Solutions.Y25.Day07 do
  alias AoC.Input

  def parse(input, _part) do
    lines =
      input
      |> Input.read!()
      |> String.split("\n", trim: true)
      |> Enum.map(&String.to_charlist/1)

    [first_row | rows] = lines
    start = Enum.find_index(first_row, fn x -> x == ?S end)

    layers =
      Enum.map(rows, fn row ->
        for {?^, x} <- Enum.with_index(row),
            reduce: %{},
            do: (acc -> Map.put(acc, x, true))
      end)

    {start, layers}
  end

  def part_one({start, layers}) do
    {_last_positions, count} =
      Enum.reduce(layers, {[start], 0}, fn layer, {poses, count} ->
        {splitters, keep_positions} = Enum.split_with(poses, &Map.has_key?(layer, &1))
        count = count + length(splitters)
        split_positions = Enum.flat_map(splitters, &[&1 - 1, &1 + 1])
        new_positions = Enum.uniq(keep_positions ++ split_positions)
        {new_positions, count}
      end)

    count
  end

  def part_two({start_pos, layers}) do
    new_poscounts =
      Enum.reduce(layers, _init_poscounts = %{start_pos => 1}, fn layer, poscounts ->
        Enum.reduce(poscounts, %{}, fn {pos, n}, new_poscounts ->
          if Map.has_key?(layer, pos) do
            new_poscounts
            |> Map.update(pos - 1, n, &(&1 + n))
            |> Map.update(pos + 1, n, &(&1 + n))
          else
            Map.update(new_poscounts, pos, n, &(&1 + n))
          end
        end)
      end)

    Enum.sum_by(new_poscounts, &elem(&1, 1))
  end
end

sevenseacat

sevenseacat

Author of Ash Framework

I have no idea what the algorithm is called here but I think I’ve done something very similar in previous puzzles - instead of adding | to my grid map, add the number of beams that have reached this point. Then at the end, add up all of the numbers on the bottom row.

https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2025/day07.ex

Wasted about half an hour debugging the part 2 example because one of my clauses was wrong - I forgot to increment when a beam overlaps another beam :woman_facepalming:

Name                     ips        average  deviation         median         99th %
day 07, part 1         99.21       10.08 ms     ±7.20%        9.72 ms       11.75 ms
day 07, part 2         90.82       11.01 ms     ±7.99%       10.61 ms       12.94 ms
rvnash

rvnash

I think it’s called “Pascal’s Triangle”, dunno maybe I’m wrong. Anyway, I naively did Part 2 and of course it doesn’t complete in the age of the universe. So, decided to keep track of both timelines and tachyons in one pass. Went much better. Used MapSet and Map to speed lookups. Fun day!

Edit: It is Pascal’s Triangle except with the twist that the tree doesn’t split necessarily evenly on each level.

defmodule RAoc.Solutions.Y25.Day07 do
  alias AoC.Input

  def parse(input, _part) do
    Input.read!(input)
    |> String.split("\n", trim: true)
    |> then(fn [first_line | rest] ->
      {first_line
       |> String.graphemes()
       |> Enum.with_index()
       |> Enum.filter(fn {c, _n} -> c == "S" end)
       |> List.first()
       |> elem(1),
       rest
       |> Enum.map(fn line ->
         line
         |> String.graphemes()
         |> Enum.with_index()
         |> Enum.reject(&(elem(&1, 0) == "."))
         |> Enum.map(&elem(&1, 1))
         |> MapSet.new()
       end)}
    end)
  end

  def part_one(problem) do
    tach_counters(problem)
    |> elem(1)
  end

  def part_two(problem) do
    tach_counters(problem)
    |> elem(0)
    |> Enum.sum_by(fn {_, n} -> n end)
  end

  defp tach_counters({tachyon, rows}) do
    tachyons_w_timelines = %{tachyon => 1}

    Enum.reduce(rows, {tachyons_w_timelines, 0}, fn row, {tachyons_w_timelines, split_count} ->
      Enum.reduce(tachyons_w_timelines, {tachyons_w_timelines, split_count}, fn {n, last},
                                                                                {tachyons_w_timelines,
                                                                                 split_count} ->
        if MapSet.member?(row, n) do
          {tachyons_w_timelines
           |> Map.delete(n)
           |> Map.update(n + 1, last, &(&1 + last))
           |> Map.update(n - 1, last, &(&1 + last)), split_count + 1}
        else
          {tachyons_w_timelines, split_count}
        end
      end)
    end)
  end
end
newton-peixoto

newton-peixoto

Part 01 was pretty straightforward, but in Part 02 I had difficulty parsing the input into an adjacency list. On my first try, I ran a pure DFS, which of course didn’t finish. I had to optimize it using memoization which took some time

https://github.com/newton-peixoto/advent-of-code/blob/main/2025/livebooks/day-07.livemd

sevenseacat

sevenseacat

Author of Ash Framework

oh I’ve heard of that! Cheers :slight_smile:

vkryukov

vkryukov

Very elegant solution. I realized, after looking at it, that I shouldn’t even have to check for boundary conditions, as by design the beams cannot go outside.

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews