diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2024-12-13 23:27:30 +0100 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2024-12-13 23:27:30 +0100 |
commit | 536f5af5e4c07c49e1ac8509a44a5098dfd866ce (patch) | |
tree | 5adc0433d61e95e86828066bc4f41c11d4e1c1f5 | |
parent | dae1e9ead3f23d4bdd4417f16e4ef756bafcc86a (diff) | |
download | programming-problem-solving-536f5af5e4c07c49e1ac8509a44a5098dfd866ce.tar.gz |
aoc2024: day 9 part 2 remove some cruft
-rw-r--r-- | aoc24/src/day9.zig | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/aoc24/src/day9.zig b/aoc24/src/day9.zig index f2bae47..c80f05f 100644 --- a/aoc24/src/day9.zig +++ b/aoc24/src/day9.zig @@ -80,11 +80,6 @@ pub fn part2(allocator: std.mem.Allocator, input: Input) !u64 { var file: u32 = @intCast(input.compressed.len - 1); while (true) : (file = std.math.sub(u32, file, 1) catch break) { - // for (filesystem) |item| { - // if (item) |it| std.debug.print("{}", .{it}) else std.debug.print(".", .{}); - // } - // std.debug.print("\n", .{}); - const index = std.mem.indexOfScalar(?u32, filesystem, file).?; const len = input.compressed[file].taken; std.debug.assert(std.mem.allEqual(?u32, filesystem[index..][0..len], file)); @@ -109,50 +104,3 @@ pub fn part2(allocator: std.mem.Allocator, input: Input) !u64 { return sum; } - -pub fn part2OptimizedButWrong(allocator: std.mem.Allocator, input: Input) !u64 { - var filesystem = std.ArrayList(?u32).init(allocator); - for (input.compressed) |entry| { - try filesystem.appendNTimes(null, entry.taken); - try filesystem.appendNTimes(null, entry.free); - } - - var file_locations = std.ArrayList(u32).init(allocator); - var free_slots = std.ArrayList(struct { start: u32, size: u8 }).init(allocator); - var i: u32 = 0; - for (input.compressed) |e| { - try file_locations.append(i); - try free_slots.append(.{ .start = i + e.taken, .size = e.free }); - i += e.taken + e.free; - } - - var file_id: u32 = @intCast(input.compressed.len - 1); - while (true) { - const file_size = input.compressed[file_id].taken; - for (free_slots.items) |*slot| { - if (file_size <= slot.size) { - for (filesystem.items[slot.start..][0..file_size]) |id| std.debug.assert(id == null); - @memset(filesystem.items[slot.start..][0..file_size], file_id); - slot.size -= file_size; - slot.start += file_size; - break; - } - } else { - for (filesystem.items[file_locations.items[file_id]..][0..file_size]) |id| std.debug.assert(id == null); - @memset(filesystem.items[file_locations.items[file_id]..][0..file_size], file_id); - } - file_id = std.math.sub(u32, file_id, 1) catch break; - } - - // for (filesystem.items) |item| { - // if (item) |it| std.debug.print("{}", .{it}) else std.debug.print(".", .{}); - // } - // std.debug.print("\n", .{}); - - var sum: u64 = 0; - for (0.., filesystem.items) |pos, id| { - sum += pos * @as(u64, @intCast(id orelse 0)); - } - - return sum; -} |