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
