-
Notifications
You must be signed in to change notification settings - Fork 1
/
day9.c3
92 lines (84 loc) · 2.05 KB
/
day9.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
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
92
/*
* Advent of Code 2023 day 9
*/
import std::io;
import std::time;
fn void main()
{
io::printn("Advent of code, day 9.");
@pool()
{
// Simple benchmarking with Clock, "mark" returns the last duration and resets the clock
Clock c = clock::now();
io::printfn("* Steps part 1: %d - completed in %s", part1(), c.mark());
io::printfn("* Steps part 2: %d - completed in %s", part2(), c.mark());
};
}
fn long solve1(int[] vals)
{
// We need a buffer to hold the new values.
int[30] res;
isz len = vals.len - 1;
bool is_zero = true;
// Calculate the new row (fairly inefficient, we could vectorize this for example)
for (isz i = 0; i < len; i++)
{
if ((res[i] = vals[i + 1] - vals[i]) != 0) is_zero = false;
}
// If we have all zeros, then the value is the left hand side + 0
if (is_zero) return vals[len];
// Otherwise it is the previous interpolated value + the left hand side.
return solve1(res[:len]) + vals[len];
}
fn long part1()
{
File f = file::open("day9.txt", "rb")!!;
defer (void)f.close();
// We can use a dynamic array, or just reuse a slice of
// a buffer.
int[100] buffer;
long sum;
while (try line = io::treadline(&f))
{
// Parse the line.
String[] nums = line.tsplit(" ");
foreach (i, num : nums)
{
buffer[i] = num.to_int()!!;
}
// Solve and add to the sum.
sum += solve1(buffer[:nums.len]);
}
return sum;
}
fn long solve2(int[] vals)
{
// This solve is almost identical to solve 1
int[30] res;
isz len = vals.len - 1;
bool is_zero = true;
for (isz i = 0; i < len; i++)
{
if ((res[i] = vals[i + 1] - vals[i]) != 0) is_zero = false;
}
// Rather than getting it from the end, let's return the first value, and change the sign of the solve.
if (is_zero) return vals[0];
return -solve2(res[:len]) + vals[0];
}
fn long part2()
{
File f = file::open("day9.txt", "rb")!!;
defer (void)f.close();
int[100] buffer;
long sum;
while (try line = io::treadline(&f))
{
String[] nums = line.tsplit(" ");
foreach (i, num : nums)
{
buffer[i] = num.to_int()!!;
}
sum += solve2(buffer[:nums.len]);
}
return sum;
}