Day 01 - minor editing, contracting part 2.
This commit is contained in:
parent
f46e146baa
commit
0f238f22a1
30
src/Day01.hs
30
src/Day01.hs
|
@ -1,25 +1,21 @@
|
|||
module Day01 (main) where
|
||||
|
||||
import Data.Set (empty, insert, member)
|
||||
import Data.IntSet (empty, insert, member)
|
||||
|
||||
freqChangeList :: String -> [Int]
|
||||
freqChangeList = map (read . dropWhile (== '+')) . lines
|
||||
parseChanges :: String -> [Int]
|
||||
parseChanges = map (read . dropWhile (== '+')) . lines
|
||||
|
||||
totalFrequency :: [Int] -> Int
|
||||
totalFrequency = foldr1 (+)
|
||||
part1 :: [Int] -> Int
|
||||
part1 = sum
|
||||
|
||||
firstFreqTwice :: [Int] -> Int
|
||||
firstFreqTwice changeList =
|
||||
let freqs = scanl1 (+) $ cycle changeList
|
||||
in dupl freqs empty
|
||||
where dupl (f:fs) s =
|
||||
if member f s
|
||||
then f
|
||||
else dupl fs (insert f s)
|
||||
part2 :: [Int] -> Int
|
||||
part2 = dupl empty . scanl1 (+) . cycle
|
||||
where dupl s (f:fs)
|
||||
| member f s = f
|
||||
| otherwise = dupl (insert f s) fs
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- readFile "input/01.txt"
|
||||
let changeList = freqChangeList input
|
||||
print $ totalFrequency changeList
|
||||
print $ firstFreqTwice changeList
|
||||
changes <- parseChanges <$> readFile "input/01.txt"
|
||||
print $ part1 changes
|
||||
print $ part2 changes
|
||||
|
|
Loading…
Reference in New Issue