-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.c3
53 lines (45 loc) · 1.47 KB
/
day13.c3
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
import std::io, std::time, std::collections, std::math;
fn void main()
{
@pool() {
Clock c = clock::now();
io::printfn("- Part1: %d - %s", part1()!!, c.mark());
io::printfn("- Part2: %d - %s", part2()!!, c.mark());
};
}
def Pos = isz[<2>];
struct Machine {
Pos a;
Pos b;
Pos price;
}
fn long! part1() => solve(0);
fn long! part2() => solve(10000000000000);
fn long! solve(isz offset)
{
String input = (String) file::load_temp("input.txt")!;
String[] machines = input.trim().tsplit("\n\n");
usz sum;
foreach(machine: machines) {
Machine m = from(machine, offset);
// Cramers rule
usz a = (m.price.x*m.b.y - m.price.y*m.b.x) / (m.a.x*m.b.y - m.a.y*m.b.x);
usz b = (m.a.x*m.price.y - m.a.y*m.price.x) / (m.a.x*m.b.y - m.a.y*m.b.x);
if (m.price.x == m.a.x * a + m.b.x * b && m.price.y == m.a.y * a + m.b.y * b) {
sum += 3*a + b;
}
}
return sum;
}
fn Machine from(String input, isz offset)
{
String[] parts = input.tsplit("\n");
String[] a = ((String) parts[0][9..].trim()).tsplit(", ");
String[] b = ((String) parts[1][9..].trim()).tsplit(", ");
String[] price = ((String) parts[2][6..].trim()).tsplit(", ");
return Machine {
.a = { a[0][1..].to_long()!!, a[1][1..].to_long()!! },
.b = { b[0][1..].to_long()!!, b[1][1..].to_long()!! },
.price = { price[0][2..].to_long()!! + offset, price[1][2..].to_long()!! + offset }
};
}