day9: add haskell solution
This commit is contained in:
parent
f946be1b04
commit
6f526a5cc3
4 changed files with 1033 additions and 0 deletions
|
@ -14,5 +14,6 @@ https://adventofcode.com/2020/
|
||||||
| 6 | | `**` | |
|
| 6 | | `**` | |
|
||||||
| 7 | `**` | | |
|
| 7 | `**` | | |
|
||||||
| 8 | | `**` | |
|
| 8 | | `**` | |
|
||||||
|
| 9 | | `**` | |
|
||||||
|
|
||||||
`test.sh` can be used to run all solutions and automatically compares them to (my) puzzle inputs and the expected outputs.
|
`test.sh` can be used to run all solutions and automatically compares them to (my) puzzle inputs and the expected outputs.
|
||||||
|
|
2
data/day9.expected
Normal file
2
data/day9.expected
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
26134589
|
||||||
|
3535124
|
1000
data/day9.input
Normal file
1000
data/day9.input
Normal file
File diff suppressed because it is too large
Load diff
30
day9/day9.hs
Normal file
30
day9/day9.hs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import AoC
|
||||||
|
|
||||||
|
import Control.Monad
|
||||||
|
import Data.List
|
||||||
|
import Data.Maybe
|
||||||
|
|
||||||
|
slidingWindow :: Int -> [a] -> [[a]]
|
||||||
|
slidingWindow n xs
|
||||||
|
| length xs < n = []
|
||||||
|
| otherwise = take n xs : slidingWindow n (tail xs)
|
||||||
|
|
||||||
|
summingTo :: (Eq a, Num a) => Int -> a -> [a] -> Maybe [a]
|
||||||
|
n `summingTo` x = find ((==x) . sum) . replicateM n
|
||||||
|
|
||||||
|
sublistSummingTo :: (Num a, Ord a) => a -> [a] -> Maybe [a]
|
||||||
|
sublistSummingTo n = fmap snd . find ((== n) . fst) . scanl go (0, [])
|
||||||
|
where go = dropNeeded .: addToAcc
|
||||||
|
addToAcc (sum, parts) x = (sum+x, parts++[x])
|
||||||
|
dropNeeded = fromJust . find ((<= n) . fst) . iterate dropPart
|
||||||
|
dropPart (sum, (x:xs)) = (sum-x, xs)
|
||||||
|
|
||||||
|
part1 :: [Int] -> Int
|
||||||
|
part1 = snd . fromJust . find (not . uncurry doesSum) . map (\xs -> (init xs, last xs)) . slidingWindow 26
|
||||||
|
where doesSum xs y = isJust $ 2 `summingTo` y $ xs
|
||||||
|
|
||||||
|
part2 :: [Int] -> Int
|
||||||
|
part2 xs = minimum range + maximum range
|
||||||
|
where range = fromJust $ sublistSummingTo (part1 xs) xs
|
||||||
|
|
||||||
|
main = runAoC (map read . lines) part1 part2
|
Loading…
Reference in a new issue