Improve performance

This commit is contained in:
Sijmen 2018-12-01 16:21:38 +01:00
parent a2948ce5af
commit a68afd169e
3 changed files with 13 additions and 6 deletions

6
.gitignore vendored
View file

@ -1 +1,7 @@
# Ignore everything without extension
*
!*/
*.in
*.hi
*.o

View file

@ -1,6 +1,6 @@
module Day1A where
read' :: String -> Integer
read' :: String -> Int
read' ('+':str) = read str
read' str = read str

View file

@ -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
| (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