From 85bd7ec1bd9cdc7ec53692fce5cae3118b7357a0 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 5 Dec 2021 15:18:59 +0700 Subject: [aoc] Add first five/fifth --- aoc/2021/05/part-one.zig | 46 ++++++++++++++++++++++++++++++++++++++++++++++ aoc/2021/05/part-two.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 aoc/2021/05/part-one.zig create mode 100644 aoc/2021/05/part-two.zig (limited to 'aoc/2021/05') diff --git a/aoc/2021/05/part-one.zig b/aoc/2021/05/part-one.zig new file mode 100644 index 0000000..95ad5a8 --- /dev/null +++ b/aoc/2021/05/part-one.zig @@ -0,0 +1,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 }); +} diff --git a/aoc/2021/05/part-two.zig b/aoc/2021/05/part-two.zig new file mode 100644 index 0000000..2c7ed83 --- /dev/null +++ b/aoc/2021/05/part-two.zig @@ -0,0 +1,48 @@ +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 }); +} -- cgit 1.4.1