brute 9 solution
[aoc20.git] / 9 / one.icl
1 module one
2
3 import StdEnv
4 import Data.List
5 import StdMaybe
6
7 read :: *File -> [Int]
8 read f
9 # (ok, i, f) = freadi f
10 | not ok = []
11 = [i:read f]
12
13 Start w
14 # (io, w) = stdio w
15 # ls = read io
16 = (one ls, two ls)
17
18 one = last o pre
19
20 pre l
21 # (preamble, [num:rest]) = splitAt 25 l
22 | isEmpty [()\\a<-preamble, b<-preamble | a + b == num] = [num]
23 = [num:pre (tl l)]
24
25 two ls
26 # ls = fst (splitAt 25 ls) ++ pre ls
27 # a = sort (hd (calc (last ls) ls))
28 = (last a + hd a)
29 where
30 // calc :: Int [Int] -> (Int, Int)
31 calc num xs = hd (filter (not o isEmpty) [calc` num l\\l<-tails xs])
32
33 calc` num xs = filter ((==)num o sum) (dropWhile ((<) num o sum) (inits xs))