-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.c3
86 lines (81 loc) · 1.67 KB
/
day2.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
import std::io;
import std::time;
fn bool int[].steady_inc(self, int prev, int skip)
{
bool is_inc = self[0] > prev;
if (self[0] - prev == 0) return false;
for(int i = 0; i < self.len; i++) {
if (skip == i) continue;
int diff = self[i] - prev;
if (is_inc != (diff >= 1 && diff <= 3)) return false;
if (!is_inc != (diff <= -1 && diff >= -3)) return false;
prev = self[i];
}
return true;
}
fn bool int[].is_safe(self, int skip)
{
if (skip == 0) {
skip = -1;
self = self[1..];
}
int prev = self[0];
if (skip == 1) {
self = self[1..];
skip = -1;
}
self = self[1..];
skip--;
return self.steady_inc(prev, skip);
}
fn int! part1()
{
File f = file::open("input.txt", "rb")!;
defer (void)f.close();
int total = 0;
while (try line = io::treadline(&f))
{
String[] split = line.tsplit(" ");
int[10] buf;
for (int i = 0; i < split.len; i++) {
buf[i] = split[i].to_int()!!;
}
int[] val = buf[:split.len];
if (val.is_safe(-1)) {
total++;
}
}
return total;
}
fn int! part2()
{
File f = file::open("input.txt", "rb")!;
defer (void)f.close();
int total = 0;
while (try line = io::treadline(&f))
{
String[] split = line.tsplit(" ");
int[10] buf;
for (int i = 0; i < split.len; i++) {
buf[i] = split[i].to_int()!!;
}
int[] val = buf[:split.len];
if (val.is_safe(-1)) {
total++;
continue;
}
for (int i = val.len; i >= 0; i--) {
if (val.is_safe(i)) {
total++;
break;
}
}
}
return total;
}
fn void main()
{
Clock c = clock::now();
io::printfn("- Part1: %d - %s", part1()!!, c.mark());
io::printfn("- Part2: %d - %s", part2()!!, c.mark());
}