this post was submitted on 01 Dec 2025
47 points (100.0% liked)

Advent Of Code

1154 readers
39 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 1: Secret Entrance

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] eta@feddit.org 2 points 12 hours ago* (last edited 11 hours ago)

Elixir

Oh wow I'm late but that's because I decided to do this year in Elixir which I am not really experienced enough yet as it seems. Part 1 was ok but I didn't really put enough thought into Part 2 and got stuck for a while. However I got it done after a few failed tries and a few questions to AI on how to do things in Elixir.

#!/usr/bin/elixir

defmodule SafeDial do

def readInstructions(filename) do
	filename
	|> File.stream!()
	|> Stream.map(&String.trim/1)
	|> Stream.reject(&(&1 == ""))
	|> Stream.map(fn line ->
		case Regex.run(~r/^([A-Za-z])(\d+)$/, line) do
			[_, letter, number] -> {String.to_atom(letter), String.to_integer(number)}
			_ -> nil
		end
	end)
	|> Enum.reject(&(&1 == nil))
end

def evalInstructions(instructions) do
	initialPos = 50
	zeroCount = 0
	checkInstruction(instructions, initialPos, zeroCount)
end

defp checkInstruction(instructions, currPos, zeroCount) do
	case instructions do
		[] -> {currPos, zeroCount}
		[instruct | rest] ->
			nextPos =
				case instruct do
					{dir, step} when dir == :R and currPos + rem(step,100) > 99 ->
						currPos + rem(step,100) - 100
					{dir, step} when dir == :R and currPos + rem(step,100) > 99 ->
						currPos + rem(step,100) - 100
					{dir, step} when dir == :R ->
						currPos + rem(step,100)
					{dir, step} when dir == :L and currPos - rem(step,100) < 0 ->
						currPos - rem(step,100) + 100
					{dir, step} when dir == :L ->
						currPos - rem(step,100)
					_ -> raise "[ ERROR ] unkown instruction: #{inspect(instruct)}"
				end
			newZeroCount = if nextPos == 0, do: zeroCount + 1, else: zeroCount
			checkInstruction(rest, nextPos, newZeroCount)
		other -> raise "[ ERROR ] unknown instruction: #{inspect(other)}"
	end
end

def evalInstructionsPart2(instructions) do
	initialPos = 50
	zeroCount = 0
	checkInstructionPart2(instructions, initialPos, zeroCount)
end

defp checkInstructionPart2(instructions, currPos, zeroCount) do
	case instructions do
		[] -> {currPos, zeroCount}
		[instruct | rest] ->
			{nextPos, zeroCount1} =
				case instruct do
					{dir, step} when dir == :R and currPos + rem(step,100) == 100 ->
						{currPos + rem(step,100) - 100, zeroCount+floor(step/100)}
					{dir, step} when dir == :R and currPos + rem(step,100) > 99 ->
						{currPos + rem(step,100) - 100, zeroCount+floor(step/100)+1}
					{dir, step} when dir == :R ->
						{currPos + rem(step,100), zeroCount+floor(step/100)}
					{dir, step} when dir == :L and currPos == 0 and currPos - rem(step,100) < 0 ->
						{currPos - rem(step,100) + 100, zeroCount+floor(step/100)}
					{dir, step} when dir == :L and currPos - rem(step,100) < 0 ->
						{currPos - rem(step,100) + 100, zeroCount+floor(step/100)+1}
					{dir, step} when dir == :L ->
						{currPos - rem(step,100), zeroCount+floor(step/100)}
					_ -> raise "[ ERROR ] unkown instruction: #{inspect(instruct)}"
				end
			newZeroCount = if nextPos == 0, do: zeroCount1 + 1, else: zeroCount1
			checkInstructionPart2(rest, nextPos, newZeroCount)
		other -> raise "[ ERROR ] unknown instruction: #{inspect(other)}"
	end
end

end #module

IO.puts "### PART 1 ###"
instructions = SafeDial.readInstructions("input/day01Input.txt")
{finalPos, zeroCount} = SafeDial.evalInstructions(instructions)
IO.puts "final position:#{finalPos} zero count:#{zeroCount}"

IO.puts "### PART 2 ###"
instructions = SafeDial.readInstructions("input/day01Input.txt")
{finalPos, zeroCount} = SafeDial.evalInstructionsPart2(instructions)
IO.puts "final position:#{finalPos} zero count:#{zeroCount}"

Edit: removed debug output