Advent of Code Day 1, 2025 (Elixir Reprise)

Posted on April 15, 2026 by Jarvis Cochrane · Tagged ,

I’ve applied for a job with a company that’s building their product with Elixir. Knowing Elixir isn’t a prerequisite, but being interested and able to learn it certainly is.

Let’s go back! Back to the… Advent of Code!

This little program is a translation of my original solution in Haskell to Elixir.

Slightly to my surprise the two versions are almost identical in length. The Elixir code is maybe slightly more verbose with do...end structures and defmodule...end, but the Haskell version has the type declarations.

I’ve a little more experience in functional programming than I did at the end of last year, and that shows in the Elixir version.

Now, where did I leave my DeLorean?

#
# Elixir solution for Advent Of Code 2025 day 1 (a).
#
# Given:
#
#   * A virtual combination lock whose dial can freely rotate through
#     positions numbered 0 to 99 so that rotating right (clockwise) one
#     step from position 99 goes to position 0 and rotating left
#     (anticlockwise) one step from position 0 goes to position 99.
#
#   * An initial dial position of 50.
#
#   * A file `input.txt` containing a list of rotations, one per line,
#     of the form Lx or Rx (regex: "[LR]\d+") denoting rotations by
#     x steps left or right. E.g:
#
#         L68   -> to position -18 aka 82
#         L30   -> to position -48 aka 52
#         R48   -> to position 100 aka 0
#         L5    -> to position  95
#
# Find:
#
#   * The number of times the dial is at position 0 after a rotation.
#
# Run with:
#
#   elixir AOCDay1a.exs
#
# April 14, 2026 by Jarvis Cochrane
# Copyright (c) 2026 Jarvis Cochrane
#

defmodule AOCDay1a do

  def main(filename) do    
    File.read!(filename)
    |> String.split
    |> read_codes
    |> Enum.scan(50, &rotate_dial/2)
    |> count_zeroes
    |> IO.inspect
  end

  # Convert a code to a positive (right) or negative (left) integer

  def read_code(<<direction::utf8, count::binary>>) do
    case direction do
      ?L -> - String.to_integer(count)
      ?R -> String.to_integer(count)
    end
  end

  # Convert list of codes to list of +/- integers

  def read_codes(codes), do: Enum.map(codes, &read_code/1)

  # Return the result of rotating the dial position by the delta
  # Results wrap around the dial so are always 0 <= i <= 99

  def rotate_dial(delta, position), do: Integer.mod(position + delta, 100)

  # Count the number of zeroes found in a list of integers

  def count_zeroes(positions), do: Enum.count(positions, fn p -> p == 0 end)

end

AOCDay1a.main("input.txt")