Part 1 done
This commit is contained in:
parent
fa87faaa02
commit
0305b145a7
1 changed files with 31 additions and 1 deletions
32
day9/main.hs
32
day9/main.hs
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue