-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ha
82 lines (68 loc) · 1.68 KB
/
part1.ha
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
use fmt;
use io;
use os;
use strings;
fn read_map() [][]u8 = {
const file = os::open("input.txt")!;
defer io::close(file)!;
const lines = strings::split(strings::fromutf8(io::drain(file)!)!, "\n");
let rows: [][]u8 = [];
for (const line .. lines) {
if (len(line) == 0)
continue;
append(rows, strings::toutf8(line));
};
return rows;
};
type antenna = struct {
x: int,
y: int,
};
type antennas = struct {
groups: [128][]antenna,
};
fn extract_antennas(map: [][]u8) antennas = {
let antennas = antennas{...};
for (let y = 0z; y < len(map); y += 1) {
for (let x = 0z; x < len(map[0]); x += 1) {
if (map[y][x] == '.')
continue;
append(antennas.groups[map[y][x]], antenna{x = x: int, y = y: int});
};
};
return antennas;
};
fn is_in_bounds(map: [][]u8, x: int, y: int) bool = {
return x >= 0 && x < (len(map[0]): int) && y >= 0 && y < (len(map): int);
};
fn antinode_at(map: [][]u8, x: int, y: int) int = {
if (is_in_bounds(map, x, y) && map[y][x] != '#') {
map[y][x] = '#';
return 1;
};
return 0;
};
fn test_pair(map: [][]u8, a: antenna, b: antenna) int = {
let dx = (b.x - a.x);
let dy = (b.y - a.y);
return antinode_at(map, a.x - dx, a.y - dy) + antinode_at(map, b.x + dx, b.y + dy);
};
fn test_combos(map: [][]u8, group: []antenna) int = {
let sum = 0;
for (let i = 0z; i < len(group); i += 1) {
for (let j = i + 1; j < len(group); j += 1)
sum += test_pair(map, group[i], group[j]);
};
return sum;
};
export fn main() void = {
const map = read_map();
const antennas = extract_antennas(map);
let sum = 0;
for (const group .. antennas.groups) {
if (len(group) == 0)
continue;
sum += test_combos(map, group);
};
fmt::printfln("{}", sum)!;
};