Advent of Code 2025 - Day 1

Boilerplate

def file_to_lines(file), do: file |> File.read!() |> String.trim() |> String.split("\n")
def file_to_lines(file, fun), do: file |> file_to_lines() |> Enum.map(fun)

Part 1

def part1(file), do: file |> file_to_lines(&parse/1) |> count0()
def parse(l), do: l |> String.replace("R", "") |> String.replace("L", "-") |> Integer.parse() |> elem(0)
def count0(i), do: i |> Enum.scan(50, &Integer.mod(&1 + &2, 100)) |> Enum.count(&(&1 == 0))

Enum.scan/3 works really well here.

Part 2

def part2(file), do: file |> file_to_lines(&parse/1) |> Stream.flat_map(&flatten/1) |> count0()
def flatten(x), do: if(x < 0, do: List.duplicate(-1, abs(x)), else: List.duplicate(1, x))

It’s tempting to do math, but reusing prior work is simpler if less performant.