const std = @import("std"); // This one isn't as commented. Sorry for that. pub fn main() void { const input = @embedFile("input"); var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); var iter = std.mem.split(u8, input[0 .. input.len - 1], "\n"); const grid = iterCollapse([]const u8, &iter, allocator) catch unreachable; var nVisible: usize = 0; var bestPoints: usize = 0; // For each cell in the grid for (grid) |row, y| { for (row) |cell, x| { var cellVisible: bool = false; var cellPoints: usize = 1; defer { if (cellVisible) nVisible += 1; if (cellPoints > bestPoints) bestPoints = cellPoints; } // Look from the left var i: usize = x; var dirPoints: usize = 0; while (i > 0) : (i -= 1) { dirPoints += 1; if (grid[y][i - 1] >= cell) break; } else { cellVisible = true; } cellPoints *= dirPoints; // Look from the right i = x + 1; dirPoints = 0; while (i < grid[0].len) : (i += 1) { dirPoints += 1; if (grid[y][i] >= cell) break; } else { cellVisible = true; } cellPoints *= dirPoints; // Look from the top i = y; dirPoints = 0; while (i > 0) : (i -= 1) { dirPoints += 1; if (grid[i - 1][x] >= cell) break; } else { cellVisible = true; } cellPoints *= dirPoints; // Look from the bottom i = y + 1; dirPoints = 0; while (i < grid.len) : (i += 1) { dirPoints += 1; if (grid[i][x] >= cell) break; } else { cellVisible = true; } cellPoints *= dirPoints; } } print("Part 1: {}\n", .{nVisible}); print("Part 2: {}\n", .{bestPoints}); } // -------------------- UTIL -------------------- pub fn print(comptime fmt: []const u8, args: anytype) void { std.io.getStdOut().writer().print(fmt, args) catch unreachable; } pub fn iterCollapse(comptime T: type, iter: anytype, allocator: std.mem.Allocator) ![]T { var vector = std.ArrayList(T).init(allocator); if (!@hasDecl(@TypeOf(iter.*), "next")) { @compileError("Cannot collapse type " ++ @typeName(@TypeOf(iter.*)) ++ ". No .next() method."); } while (@field(iter.*, "next")()) |item| { try vector.append(item); } return vector.items; }