-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.kk
61 lines (52 loc) · 1.27 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
import std/os/file
import std/os/path
import std/text/parse
fun part1(input : list<instruction>) : int
val pos = input.foldl((0,0), fn (pos, i)
val (h, d) = pos
match i.direction
Forward -> (h + i.value, d)
Backward -> (h - i.value, d)
Up -> (h, d - i.value)
Down -> (h, d + i.value)
)
pos.fst * pos.snd
fun part2(input : list<instruction>) : int
val pos = input.foldl((0, 0, 0), fn (pos, i)
val (a, h, d) = pos
match i.direction
Forward -> (a, h + i.value, d + a * i.value)
Up -> (a - i.value, h, d)
Down -> (a + i.value, h, d)
Backward -> pos
)
pos.snd * pos.thd
type direction
Forward
Backward
Up
Down
struct instruction
direction : direction
value : int
fun pdirection() : parse direction
[
{ pstring("forward"); Forward },
{ pstring("backward"); Backward },
{ pstring("up"); Up },
{ pstring("down"); Down },
].choose()
fun pinstruction() : parse instruction
val d = pdirection()
whitespace()
val v = pint()
Instruction(d, v)
fun parse-all(input : string) : list<instruction>
input.lines.flatmap-maybe(fn (l)
l.slice.parse(pinstruction).maybe
)
fun main() {
val input = parse-all(read-text-file(path("input.txt")))
println(part1(input))
println(part2(input))
}