10
[aoc20.git] / 10 / one.icl
diff --git a/10/one.icl b/10/one.icl
new file mode 100644 (file)
index 0000000..cfe0e24
--- /dev/null
@@ -0,0 +1,37 @@
+module one
+
+import StdEnv
+
+read :: *File -> [Int]
+read f
+       # (ok, i, f) = freadi f
+       | not ok = []
+       = [i:read f]
+
+Start w
+       # (io, w) = stdio w
+       # ls = sort [0:read io]
+       = (one ls, two ls)
+
+one ls = uncurry (*) (foldl jolt (0, 1) [b-a\\a<-ls & b<-tl ls])
+where
+       jolt (j1, j3) 3 = (j1, j3+1)
+       jolt (j1, j3) 1 = (j1+1, j3)
+       jolt (j1, j3) _ = (j1, j3)
+
+two [a,b,c,d,e,f,g,h,i:xs]
+       | i-a <= 9 = 53 * two xs
+two [a,b,c,d,e,f,g,h:xs]
+       | h-a <= 8 = 33 * two xs
+two [a,b,c,d,e,f,g:xs]
+       | g-a <= 7 = 20 * two xs
+two [a,b,c,d,e,f:xs]
+       | f-a <= 6 = 12 * two xs
+two [a,b,c,d,e:xs]
+       | e-a <= 5 = 7 * two xs
+two [a,b,c,d:xs]
+       | d-a <= 4 = 4 * two xs
+two [a,b,c:xs]
+       | c-a <= 3 = 2 * two xs
+two [x:xs] = two xs
+two [] = 1