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