Mixifying
This commit is contained in:
18
lib/advent_of_code.ex
Normal file
18
lib/advent_of_code.ex
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule AdventOfCode do
|
||||
@moduledoc """
|
||||
Documentation for `AdventOfCode`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Hello world.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> AdventOfCode.hello()
|
||||
:world
|
||||
|
||||
"""
|
||||
def hello do
|
||||
:world
|
||||
end
|
||||
end
|
||||
8
lib/year_2025/day1.exs
Normal file
8
lib/year_2025/day1.exs
Normal file
@@ -0,0 +1,8 @@
|
||||
filepath = Path.join([File.cwd!, "lib", "year_2025", "day1_input.txt"])
|
||||
ops = File.stream!(filepath)
|
||||
|>Stream.map(&String.trim_trailing/1)
|
||||
|>Enum.to_list
|
||||
|
||||
{_, zeroes} = AdventOfCode.Year2025.Dial.rotate(ops)
|
||||
IO.puts(zeroes)
|
||||
|
||||
4059
lib/year_2025/day1_input.txt
Normal file
4059
lib/year_2025/day1_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
38
lib/year_2025/dial.ex
Normal file
38
lib/year_2025/dial.ex
Normal file
@@ -0,0 +1,38 @@
|
||||
defmodule AdventOfCode.Year2025.Dial do
|
||||
# Initial conditions
|
||||
def rotate(operations), do: rotate(50, operations, 0)
|
||||
|
||||
# When no operations remain, return the number of zeroes
|
||||
def rotate(index, [], zeroes), do: { index, zeroes }
|
||||
|
||||
# Compute the new dial position
|
||||
def rotate(index, [head | tail], zeroes) do
|
||||
{ new_index, _crosses } = case head do
|
||||
"R" <> number -> rotate_right(index, String.to_integer(number))
|
||||
"L" <> number -> rotate_left(index, String.to_integer(number))
|
||||
end
|
||||
|
||||
case new_index do
|
||||
0 -> rotate(new_index, tail, zeroes+1)
|
||||
_ -> rotate(new_index, tail, zeroes)
|
||||
end
|
||||
end
|
||||
|
||||
defp rotate_right(index, number) do
|
||||
crosses = div(number, 100)
|
||||
diff = index + rem(number, 100)
|
||||
case diff do
|
||||
diff when diff <= 99 -> { diff, crosses }
|
||||
diff when diff > 99 -> { diff - 100, crosses + 1 }
|
||||
end
|
||||
end
|
||||
|
||||
defp rotate_left(index, number) do
|
||||
crosses = div(number, 100)
|
||||
diff = index - rem(number, 100)
|
||||
case diff do
|
||||
diff when diff >= 0 -> { diff, crosses }
|
||||
diff when diff < 0 -> { diff + 100, crosses + 1 }
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user