This commit is contained in:
Dusk 2022-12-09 21:13:38 +01:00
parent bd30807378
commit 886dd22a79
2 changed files with 155 additions and 3 deletions

View File

@ -81,11 +81,11 @@ fn visibleElemsInGrid(grid: [][]const u8) u32 {
continue :elem;
}
var row_aux: usize = row;
var col_aux: usize = col;
// Upper
{
var row_aux: usize = row;
var col_aux: usize = col;
while (col_aux > 0) : (col_aux -= 1) {
if (grid[row_aux][col_aux - 1] >= grid[row][col]) {
break;
@ -97,6 +97,8 @@ fn visibleElemsInGrid(grid: [][]const u8) u32 {
}
// Lower
{
var row_aux: usize = row;
var col_aux: usize = col;
while (col_aux < n_col - 1) : (col_aux += 1) {
if (grid[row_aux][col_aux + 1] >= grid[row][col]) {
break;
@ -108,11 +110,12 @@ fn visibleElemsInGrid(grid: [][]const u8) u32 {
}
// Left
{
var row_aux: usize = row;
var col_aux: usize = col;
while (row_aux > 0) : (row_aux -= 1) {
if (grid[row_aux - 1][col_aux] >= grid[row][col]) {
break;
}
if (row_aux == 0) break;
} else {
visible += 1;
continue: elem;
@ -120,6 +123,8 @@ fn visibleElemsInGrid(grid: [][]const u8) u32 {
}
// Right
{
var row_aux: usize = row;
var col_aux: usize = col;
while (row_aux < n_row - 1) : (row_aux += 1) {
if (grid[row_aux + 1][col_aux] >= grid[row][col]) {
break;

147
day8/puzzle2.zig Normal file
View File

@ -0,0 +1,147 @@
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 max_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) {
var visible: u32 = 0;
var visible_u: u32 = 0;
var visible_d: u32 = 0;
var visible_l: u32 = 0;
var visible_r: u32 = 0;
defer {
if (visible > max_visible) {
max_visible = visible;
}
}
// Edges will always be 0
if (
col == 0 or
col == n_col - 1 or
row == 0 or
row == n_row - 1
) {
visible = 0;
continue: elem;
}
// Upper
{
var row_aux: usize = row;
var col_aux: usize = col;
while (col_aux > 0) : (col_aux -= 1) {
visible_u += 1;
if (grid[row_aux][col_aux - 1] >= grid[row][col]) {
break;
}
}
}
// Lower
{
var row_aux: usize = row;
var col_aux: usize = col;
while (col_aux < n_col - 1) : (col_aux += 1) {
visible_d += 1;
if (grid[row_aux][col_aux + 1] >= grid[row][col]) {
break;
}
}
}
// Left
{
var row_aux: usize = row;
var col_aux: usize = col;
while (row_aux > 0) : (row_aux -= 1) {
visible_l += 1;
if (grid[row_aux - 1][col_aux] >= grid[row][col]) {
break;
}
}
}
// Right
{
var row_aux: usize = row;
var col_aux: usize = col;
while (row_aux < n_row - 1) : (row_aux += 1) {
visible_r += 1;
if (grid[row_aux + 1][col_aux] >= grid[row][col]) {
break;
}
}
}
visible = visible_u * visible_d * visible_l * visible_r;
}
col = 0;
}
return max_visible;
}