Shorten 2020_09
This commit is contained in:
parent
f642872ea1
commit
81c974b567
1 changed files with 9 additions and 25 deletions
|
|
@ -1,16 +1,10 @@
|
||||||
module Aoc.Y2020.D09
|
module Aoc.Y2020.D09 (day) where
|
||||||
( day
|
|
||||||
) where
|
|
||||||
|
|
||||||
import Data.List
|
|
||||||
|
|
||||||
import Aoc.Day
|
import Aoc.Day
|
||||||
import Aoc.Parse
|
import Aoc.Parse
|
||||||
|
import Data.List
|
||||||
|
|
||||||
parser :: Parser [Int]
|
splitAndGroup :: Int -> [a] -> [([a], a)]
|
||||||
parser = manyLines decimal
|
|
||||||
|
|
||||||
splitAndGroup :: Int -> [Int] -> [([Int], Int)]
|
|
||||||
splitAndGroup width xs = case splitAt width xs of
|
splitAndGroup width xs = case splitAt width xs of
|
||||||
(_, []) -> []
|
(_, []) -> []
|
||||||
(as, b:_) -> (as, b) : splitAndGroup width (drop 1 xs)
|
(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 :: [Int] -> Int -> Bool
|
||||||
isValid nums n = elem n $ (+) <$> nums <*> nums
|
isValid nums n = elem n $ (+) <$> nums <*> nums
|
||||||
|
|
||||||
continuousSubsequences :: [a] -> [[a]]
|
|
||||||
continuousSubsequences = filter (not . null) . concatMap tails . inits
|
|
||||||
|
|
||||||
findRanges :: Int -> [Int] -> [[Int]]
|
findRanges :: Int -> [Int] -> [[Int]]
|
||||||
findRanges target = filter ((== target) . sum) . continuousSubsequences
|
findRanges target = filter ((== target) . sum) . concatMap tails . inits
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
day :: Day
|
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