Part 1 done

This commit is contained in:
Martin Potier 2020-12-10 14:26:48 +02:00
parent fa87faaa02
commit 0305b145a7
No known key found for this signature in database
GPG key ID: D4DD957DBA4AD89E

View file

@ -1,20 +1,50 @@
#! /usr/bin/env -S"ANSWER=42" nix-shell #! /usr/bin/env -S"ANSWER=42" nix-shell
#! nix-shell -p ghcid #! nix-shell -p ghcid
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple])" #! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple containers])"
#! nix-shell -i "ghcid -c 'ghci' -T main" #! nix-shell -i "ghcid -c 'ghci' -T main"
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-} {-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports #-} {-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
import Data.IntSet (IntSet)
import Data.List (tails)
import Debug.Trace (trace) import Debug.Trace (trace)
import Text.Pretty.Simple import Text.Pretty.Simple
import qualified Data.IntSet as S
exampleData :: [Int] exampleData :: [Int]
exampleData = [ 35,20,15,25,47,40,62,55,65,95,102,117,150 exampleData = [ 35,20,15,25,47,40,62,55,65,95,102,117,150
, 182,127,219,299,277,309,576 ]; , 182,127,219,299,277,309,576 ];
pairSums :: [Int] -> IntSet
pairSums xs = S.fromList $ do
i1 <- xs
i2 <- xs
pure (i1 + i2)
isSumOfPreamble :: Int -> [Int] -> Bool
isSumOfPreamble size xs = go tx
where
go (x:_) = x `S.member` (pairSums preamble)
go ([]) = False
(preamble, tx) = splitAt size xs
solvePart1 :: Int -> [Int] -> (Int,Bool)
solvePart1 size message =
head $ dropWhile (snd) $
map go ( takeWhile ((> size) . length) $ tails message )
where
go xs = (head $ drop size xs,isSumOfPreamble size xs)
main :: IO () main :: IO ()
main = do main = do
putStrLn "Test" putStrLn "Test"
print exampleData
print $ 1 `S.member` (pairSums [])
print $ pairSums exampleData
putStrLn "Day 9 - Part 1" putStrLn "Day 9 - Part 1"
print $ solvePart1 5 exampleData
input <- lines <$> readFile "day9/input"
print $ solvePart1 25 $ map read input