1
0
Fork 0

dnf: day 9

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:52 -04:00
parent 91be41de8a
commit 8b6a1e0ce8
6 changed files with 1049 additions and 1 deletions

1
2020/09/README.md Normal file
View file

@ -0,0 +1 @@
# Day 9

4
2020/09/day9.hs Normal file
View file

@ -0,0 +1,4 @@
module Day9 where
parse :: String -> [Int]
parse = map read . lines

1000
2020/09/input.txt Normal file

File diff suppressed because it is too large Load diff

22
2020/09/part1.hs Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env runghc
import qualified Data.Set as S
import Data.List (subsequences)
import Data.Either (fromLeft, lefts)
import Day9
main = interact (show . solve . parse)
solve = head . lefts . verify 25
verify :: Int -> [Int] -> [Either Int Int]
verify len xs
| length xs < len + 1 = []
| otherwise = verified:(verify len $ tail xs)
where
(preamble, [x]) = splitAt len $ take (len + 1) xs
pairs = combinations 2 preamble
sums = S.fromList $ map sum pairs
verified = if S.member x sums then (Right x) else (Left x)
combinations n xs = filter ((n==) . length) $ subsequences xs

20
2020/09/small.txt Normal file
View file

@ -0,0 +1,20 @@
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576

View file

@ -15,7 +15,7 @@ asdf install
| S | M | T | W | T | F | S | | S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | [1] | [2] | [3] | [4] | [5] | | | | [1] | [2] | [3] | [4] | [5] |
| [6] | [7] | [8] | 9 | 10 | 11 | 12 | | [6] | [7] | [8] | [9] | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 | 25 | | | | 21 | 22 | 23 | 24 | 25 | | |
@ -29,3 +29,4 @@ asdf install
[6]: ./06 [6]: ./06
[7]: ./07 [7]: ./07
[8]: ./08 [8]: ./08
[9]: ./09