1
0

wtf
Some checks failed
Build and test / test (push) Failing after 2m35s

This commit is contained in:
xeals 2023-12-05 17:50:38 +11:00
parent d85d4f0d26
commit faf81869d6
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc

View File

@ -30,31 +30,49 @@ const Component = struct {
value: usize, value: usize,
}; };
const UsizeSet = std.AutoHashMap(usize, void);
fn solve(alloc: mem.Allocator, input: []const u8) !Solution { fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
var it = util.splitLines(input); var it = util.splitLines(input);
const seed_line = it.next().?; const seed_line = it.next().?;
const seeds = try util.parseIntsScalar(usize, alloc, seed_line[mem.indexOfScalar(u8, seed_line, ':').? + 1 ..], .{}); const seeds = try util.parseIntsScalar(usize, alloc, seed_line[mem.indexOfScalar(u8, seed_line, ':').? + 1 ..], .{});
defer alloc.free(seeds); defer alloc.free(seeds);
var last_components = try std.ArrayList(Component).initCapacity(alloc, seeds.len);
defer last_components.deinit(); var last_values = UsizeSet.init(alloc);
var working_values = UsizeSet.init(alloc);
defer last_values.deinit();
defer working_values.deinit();
var low_loc: usize = std.math.maxInt(usize);
var seeds_index: usize = 0; var seeds_index: usize = 0;
while (seeds_index < seeds.len) : (seeds_index += 2) { while (seeds_index < seeds.len) : (seeds_index += 2) {
last_values.clearRetainingCapacity();
working_values.clearRetainingCapacity();
std.debug.print("= seeding {d}/{d} =\n", .{ seeds_index, seeds.len });
const start = seeds[seeds_index]; const start = seeds[seeds_index];
const range = seeds[seeds_index + 1]; const range = seeds[seeds_index + 1];
for (start..start + range) |seed| { for (start..start + range) |seed| {
try last_components.append(.{ .name = "seed", .value = seed }); // std.debug.print("seed {d}\n", .{seed});
} try working_values.put(seed, {});
} }
// Reset parse input.
it = util.splitLines(input);
_ = it.next().?;
var current_component: []const u8 = undefined; var current_component: []const u8 = undefined;
while (it.next()) |line| { while (it.next()) |line| {
if (line.len == 0) { if (line.len == 0) {
for (last_components.items, 0..) |comp, index| { std.debug.print("== swap ==\n", .{});
if (!mem.eql(u8, comp.name, current_component)) { // Copy all remaining
last_components.items[index] = .{ .name = current_component, .value = comp.value }; var lv_it = last_values.iterator();
} while (lv_it.next()) |entry| {
try working_values.put(entry.key_ptr.*, {});
} }
// Swap working sets
mem.swap(UsizeSet, &last_values, &working_values);
working_values.clearRetainingCapacity();
} else if (mem.endsWith(u8, line, "map:")) { } else if (mem.endsWith(u8, line, "map:")) {
var t_it = util.split(line, ' '); var t_it = util.split(line, ' ');
var comp_it = util.split(t_it.next().?, '-'); var comp_it = util.split(t_it.next().?, '-');
@ -62,28 +80,39 @@ fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
_ = comp_it.next(); // to _ = comp_it.next(); // to
current_component = comp_it.next().?; // B current_component = comp_it.next().?; // B
} else { } else {
std.debug.print("== {s} {d}/{d}\n", .{ current_component, working_values.count(), last_values.count() });
const map = try util.parseIntsScalar(usize, alloc, line, .{}); const map = try util.parseIntsScalar(usize, alloc, line, .{});
defer alloc.free(map); defer alloc.free(map);
// map values
map_blk: for (last_components.items, 0..) |comp, index| { var to_delete = UsizeSet.init(alloc);
if (mem.eql(u8, comp.name, current_component)) { defer to_delete.deinit();
std.debug.print("{d} already mapped\n", .{index}); var lv_it = last_values.iterator();
continue :map_blk; while (lv_it.next()) |entry| {
} // if (transitioned[index]) {
if (map[1] <= comp.value and comp.value <= map[1] + map[2]) { // std.debug.print("{d} already mapped\n", .{index});
// continue :map_blk;
// }
const item = entry.key_ptr.*;
if (map[1] <= item and item <= map[1] + map[2]) {
const offset = @as(isize, @intCast(map[0])) - @as(isize, @intCast(map[1])); const offset = @as(isize, @intCast(map[0])) - @as(isize, @intCast(map[1]));
const res: usize = @intCast(@as(isize, @intCast(comp.value)) + offset); const res: usize = @intCast(@as(isize, @intCast(item)) + offset);
std.debug.print("{d} {d} => {d}\n", .{ index, comp.value, res }); // std.debug.print("{d} => {d}\n", .{ item, res });
last_components.items[index] = .{ .name = current_component, .value = res }; try working_values.put(res, {});
try to_delete.put(item, {});
} }
} }
var td_it = to_delete.iterator();
while (td_it.next()) |entry| {
// std.debug.print("pop {d}\n", .{entry.key_ptr.*});
_ = last_values.remove(entry.key_ptr.*);
}
} }
} }
var low_loc: usize = std.math.maxInt(usize); var lv_it = last_values.iterator();
for (last_components.items) |comp| { while (lv_it.next()) |value| {
std.debug.print("{s} ", .{comp.name}); low_loc = @min(low_loc, value.key_ptr.*);
low_loc = @min(low_loc, comp.value); }
} }
return .{ .a = low_loc, .b = 0 }; return .{ .a = low_loc, .b = 0 };