var tf = TileFloor{
.blacks = std.AutoArrayHashMap(Coord, void).init(allocator.?),
.neighbor = std.AutoArrayHashMap(Coord, usize).init(allocator.?),
};
defer {
tf.blacks.deinit();
tf.neighbor.deinit();
}
// Collect initial blacks
var lines = std.mem.tokenize(u8, @embedFile(PATH), "\n");
while (lines.next()) |line| {
const target = parseInstruction(line);
tf.blackFlip(target);
}
var round: usize = 0;
while (round < 100) : (round += 1) {
for (tf.blacks.keys()) |black| {
for (directions) |dir| {
const entry = try tf.neighbor.getOrPut(.{ black[0] + dir[0], black[1] + dir[1] });
if (entry.found_existing) entry.value_ptr.* += 1 else entry.value_ptr.* = 1;
}
}
var next = std.AutoArrayHashMap(Coord, void).init(allocator.?);
while (tf.neighbor.popOrNull()) |nbor| {
const is_black = tf.blacks.contains(nbor.key);
if ((is_black and nbor.value <= 2) or (!is_black and nbor.value == 2)) {
try next.put(nbor.key, {});
}
}
tf.blacks.deinit();
tf.blacks = next;
}
// std.debug.print("{any}\n", .{tf.blacks.keys()});
return tf.blacks.count();