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,60 +30,89 @@ 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, {});
} }
}
var current_component: []const u8 = undefined; // Reset parse input.
while (it.next()) |line| { it = util.splitLines(input);
if (line.len == 0) { _ = it.next().?;
for (last_components.items, 0..) |comp, index| { var current_component: []const u8 = undefined;
if (!mem.eql(u8, comp.name, current_component)) { while (it.next()) |line| {
last_components.items[index] = .{ .name = current_component, .value = comp.value }; if (line.len == 0) {
std.debug.print("== swap ==\n", .{});
// Copy all remaining
var lv_it = last_values.iterator();
while (lv_it.next()) |entry| {
try working_values.put(entry.key_ptr.*, {});
} }
} // Swap working sets
} else if (mem.endsWith(u8, line, "map:")) { mem.swap(UsizeSet, &last_values, &working_values);
var t_it = util.split(line, ' '); working_values.clearRetainingCapacity();
var comp_it = util.split(t_it.next().?, '-'); } else if (mem.endsWith(u8, line, "map:")) {
_ = comp_it.next(); // A var t_it = util.split(line, ' ');
_ = comp_it.next(); // to var comp_it = util.split(t_it.next().?, '-');
current_component = comp_it.next().?; // B _ = comp_it.next(); // A
} else { _ = comp_it.next(); // to
const map = try util.parseIntsScalar(usize, alloc, line, .{}); current_component = comp_it.next().?; // B
defer alloc.free(map); } else {
// map values std.debug.print("== {s} {d}/{d}\n", .{ current_component, working_values.count(), last_values.count() });
map_blk: for (last_components.items, 0..) |comp, index| { const map = try util.parseIntsScalar(usize, alloc, line, .{});
if (mem.eql(u8, comp.name, current_component)) { defer alloc.free(map);
std.debug.print("{d} already mapped\n", .{index});
continue :map_blk; var to_delete = UsizeSet.init(alloc);
defer to_delete.deinit();
var lv_it = last_values.iterator();
while (lv_it.next()) |entry| {
// if (transitioned[index]) {
// 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 res: usize = @intCast(@as(isize, @intCast(item)) + offset);
// std.debug.print("{d} => {d}\n", .{ item, res });
try working_values.put(res, {});
try to_delete.put(item, {});
}
} }
if (map[1] <= comp.value and comp.value <= map[1] + map[2]) { var td_it = to_delete.iterator();
const offset = @as(isize, @intCast(map[0])) - @as(isize, @intCast(map[1])); while (td_it.next()) |entry| {
const res: usize = @intCast(@as(isize, @intCast(comp.value)) + offset); // std.debug.print("pop {d}\n", .{entry.key_ptr.*});
std.debug.print("{d} {d} => {d}\n", .{ index, comp.value, res }); _ = last_values.remove(entry.key_ptr.*);
last_components.items[index] = .{ .name = current_component, .value = res };
} }
} }
} }
}
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 };