-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.c3
60 lines (51 loc) · 1.35 KB
/
part1.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
module part1;
import std::io;
import std::collections::list;
def Row = List(<int>);
def Grid = List(<Row>);
fn String[] read_input() {
return ((String)io::file::load_new("input.txt")!!).strip_end("\n").split("\n");
}
fn Grid create_grid(usz w, usz h) {
Grid grid;
grid.new_init(h);
for (usz y = 0; y < h; ++ y) {
Row row;
row.new_init(w);
for (usz x = 0; x < w; ++ x) {
row.push(0);
}
grid.push(row);
}
return grid;
}
fn void flood_fill(String[] lines, Grid* grid, usz* area, usz* perimeter,
int id, char ch, int sx, int sy) {
if (sx < 0 || sx >= lines[0].len || sy < 0 || sy >= lines.len || lines[sy][sx] != ch) {
++ *perimeter;
return;
} else if ((*grid)[sy][sx] != 0) return;
(*grid)[sy][sx] = id;
++ *area;
int[*][*] dirs = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
foreach (dir : dirs) {
int x = sx + dir[0];
int y = sy + dir[1];
flood_fill(lines, grid, area, perimeter, id, ch, x, y);
}
}
fn void main() {
String[] lines = read_input();
Grid grid = create_grid(lines[0].len, lines.len);
int next_id = 1;
usz sum = 0;
for (usz y = 0; y < lines.len; ++ y) {
for (usz x = 0; x < lines[0].len; ++ x) {
if (grid[y][x] != 0) continue;
usz area, perimeter;
flood_fill(lines, &grid, &area, &perimeter, next_id ++, lines[y][x], (int)x, (int)y);
sum += area * perimeter;
}
}
io::printn(sum);
}