1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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)
swap(usize, &x1, &x2);
if (y1 > y2)
swap(usize, &y1, &y2);
if (x1 == x2) {
while (y1 <= y2) : (y1 += 1)
diagram[index(x1, y1)] += 1;
} else if (y1 == y2) {
while (x1 <= x2) : (x1 += 1)
diagram[index(x1, y1)] += 1;
}
}
var result: usize = 0;
for (diagram) |point|
result += @boolToInt(point > 1);
print("{}\n", .{ result });
}
|