From 0cb6fcf521c2f754aa6cbc2a039c8eaa7e166e63 Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Sun, 17 Dec 2017 23:30:12 -0500 Subject: [PATCH] day17 solution --- day17/Makefile | 8 ++++++++ day17/day17.erl | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 day17/Makefile create mode 100644 day17/day17.erl diff --git a/day17/Makefile b/day17/Makefile new file mode 100644 index 0000000..bc4f621 --- /dev/null +++ b/day17/Makefile @@ -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 diff --git a/day17/day17.erl b/day17/day17.erl new file mode 100644 index 0000000..056f85c --- /dev/null +++ b/day17/day17.erl @@ -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)]).