-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.kk
93 lines (68 loc) · 1.99 KB
/
day13.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
87
88
89
90
91
import std/os/file
import std/os/path
import std/text/regex
val log-enabled = False
val input-filename = "input/d13.txt"
effect fun log(msg: string) : ()
fun main()
with fun log(msg: string)
if log-enabled then console/string/println(msg)
val input = parse-input(read-text-file(path(input-filename)))
log("Input " ++ input.show)
part1(input)
part2(input)
fun part1(input: list<problem>)
val total = input
.filter-map(solve)
.map(fn((a, b)) a * 3 + b)
.sum
println("Part 1: " ++ total.show)
fun part2(input: list<problem>)
val input' = input.map fn(p)
p(xp = p.xp + 10000000000000, yp = p.yp + 10000000000000)
val total = input'
.filter-map(solve)
.map(fn((a, b)) a * 3 + b)
.sum
println("Part 2: " ++ total.show)
fun parse-input(input: string) : exn list<problem>
val r = regex(r"X.(\d+).*Y.(\d+)")
with prob <- input.trim().split("\n\n").map
val Cons(a-line, Cons(b-line, Cons(p-line, Nil))) = prob.split("\n")
val a = r.exec(a-line).vector
val b = r.exec(b-line).vector
val p = r.exec(p-line).vector
fun slice2int(s: sslice)
s.string.parse-int.unjust
Problem(
xa = slice2int(a[1]),
ya = slice2int(a[2]),
xb = slice2int(b[1]),
yb = slice2int(b[2]),
xp = slice2int(p[1]),
yp = slice2int(p[2])
)
struct problem
xa : int
ya : int
xb : int
yb : int
xp : int
yp : int
fun problem/show(p: problem) : string
(
"{" ++
"xa=" ++ p.xa.show ++
", ya=" ++ p.ya.show ++
", xb=" ++ p.xb.show ++
", yb=" ++ p.yb.show ++
", xp=" ++ p.xp.show ++
", yp=" ++ p.yp.show ++
"}"
)
fun solve(p: problem) : maybe<(int, int)>
val (b, m) = divmod((p.xa * p.yp - p.ya * p.xp), (p.xa * p.yb - p.xb * p.ya))
if m != 0 then return Nothing
val (a, m2) = divmod(p.xp - p.xb * b, p.xa)
if m2 != 0 then return Nothing
Just((a, b))