Solve day 8 part 2
This commit is contained in:
parent
cec001ece8
commit
1afee9d007
103
src/day08.zig
103
src/day08.zig
@ -11,18 +11,32 @@ const MAX_Y = 99;
|
|||||||
|
|
||||||
const Grid = struct {
|
const Grid = struct {
|
||||||
grid: [MAX_Y][MAX_X]u8,
|
grid: [MAX_Y][MAX_X]u8,
|
||||||
|
size: usize,
|
||||||
fn new() Grid {
|
fn new() Grid {
|
||||||
return Grid{
|
return Grid{
|
||||||
.grid = [_][MAX_X]u8{[_]u8{0} ** MAX_X} ** MAX_Y,
|
.grid = [_][MAX_X]u8{[_]u8{0} ** MAX_X} ** MAX_Y,
|
||||||
|
.size = MAX_Y,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
fn is_tree_visible(self: Grid, size: usize, x: usize, y: usize) bool {
|
fn parse(data: []const u8) !Grid {
|
||||||
|
var grid = Grid.new();
|
||||||
|
var lines = std.mem.tokenize(u8, data, "\n");
|
||||||
|
var y: usize = 0;
|
||||||
|
while (lines.next()) |line| : (y += 1) {
|
||||||
|
var x: usize = 0;
|
||||||
|
grid.size = line.len;
|
||||||
|
while (x < line.len) : (x += 1) {
|
||||||
|
const height = try std.fmt.parseInt(u8, line[x .. x + 1], 10);
|
||||||
|
grid.grid[y][x] = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
fn is_tree_visible(self: Grid, x: usize, y: usize) bool {
|
||||||
const height = self.grid[y][x];
|
const height = self.grid[y][x];
|
||||||
// std.debug.print("testing: {d}, {d}: {d}\n", .{ x, y, height });
|
|
||||||
var up_y = y - 1;
|
var up_y = y - 1;
|
||||||
while (true) : (up_y -= 1) {
|
while (true) : (up_y -= 1) {
|
||||||
if (self.grid[up_y][x] >= height) {
|
if (self.grid[up_y][x] >= height) {
|
||||||
// std.debug.print("up_y fail: {d}, {d}: {d}\n", .{ x, up_y, height });
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (up_y == 0) return true;
|
if (up_y == 0) return true;
|
||||||
@ -30,53 +44,64 @@ const Grid = struct {
|
|||||||
var left_x = x - 1;
|
var left_x = x - 1;
|
||||||
while (true) : (left_x -= 1) {
|
while (true) : (left_x -= 1) {
|
||||||
if (self.grid[y][left_x] >= height) {
|
if (self.grid[y][left_x] >= height) {
|
||||||
// std.debug.print("left_x fail: {d}, {d}: {d}\n", .{ left_x, y, height });
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (left_x == 0) return true;
|
if (left_x == 0) return true;
|
||||||
}
|
}
|
||||||
var down_y = y + 1;
|
var down_y = y + 1;
|
||||||
while (down_y < size) : (down_y += 1) {
|
while (down_y < self.size) : (down_y += 1) {
|
||||||
if (self.grid[down_y][x] >= height) {
|
if (self.grid[down_y][x] >= height) {
|
||||||
// std.debug.print("down_y fail: {d}, {d}: {d}\n", .{ x, down_y, height });
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (down_y == size - 1) return true;
|
if (down_y == self.size - 1) return true;
|
||||||
}
|
}
|
||||||
var right_x = x + 1;
|
var right_x = x + 1;
|
||||||
while (right_x < size) : (right_x += 1) {
|
while (right_x < self.size) : (right_x += 1) {
|
||||||
if (self.grid[y][right_x] >= height) {
|
if (self.grid[y][right_x] >= height) {
|
||||||
// std.debug.print("right_x fail: {d}, {d}: {d}\n", .{ right_x, y, height });
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (right_x == size - 1) return true;
|
if (right_x == self.size - 1) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
fn tree_score(self: Grid, x: usize, y: usize) usize {
|
||||||
|
const height = self.grid[y][x];
|
||||||
|
var up_y: usize = 1;
|
||||||
|
while (y - up_y > 0) : (up_y += 1) {
|
||||||
|
if (self.grid[y - up_y][x] >= height) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var left_x: usize = 1;
|
||||||
|
while (x - left_x > 0) : (left_x += 1) {
|
||||||
|
if (self.grid[y][x - left_x] >= height) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var down_y: usize = 1;
|
||||||
|
while (y + down_y < self.size - 1) : (down_y += 1) {
|
||||||
|
if (self.grid[y + down_y][x] >= height) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var right_x: usize = 1;
|
||||||
|
while (x + right_x < self.size - 1) : (right_x += 1) {
|
||||||
|
if (self.grid[y][x + right_x] >= height) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return up_y * left_x * down_y * right_x;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn solve_part1(data: []const u8) !usize {
|
pub fn solve_part1(data: []const u8) !usize {
|
||||||
var lines = std.mem.tokenize(u8, data, "\n");
|
var grid = try Grid.parse(data);
|
||||||
var grid = Grid.new();
|
var count: usize = (grid.size * 4) - 4;
|
||||||
var size: usize = 0;
|
var y: usize = 1;
|
||||||
var y: usize = 0;
|
while (y < grid.size - 1) : (y += 1) {
|
||||||
while (lines.next()) |line| : (y += 1) {
|
|
||||||
var x: usize = 0;
|
|
||||||
size = line.len;
|
|
||||||
std.debug.print("line.len: {d}\n", .{line.len});
|
|
||||||
while (x < line.len) : (x += 1) {
|
|
||||||
const height = try std.fmt.parseInt(u8, line[x .. x + 1], 10);
|
|
||||||
grid.grid[y][x] = height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var count: usize = (size * 4) - 4;
|
|
||||||
std.debug.print("count: {d}\n", .{count});
|
|
||||||
y = 1;
|
|
||||||
while (y < size - 1) : (y += 1) {
|
|
||||||
var x: usize = 1;
|
var x: usize = 1;
|
||||||
while (x < size - 1) : (x += 1) {
|
while (x < grid.size - 1) : (x += 1) {
|
||||||
if (grid.is_tree_visible(size, x, y)) {
|
if (grid.is_tree_visible(x, y)) {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,9 +110,19 @@ pub fn solve_part1(data: []const u8) !usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_part2(data: []const u8) !usize {
|
pub fn solve_part2(data: []const u8) !usize {
|
||||||
var lines = std.mem.tokenize(u8, data, "\n");
|
var grid = try Grid.parse(data);
|
||||||
_ = lines;
|
var best_score: usize = 0;
|
||||||
return 0;
|
var y: usize = 1;
|
||||||
|
while (y < grid.size - 1) : (y += 1) {
|
||||||
|
var x: usize = 1;
|
||||||
|
while (x < grid.size - 1) : (x += 1) {
|
||||||
|
const score = grid.tree_score(x, y);
|
||||||
|
if (score > best_score) {
|
||||||
|
best_score = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return best_score;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "solves part1" {
|
test "solves part1" {
|
||||||
@ -95,5 +130,5 @@ test "solves part1" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test "solves part2" {
|
test "solves part2" {
|
||||||
try std.testing.expectEqual(solve_part2(test_input1), 0);
|
try std.testing.expectEqual(solve_part2(test_input1), 8);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user