-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.c3
94 lines (84 loc) · 2.15 KB
/
day8.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
import std::io;
import std::time;
import std::collections;
import std::math;
const String FILE = "input.txt";
const int SIZE = 50;
char[SIZE][SIZE] map;
bool[SIZE][SIZE] antinodes;
def TotalFn = fn int();
def AntinodeFn = fn void(int, int, int, int, char);
fn void main()
{
@pool() {
Clock c = clock::now();
io::printfn("- Part1: %d - %s", solve(&adjacent, &unique)!!, c.mark());
io::printfn("- Part2: %d - %s", solve(&rays, &unique_with_same_locaiton)!!, c.mark());
};
}
fn long! solve(AntinodeFn find_antinodes, TotalFn total)
{
File f = file::open(FILE, "r")!;
defer (void)f.close();
int row = 0;
while (try line = io::treadline(&f)) {
foreach(col, ch : line) {
map[row][col] = ch;
}
row++;
}
for(int y = 0; y < SIZE; y++) {
for(int x = 0; x < SIZE; x++) {
char this = map[y][x];
if (this == '.') continue;
for (int y1 = y; y1 < SIZE; y1++) {
for (int x1 = 0; x1 < SIZE; x1++) {
if (x1 <= x && y1 == y) continue;
if (map[y1][x1] == this) {
find_antinodes(x1+x1-x, y1+y1-y, x1-x, y1-y, this);
find_antinodes(x+x-x1, y+y-y1, x-x1, y-y1, this);
}
}
}
}
}
return total();
}
fn int unique()
{
int total = 0;
for(int y = 0; y < SIZE; y++) {
for(int x = 0; x < SIZE; x++) {
if (antinodes[y][x]) total++;
}
}
return total;
}
fn int unique_with_same_locaiton()
{
int total = 0;
for(int y = 0; y < SIZE; y++) {
for(int x = 0; x < SIZE; x++) {
if (antinodes[y][x] || map[y][x] != '.') total++;
}
}
return total;
}
fn void adjacent(int x, int y, int dx, int dy, char c)
{
if (x>=0 && x<SIZE && y>=0 && y<SIZE) {
if (map[y][x] != c) {
antinodes[y][x] = true;
}
}
}
fn void rays(int x, int y, int dx, int dy, char c)
{
while(x>=0 && x<SIZE && y>=0 && y<SIZE) {
if (map[y][x] != c) {
antinodes[y][x] = true;
}
x+=dx;
y+=dy;
}
}