Improve performance
This commit is contained in:
parent
a2948ce5af
commit
a68afd169e
3 changed files with 13 additions and 6 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1 +1,7 @@
|
|||
# Ignore everything without extension
|
||||
*
|
||||
!*/
|
||||
|
||||
*.in
|
||||
*.hi
|
||||
*.o
|
||||
|
|
2
Day1A.hs
2
Day1A.hs
|
@ -1,6 +1,6 @@
|
|||
module Day1A where
|
||||
|
||||
read' :: String -> Integer
|
||||
read' :: String -> Int
|
||||
read' ('+':str) = read str
|
||||
read' str = read str
|
||||
|
||||
|
|
11
Day1B.hs
11
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
|
||||
|
|
Loading…
Reference in a new issue