From 45d8cfc46ea16f398da1126486ee72e26ab0b08c Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sat, 16 Dec 2017 23:37:57 -0800 Subject: [PATCH] Day 17 - it's a lot faster in Javascript... --- .gitignore | 1 + 17.hs | 23 +++++++++++++++++++++++ 17.js | 15 +++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 17.hs create mode 100644 17.js diff --git a/.gitignore b/.gitignore index 8d0ac89..0f52a43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ * !.gitignore !*.hs +!*.js !*.txt \ No newline at end of file diff --git a/17.hs b/17.hs new file mode 100644 index 0000000..97231d5 --- /dev/null +++ b/17.hs @@ -0,0 +1,23 @@ +{-# LANGUAGE BangPatterns #-} +import Data.List (foldl') +(%) = mod + +ith :: Int -> Int -> Int +ith i steps = + let (position, list) = foldl' (\(currPos, currList) n -> + let newPos = (currPos + steps % n + 1) % n + in (newPos, take newPos currList ++ [n] ++ drop newPos currList)) + (0, [0]) [1..i] + in list !! (position + 1) + +oneth :: Int -> Int -> Int +oneth i steps = + snd $ foldl' (\(currPos, currOneth) n -> + let newPos = (currPos + steps % n + 1) % n + in (newPos, if newPos == 0 then n else currOneth)) + (0, 1) [1..i] + +main :: IO () +main = do + print $ ith 2017 312 + print $ oneth 50000000 312 \ No newline at end of file diff --git a/17.js b/17.js new file mode 100644 index 0000000..f355f17 --- /dev/null +++ b/17.js @@ -0,0 +1,15 @@ +p = 0; +arr = [0]; +for (n = 1; n <= 2017; n++) { + p = (p + 312 % n + 1) % n; + arr.splice(p, 0, n); +} +console.log(arr[arr.indexOf(2017) + 1]); + +p = 0; +oneth = 1; +for (n = 1; n <= 50000000; n++) { + p = (p + 312 % n + 1) % n; + if (p == 0) oneth = n; +} +console.log(oneth); \ No newline at end of file