From a68afd169ea53117da890fedb161f0e9942d11eb Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 1 Dec 2018 16:21:38 +0100 Subject: [PATCH] Improve performance --- .gitignore | 6 ++++++ Day1A.hs | 2 +- Day1B.hs | 11 ++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 14140f4..65dd287 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ +# Ignore everything without extension +* +!*/ + *.in +*.hi +*.o diff --git a/Day1A.hs b/Day1A.hs index 7a05045..4ed7b42 100644 --- a/Day1A.hs +++ b/Day1A.hs @@ -1,6 +1,6 @@ module Day1A where -read' :: String -> Integer +read' :: String -> Int read' ('+':str) = read str read' str = read str diff --git a/Day1B.hs b/Day1B.hs index e9553fb..5c096ef 100644 --- a/Day1B.hs +++ b/Day1B.hs @@ -1,14 +1,15 @@ module Day1B where import Day1A (read') -import qualified Data.Set as S +import qualified Data.IntSet as S -handle :: Integer -> (S.Set Integer) -> [Integer] -> Integer +handle :: Int -> S.IntSet -> [Int] -> Int handle freq history (nextDelta:xs) - | S.member freq history = freq -- When the frequency is in the history set, return it - | otherwise = handle (freq + nextDelta) (S.insert freq history) xs -- Recurse otherwise + | (S.size s) == (S.size history) = freq -- When the frequency is in the history set, return it + | otherwise = handle (freq + nextDelta) (S.insert freq history) xs -- Recurse otherwise + where s = S.insert freq history handle _ _ _ = error "xs should not be empty :(" main :: IO () main = interact $ show . handle 0 S.empty . deltaFreqs - where deltaFreqs = cycle . map read' . lines -- Convert input to Integers and repeat infinitely + where deltaFreqs = cycle . map read' . lines -- Convert input to Ints and repeat infinitely