-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.c3
107 lines (98 loc) · 3.09 KB
/
day4.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import std::io;
import std::time;
const int X_LEN = 140; // 140
const int Y_LEN = 140; // 140
fn long! part1()
{
String input = (String) file::load_temp("input.txt")!;
char[X_LEN][Y_LEN] matrix;
usz dy = 0;
foreach (i, ch: input) {
if (ch == '\n') { dy++; }
else { matrix[i % (X_LEN+1)][dy] = ch; }
}
DString ds;
usz count = 0;
for (int y = 0; y < Y_LEN; y++) {
for (int x = 0; x < X_LEN; x++) {
if (x+3 < X_LEN) {
ds.append_char(matrix[x][y]);
ds.append_char(matrix[x+1][y]);
ds.append_char(matrix[x+2][y]);
ds.append_char(matrix[x+3][y]);
if(ds.str_view() == "XMAS" || ds.str_view() == "SAMX") {
count++;
}
ds.clear();
}
if (y+3 < Y_LEN) {
ds.append_char(matrix[x][y]);
ds.append_char(matrix[x][y+1]);
ds.append_char(matrix[x][y+2]);
ds.append_char(matrix[x][y+3]);
if(ds.str_view() == "XMAS" || ds.str_view() == "SAMX") {
count++;
}
ds.clear();
}
if (y+3 <Y_LEN && x+3 < X_LEN) {
ds.append_char(matrix[x][y]);
ds.append_char(matrix[x+1][y+1]);
ds.append_char(matrix[x+2][y+2]);
ds.append_char(matrix[x+3][y+3]);
if(ds.str_view() == "XMAS" || ds.str_view() == "SAMX") {
count++;
}
ds.clear();
}
if (x+3 < X_LEN && y-3 >= 0) {
ds.append_char(matrix[x][y]);
ds.append_char(matrix[x+1][y-1]);
ds.append_char(matrix[x+2][y-2]);
ds.append_char(matrix[x+3][y-3]);
if(ds.str_view() == "XMAS" || ds.str_view() == "SAMX") {
count++;
}
ds.clear();
}
}
}
return count;
}
fn long! part2()
{
String input = (String) file::load_temp("input.txt")!;
char[X_LEN][Y_LEN] matrix;
usz dy = 0;
foreach (i, ch: input) {
if (ch == '\n') { dy++; }
else { matrix[i % (X_LEN+1)][dy] = ch; }
}
DString da;
DString db;
usz count = 0;
for (int y = 1; y < Y_LEN - 1; y++) {
for (int x = 1; x < X_LEN - 1; x++) {
if (matrix[x][y] == 'A') {
da.append_char(matrix[x-1][y-1]);
da.append_char(matrix[x+1][y+1]);
db.append_char(matrix[x-1][y+1]);
db.append_char(matrix[x+1][y-1]);
if (da.str_view() == "MS" || da.str_view() == "SM") {
if (db.str_view() == "MS" || db.str_view() == "SM") {
count++;
}
}
}
da.clear();
db.clear();
}
}
return count;
}
fn void main()
{
Clock c = clock::now();
io::printfn("- Part1: %d - %s", part1()!!, c.mark());
io::printfn("- Part2: %d - %s", part2()!!, c.mark());
}