commit b338064781c1514ccb50d41f7eec4f45bb0bd0ed
parent d7ad5ab57e95d65c462b4409371dbabf958efdd3
Author: amin <dev@aminmesbah.com>
Date: Thu, 6 Dec 2018 06:50:41 +0000
Add solution for 06-2
FossilOrigin-Name: 1977bba604017e6cc6c1b16b8421b1db7d80a0311411800ad94ddd48f1afe4d2
Diffstat:
M | day_06_1.zig | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
1 file changed, 52 insertions(+), 2 deletions(-)
diff --git a/day_06_1.zig b/day_06_1.zig
@@ -6,8 +6,58 @@ const math = std.math;
pub fn main() !void {
var allocator = &std.heap.DirectAllocator.init().allocator;
- var result = try biggest_finite_area(allocator, coordinates);
- debug.warn("06-1: {}\n", result);
+ var result1 = try biggest_finite_area(allocator, coordinates);
+ debug.warn("06-1: {}\n", result1);
+ var result2 = try safe_area(coordinates, 10000);
+ debug.warn("06-2: {}\n", result2);
+}
+
+fn safe_area(coords: []const V2, distance_threshold: usize) !u32 {
+ var max = point(0, 0);
+ for (coords) |c| {
+ //V2.print(c);
+ if (c.x > max.x) {
+ max.x = c.x;
+ }
+ if (c.y > max.y) {
+ max.y = c.y;
+ }
+ }
+
+ const field_stride = max.x + 1;
+ const field_size: usize = field_stride * (max.y + 1);
+
+ var area: u32 = 0;
+ var cell_i: usize = 0;
+ while (cell_i < field_size) : (cell_i += 1) {
+ var distance_sum: usize = 0;
+ for (coords) |coord, coord_i| {
+ var dist = try manhattan_distance(point_from_index(cell_i, field_stride), coord);
+ distance_sum += dist;
+ if (distance_sum >= distance_threshold) {
+ break;
+ }
+ }
+ if (distance_sum < distance_threshold) {
+ area += 1;
+ }
+ }
+
+ return area;
+}
+
+test "safe area" {
+ const test_threshold: usize = 32;
+ const test_coords = []const V2 {
+ point(1, 1),
+ point(1, 6),
+ point(8, 3),
+ point(3, 4),
+ point(5, 5),
+ point(8, 9),
+ };
+
+ debug.assert(16 == try safe_area(test_coords, test_threshold));
}
fn biggest_finite_area(allocator: *mem.Allocator, coords: []const V2) !u32 {