Last active
December 16, 2017 04:03
-
-
Save ElvishJerricco/4e6d9b467a55a672b279d29c526c8644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Data.Attoparsec.ByteString.Char8 | |
import qualified Data.ByteString as BS | |
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 Done _ parsed = parse fileParser content | |
x <- V.thaw $ V.fromList parsed | |
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 | |
fileParser :: Parser [Int] | |
fileParser = many' (signed decimal <* endOfLine) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment