1
0
Fork 0

Day 17 - it's a lot faster in Javascript...

This commit is contained in:
Jonathan Chan 2017-12-16 23:37:57 -08:00
parent e46dd27349
commit 45d8cfc46e
3 changed files with 39 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
* *
!.gitignore !.gitignore
!*.hs !*.hs
!*.js
!*.txt !*.txt

23
17.hs Normal file
View File

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

15
17.js Normal file
View File

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