1
0
Fork 0

day17 solution

main
Andrew Coleman 2017-12-17 23:30:12 -05:00
parent 1445104f24
commit 0cb6fcf521
2 changed files with 30 additions and 0 deletions

8
day17/Makefile Normal file
View File

@ -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

22
day17/day17.erl Normal file
View File

@ -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)]).