This commit is contained in:
parent
b9bff3998e
commit
0b6eda2892
@ -14,41 +14,57 @@ const Solution = struct {
|
|||||||
b: usize,
|
b: usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
fn northWeight(alloc: mem.Allocator, grid: [][]u8) !usize {
|
||||||
const width = mem.indexOfScalar(u8, input, '\n').?;
|
var next_slot = try alloc.alloc(u32, grid[0].len);
|
||||||
var height = mem.count(u8, input, &[_]u8{'\n'});
|
|
||||||
if (input[input.len - 1] != '\n') height += 1;
|
|
||||||
|
|
||||||
var next_slot = try alloc.alloc(u32, width);
|
|
||||||
defer alloc.free(next_slot);
|
defer alloc.free(next_slot);
|
||||||
@memset(next_slot, 0);
|
@memset(next_slot, 0);
|
||||||
|
|
||||||
var weight: usize = 0;
|
var weight: usize = 0;
|
||||||
|
for (grid, 0..) |line, row| {
|
||||||
|
for (line, 0..) |char, col| {
|
||||||
|
switch (char) {
|
||||||
|
'O' => {
|
||||||
|
weight += grid.len - next_slot[col];
|
||||||
|
next_slot[col] += 1;
|
||||||
|
},
|
||||||
|
'#' => next_slot[col] = @as(u32, @intCast(row)) + 1,
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spin(grid: [][]u8, n: usize) void {
|
||||||
|
_ = grid;
|
||||||
|
for (0..n) |_| {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
||||||
|
var height = mem.count(u8, input, &[_]u8{'\n'});
|
||||||
|
if (input[input.len - 1] != '\n') height += 1;
|
||||||
|
|
||||||
|
var grid = try alloc.alloc([]u8, height);
|
||||||
|
defer alloc.free(grid);
|
||||||
|
|
||||||
var it = util.splitLines(input);
|
var it = util.splitLines(input);
|
||||||
var row: u32 = 0;
|
var row: u32 = 0;
|
||||||
while (it.next()) |line| {
|
while (it.next()) |line| {
|
||||||
if (line.len == 0) continue;
|
if (line.len == 0) continue;
|
||||||
for (line, 0..) |char, col| {
|
grid[row] = try alloc.alloc(u8, line.len);
|
||||||
switch (char) {
|
@memcpy(grid[row], line);
|
||||||
'O' => {
|
|
||||||
std.debug.print("rock at {}x{} moves to {}\n", .{ col, row, height - next_slot[col] });
|
|
||||||
weight += height - next_slot[col];
|
|
||||||
next_slot[col] += 1;
|
|
||||||
std.debug.print("{any}\n", .{next_slot});
|
|
||||||
},
|
|
||||||
'#' => {
|
|
||||||
std.debug.print("cube at {}x{}\n", .{ col, row });
|
|
||||||
next_slot[col] = @as(u32, @intCast(row)) + 1;
|
|
||||||
std.debug.print("{any}\n", .{next_slot});
|
|
||||||
},
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
row += 1;
|
row += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return .{ .a = weight, .b = 0 };
|
const w = try northWeight(alloc, grid);
|
||||||
|
spin(grid, 1000000000);
|
||||||
|
const v = try northWeight(alloc, grid);
|
||||||
|
_ = v;
|
||||||
|
|
||||||
|
for (grid) |line| {
|
||||||
|
alloc.free(line);
|
||||||
|
}
|
||||||
|
return .{ .a = w, .b = 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
test "silver" {
|
test "silver" {
|
||||||
|
Loading…
Reference in New Issue
Block a user