From 0b6eda2892e579bc2dc3aee0da065e5074774add Mon Sep 17 00:00:00 2001 From: xeals Date: Thu, 14 Dec 2023 17:44:03 +1100 Subject: [PATCH] wip part2 --- src/day14.zig | 62 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/day14.zig b/src/day14.zig index bfbf143..151c4be 100644 --- a/src/day14.zig +++ b/src/day14.zig @@ -14,41 +14,57 @@ const Solution = struct { b: usize, }; -fn solve(alloc: mem.Allocator, input: []const u8) !Solution { - const width = mem.indexOfScalar(u8, input, '\n').?; - var height = mem.count(u8, input, &[_]u8{'\n'}); - if (input[input.len - 1] != '\n') height += 1; - - var next_slot = try alloc.alloc(u32, width); +fn northWeight(alloc: mem.Allocator, grid: [][]u8) !usize { + var next_slot = try alloc.alloc(u32, grid[0].len); defer alloc.free(next_slot); @memset(next_slot, 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 row: u32 = 0; while (it.next()) |line| { if (line.len == 0) continue; - for (line, 0..) |char, col| { - switch (char) { - '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 => {}, - } - } + grid[row] = try alloc.alloc(u8, line.len); + @memcpy(grid[row], line); 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" {