Day 17 - sped up to ~6s using strict recursion!
This commit is contained in:
parent
c585f7d7ba
commit
ec07598f61
31
17.hs
31
17.hs
|
@ -1,23 +1,22 @@
|
||||||
{-# LANGUAGE BangPatterns #-}
|
{-# LANGUAGE BangPatterns #-}
|
||||||
import Data.List (foldl')
|
|
||||||
(%) = mod
|
(%) = mod
|
||||||
|
steps = 312
|
||||||
|
|
||||||
ith :: Int -> Int -> Int
|
postNth :: Int -> Int -> [Int] -> Int
|
||||||
ith i steps =
|
postNth 2018 pos list = list !! (pos + 1)
|
||||||
let (position, list) = foldl' (\(currPos, currList) n ->
|
postNth n pos list =
|
||||||
let newPos = (currPos + steps % n + 1) % n
|
let !newPos = (pos + steps % n + 1) % n
|
||||||
in (newPos, take newPos currList ++ [n] ++ drop newPos currList))
|
!newList = take newPos list ++ [n] ++ drop newPos list
|
||||||
(0, [0]) [1..i]
|
in postNth (n + 1) newPos newList
|
||||||
in list !! (position + 1)
|
|
||||||
|
|
||||||
oneth :: Int -> Int -> Int
|
oneNth :: Int -> Int -> Int -> Int
|
||||||
oneth i steps =
|
oneNth 50000001 _ oneth = oneth
|
||||||
snd $ foldl' (\(currPos, currOneth) n ->
|
oneNth n pos oneth =
|
||||||
let newPos = (currPos + steps % n + 1) % n
|
let !newPos = (pos + steps % n + 1) % n
|
||||||
in (newPos, if newPos == 0 then n else currOneth))
|
!newOneth = if newPos == 0 then n else oneth
|
||||||
(0, 1) [1..i]
|
in oneNth (n + 1) newPos newOneth
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
print $ ith 2017 312
|
print $ postNth 1 1 [0]
|
||||||
print $ oneth 50000000 312
|
print $ oneNth 1 1 1
|
Loading…
Reference in New Issue