const allocator = std.heap.page_allocator; const parseUnsigned = std.fmt.parseUnsigned; const print = std.debug.print; const set = std.mem.set; const swap = std.mem.swap; const std = @import("std"); const tokenize = std.mem.tokenize; inline fn index(x: usize, y: usize) usize { return x * 1000 + y; } pub fn main() !void { const diagram = try allocator.alloc(u16, 1_000_000); defer allocator.free(diagram); set(u16, diagram, 0); var input = tokenize(@embedFile("input"), "\n"); while (input.next()) |line| { var segment = tokenize(line, " -> "); var point = tokenize(segment.next().?, ","); var x1 = try parseUnsigned(usize, point.next().?, 10); var y1 = try parseUnsigned(usize, point.next().?, 10); point = tokenize(segment.next().?, ","); var x2 = try parseUnsigned(usize, point.next().?, 10); var y2 = try parseUnsigned(usize, point.next().?, 10); if (x1 == x2) { if (y1 > y2) swap(usize, &y1, &y2); while (y1 <= y2) : (y1 += 1) diagram[index(x1, y1)] += 1; } else if (y1 == y2) { if (x1 > x2) swap(usize, &x1, &x2); while (x1 <= x2) : (x1 += 1) diagram[index(x1, y1)] += 1; } else while (true) { diagram[index(x1, y1)] += 1; if (x1 == x2 or y1 == y2) break; if (x1 > x2) x1 -= 1 else x1 += 1; if (y1 > y2) y1 -= 1 else y1 += 1; } } var result: usize = 0; for (diagram) |point| result += @boolToInt(point > 1); print("{}\n", .{ result }); }