day17 solution
parent
1445104f24
commit
0cb6fcf521
|
@ -0,0 +1,8 @@
|
|||
all: day17.beam
|
||||
erl -noshell -s day17 start -s init stop
|
||||
|
||||
day17.beam:
|
||||
erlc day17.erl
|
||||
|
||||
clean:
|
||||
rm -f day17.beam
|
|
@ -0,0 +1,22 @@
|
|||
-module(day17).
|
||||
-export([start/0]).
|
||||
|
||||
spin_the_lock(NextValue, SpinQueue, Index, _Steps)
|
||||
when NextValue == 2018 -> lists:nth(Index + 1 rem 2018, SpinQueue);
|
||||
spin_the_lock(NextValue, SpinQueue, Index, Steps)
|
||||
-> NextPosition = (Index + Steps) rem NextValue,
|
||||
{ First, Last } = lists:split(NextPosition, SpinQueue),
|
||||
spin_the_lock(NextValue + 1, lists:append([First, [NextValue], Last]), NextPosition + 1, Steps).
|
||||
|
||||
spin_the_lock2(NextValue, ValAtOne, _Index, _Steps)
|
||||
when NextValue == 50000001 -> ValAtOne;
|
||||
spin_the_lock2(NextValue, ValAtOne, Index, Steps)
|
||||
-> NextPosition = (Index + Steps) rem NextValue,
|
||||
if NextPosition == 0 -> spin_the_lock2(NextValue + 1, NextValue, NextPosition + 1, Steps);
|
||||
true -> spin_the_lock2(NextValue + 1, ValAtOne, NextPosition + 1, Steps)
|
||||
end.
|
||||
|
||||
start()
|
||||
-> io:fwrite("~p~n", [spin_the_lock(1, [0], 0, 3)]),
|
||||
io:fwrite("~p~n", [spin_the_lock(1, [0], 0, 303)]),
|
||||
io:fwrite("~p~n", [spin_the_lock2(1, 0, 0, 303)]).
|
Loading…
Reference in New Issue