Day 17 - it's a lot faster in Javascript...
This commit is contained in:
parent
e46dd27349
commit
45d8cfc46e
|
@ -1,4 +1,5 @@
|
||||||
*
|
*
|
||||||
!.gitignore
|
!.gitignore
|
||||||
!*.hs
|
!*.hs
|
||||||
|
!*.js
|
||||||
!*.txt
|
!*.txt
|
|
@ -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
|
|
@ -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);
|
Loading…
Reference in New Issue