-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.kk
86 lines (65 loc) · 1.79 KB
/
run.kk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import std/os/file
import std/os/path
import std/text/parse
fun lshow(xs: list<list<int>>)
xs.show-list(fn (l) l.show-list(show))
fun most-common-bin(xs: list<int>): int
val s = xs.sum()
val l = xs.length()
if 2 * s >= l then 1
else 0
fun least-common-bin(xs: list<int>): int
if xs.most-common-bin() == 0 then 1
else 0
fun counts(xs : list<list<int>>): <exn> list<int>
val cs = xs.foldl1 fn (a, b)
a.zipwith(b, (+))
val l = xs.length()
cs.map fn (v)
if 2 * v >= l then 1
else 0
fun toint2(xs: list<int>): int
xs.reverse.map-indexed(fn (i, v) v * exp2(i)).sum()
fun flip(xs: list<int>): list<int>
xs.map fn (v)
if v == 1 then 0
else 1
fun part1(input)
val cs = counts(input)
val gamma = cs.toint2()
val epsilon = cs.flip().toint2()
gamma * epsilon
fun locate(xs: list<list<int>>, selector: (list<int> -> int))
fun locate'(remaining: list<list<int>>): <div> list<int>
match remaining
Nil -> Nil
Cons(x, Nil) -> x
_ ->
val v = remaining.flatmap-maybe(head).selector()
val rest = remaining.flatmap-maybe fn (row)
match row
Cons(_, Nil) -> Nothing
Cons(h, tail) -> if h == v then Just(tail)
else Nothing
Nil -> Nothing
Cons(v, rest.locate'())
xs.locate'()
fun part2(input)
val o2 = input.locate(most-common-bin).toint2()
val co2 = input.locate(least-common-bin).toint2()
o2 * co2
fun pline()
many(psingle)
fun psingle()
val c = one-of("01")
if c == '0' then 0
else 1
fun parse-all(input : string): list<list<int>>
input.lines.flatmap-maybe(fn (l)
l.slice.parse(pline).maybe
).filter(is-cons)
fun main() {
val input = parse-all(read-text-file(path("input.txt")))
println(part1(input))
println(part2(input))
}