Skip to content

Instantly share code, notes, and snippets.

@ElvishJerricco
Last active December 16, 2017 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElvishJerricco/371a8bde2edd267dd40d14673d54b7ed to your computer and use it in GitHub Desktop.
Save ElvishJerricco/371a8bde2edd267dd40d14673d54b7ed to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.Text as T
import qualified Data.Text.Read as T
import qualified Data.Text.IO as T
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as MV
import Formatting
import Formatting.Clock
import System.Clock
main :: IO ()
main = do
start <- getTime Monotonic
content <- T.readFile "xmas5.txt"
end <- getTime Monotonic
fprint ("reading file into content took " % timeSpecs % "\n") start end
start1 <- getTime Monotonic
let f str = either (const Nothing) Just $ do
(i, str') <- T.signed T.decimal str
return (i, T.drop 1 str')
x <- V.thaw $ V.unfoldr f content
end1 <- getTime Monotonic
fprint ("parsing " % int % " lines took " % timeSpecs % "\n") (MV.length x) start1 end1
let loop :: Int -> Int -> IO Int
loop !steps !i
| i >= MV.length x || i < 0 = return steps
| otherwise = do
v <- MV.read x i
MV.write x i (if v >= 3 then v - 1 else v + 1)
loop (steps + 1) (i + v)
start2 <- getTime Monotonic
steps <- loop 0 0
end2 <- getTime Monotonic
fprint (int % ", is the answer, it took " % timeSpecs % "\n") steps start2 end2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment