Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
| 81c974b567 |
1 changed files with 9 additions and 25 deletions
|
|
@ -1,16 +1,10 @@
|
|||
module Aoc.Y2020.D09
|
||||
( day
|
||||
) where
|
||||
|
||||
import Data.List
|
||||
module Aoc.Y2020.D09 (day) where
|
||||
|
||||
import Aoc.Day
|
||||
import Aoc.Parse
|
||||
import Data.List
|
||||
|
||||
parser :: Parser [Int]
|
||||
parser = manyLines decimal
|
||||
|
||||
splitAndGroup :: Int -> [Int] -> [([Int], Int)]
|
||||
splitAndGroup :: Int -> [a] -> [([a], a)]
|
||||
splitAndGroup width xs = case splitAt width xs of
|
||||
(_, []) -> []
|
||||
(as, b:_) -> (as, b) : splitAndGroup width (drop 1 xs)
|
||||
|
|
@ -18,22 +12,12 @@ splitAndGroup width xs = case splitAt width xs of
|
|||
isValid :: [Int] -> Int -> Bool
|
||||
isValid nums n = elem n $ (+) <$> nums <*> nums
|
||||
|
||||
continuousSubsequences :: [a] -> [[a]]
|
||||
continuousSubsequences = filter (not . null) . concatMap tails . inits
|
||||
|
||||
findRanges :: Int -> [Int] -> [[Int]]
|
||||
findRanges target = filter ((== target) . sum) . continuousSubsequences
|
||||
|
||||
solver :: [Int] -> IO ()
|
||||
solver nums = do
|
||||
putStrLn ">> Part 1"
|
||||
let (_, invalidN) = head $ dropWhile (uncurry isValid) $ splitAndGroup 25 nums
|
||||
print invalidN
|
||||
|
||||
putStrLn ""
|
||||
putStrLn ">> Part 2"
|
||||
let weakness = head $ findRanges invalidN nums
|
||||
print $ minimum weakness + maximum weakness
|
||||
findRanges target = filter ((== target) . sum) . concatMap tails . inits
|
||||
|
||||
day :: Day
|
||||
day = dayParse parser solver
|
||||
day = dayParse (manyLines decimal) $ \nums -> do
|
||||
let (_, invalidN) = head $ dropWhile (uncurry isValid) $ splitAndGroup 25 nums
|
||||
putStrLn $ ">> Part 1: " ++ show invalidN
|
||||
let weakness = head $ findRanges invalidN nums
|
||||
putStrLn $ ">> Part 2: " ++ show (minimum weakness + maximum weakness)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue