2022-12-09 12:29:40 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const input = @embedFile("input");
|
|
|
|
|
|
|
|
const stdout = std.io.getStdOut().writer();
|
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
const allocator = arena.allocator();
|
|
|
|
|
|
|
|
const grid = textToGrid(input, allocator);
|
|
|
|
|
|
|
|
// for (grid) |row| {
|
|
|
|
// stdout.print("{s}\n", .{row}) catch unreachable;
|
|
|
|
// }
|
|
|
|
|
|
|
|
const visible = visibleElemsInGrid(grid);
|
|
|
|
|
|
|
|
stdout.print("{}\n", .{visible}) catch unreachable;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn textToGrid(text: []const u8, allocator: Allocator) [][]const u8 {
|
|
|
|
|
|
|
|
var iterator = std.mem.split(u8, text, "\n");
|
|
|
|
|
|
|
|
const n_col = iterator.first().len;
|
|
|
|
const n_row = iterator.buffer.len / (n_col + 1);
|
|
|
|
|
|
|
|
// Need to do this to start at the beggining
|
|
|
|
iterator.reset();
|
|
|
|
|
|
|
|
// Creating and allocating the grid
|
|
|
|
var grid: [][]const u8 = allocator.alloc([]const u8, n_row) catch unreachable;
|
|
|
|
for (grid) |*row| {
|
|
|
|
row.* = allocator.alloc(u8, n_col) catch unreachable;
|
|
|
|
}
|
|
|
|
|
|
|
|
//stdout.print("Width: {}\n", .{n_col}) catch unreachable;
|
|
|
|
//stdout.print("Height: {}\n", .{n_row}) catch unreachable;
|
|
|
|
|
|
|
|
|
|
|
|
// Filling the grid
|
|
|
|
var i: usize = 0;
|
|
|
|
while (iterator.next()) |row| : (i += 1) {
|
|
|
|
if (row.len == 0) continue;
|
|
|
|
grid[i] = row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return grid;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visibleElemsInGrid(grid: [][]const u8) u32 {
|
|
|
|
|
|
|
|
var visible: u32 = 0;
|
|
|
|
|
|
|
|
const n_row = grid.len;
|
|
|
|
const n_col = grid[0].len;
|
|
|
|
|
|
|
|
var row: usize = 0;
|
|
|
|
var col: usize = 0;
|
|
|
|
|
|
|
|
// For each element of the grid
|
|
|
|
while (row < n_row) : (row += 1) {
|
|
|
|
elem: while (col < n_col) : (col += 1) {
|
|
|
|
|
|
|
|
// Edges are always visible
|
|
|
|
if (
|
|
|
|
col == 0 or
|
|
|
|
col == n_col - 1 or
|
|
|
|
row == 0 or
|
|
|
|
row == n_row - 1
|
|
|
|
) {
|
|
|
|
visible += 1;
|
|
|
|
continue :elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Upper
|
|
|
|
{
|
2022-12-09 20:13:38 +00:00
|
|
|
var row_aux: usize = row;
|
|
|
|
var col_aux: usize = col;
|
2022-12-09 12:29:40 +00:00
|
|
|
while (col_aux > 0) : (col_aux -= 1) {
|
|
|
|
if (grid[row_aux][col_aux - 1] >= grid[row][col]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
visible += 1;
|
|
|
|
continue: elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Lower
|
|
|
|
{
|
2022-12-09 20:13:38 +00:00
|
|
|
var row_aux: usize = row;
|
|
|
|
var col_aux: usize = col;
|
2022-12-09 12:29:40 +00:00
|
|
|
while (col_aux < n_col - 1) : (col_aux += 1) {
|
|
|
|
if (grid[row_aux][col_aux + 1] >= grid[row][col]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
visible += 1;
|
|
|
|
continue: elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Left
|
|
|
|
{
|
2022-12-09 20:13:38 +00:00
|
|
|
var row_aux: usize = row;
|
|
|
|
var col_aux: usize = col;
|
2022-12-09 12:29:40 +00:00
|
|
|
while (row_aux > 0) : (row_aux -= 1) {
|
|
|
|
if (grid[row_aux - 1][col_aux] >= grid[row][col]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
visible += 1;
|
|
|
|
continue: elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Right
|
|
|
|
{
|
2022-12-09 20:13:38 +00:00
|
|
|
var row_aux: usize = row;
|
|
|
|
var col_aux: usize = col;
|
2022-12-09 12:29:40 +00:00
|
|
|
while (row_aux < n_row - 1) : (row_aux += 1) {
|
|
|
|
if (grid[row_aux + 1][col_aux] >= grid[row][col]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
visible += 1;
|
|
|
|
continue: elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return visible;
|
|
|
|
}
|