From 0305b145a762508f23c72d0eb497b04e2b7cef9e Mon Sep 17 00:00:00 2001 From: Martin Potier Date: Thu, 10 Dec 2020 14:26:48 +0200 Subject: [PATCH] Part 1 done --- day9/main.hs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/day9/main.hs b/day9/main.hs index d75ea03..1fa4e84 100755 --- a/day9/main.hs +++ b/day9/main.hs @@ -1,20 +1,50 @@ #! /usr/bin/env -S"ANSWER=42" nix-shell #! 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" {-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-} {-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports #-} {-# LANGUAGE OverloadedStrings #-} +import Data.IntSet (IntSet) +import Data.List (tails) import Debug.Trace (trace) import Text.Pretty.Simple +import qualified Data.IntSet as S + exampleData :: [Int] exampleData = [ 35,20,15,25,47,40,62,55,65,95,102,117,150 , 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 = do putStrLn "Test" + print exampleData + print $ 1 `S.member` (pairSums []) + print $ pairSums exampleData putStrLn "Day 9 - Part 1" + print $ solvePart1 5 exampleData + input <- lines <$> readFile "day9/input" + print $ solvePart1 25 $ map read input