Skip to content

Instantly share code, notes, and snippets.

@ElvishJerricco
Last active December 16, 2017 04:05
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/201b0264b7e7a07a9d8501958f15d24f to your computer and use it in GitHub Desktop.
Save ElvishJerricco/201b0264b7e7a07a9d8501958f15d24f to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8
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 <- BS.readFile "xmas5.txt"
end <- getTime Monotonic
fprint ("reading file into content took " % timeSpecs % "\n") start end
start1 <- getTime Monotonic
let f str = do
(i, str') <- BS8.readInt str
return (i, BS.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