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
|
module Day01 (main) where
|
||||||
|
|
||||||
import Data.Set (empty, insert, member)
|
import Data.IntSet (empty, insert, member)
|
||||||
|
|
||||||
freqChangeList :: String -> [Int]
|
parseChanges :: String -> [Int]
|
||||||
freqChangeList = map (read . dropWhile (== '+')) . lines
|
parseChanges = map (read . dropWhile (== '+')) . lines
|
||||||
|
|
||||||
totalFrequency :: [Int] -> Int
|
part1 :: [Int] -> Int
|
||||||
totalFrequency = foldr1 (+)
|
part1 = sum
|
||||||
|
|
||||||
firstFreqTwice :: [Int] -> Int
|
part2 :: [Int] -> Int
|
||||||
firstFreqTwice changeList =
|
part2 = dupl empty . scanl1 (+) . cycle
|
||||||
let freqs = scanl1 (+) $ cycle changeList
|
where dupl s (f:fs)
|
||||||
in dupl freqs empty
|
| member f s = f
|
||||||
where dupl (f:fs) s =
|
| otherwise = dupl (insert f s) fs
|
||||||
if member f s
|
|
||||||
then f
|
|
||||||
else dupl fs (insert f s)
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "input/01.txt"
|
changes <- parseChanges <$> readFile "input/01.txt"
|
||||||
let changeList = freqChangeList input
|
print $ part1 changes
|
||||||
print $ totalFrequency changeList
|
print $ part2 changes
|
||||||
print $ firstFreqTwice changeList
|
|
||||||
|
|
Loading…
Reference in New Issue