From 0665901987cc5a46ee251a8d073f405015f33a18 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Mon, 13 Dec 2021 16:40:32 +0700 Subject: [aoc/2021] Finish day 13 --- aoc/2021/13/part-one.zig | 36 ++++++++++++++++++++++++++++++++++++ aoc/2021/13/part-two.zig | 31 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 aoc/2021/13/part-one.zig create mode 100644 aoc/2021/13/part-two.zig diff --git a/aoc/2021/13/part-one.zig b/aoc/2021/13/part-one.zig new file mode 100644 index 0000000..bdd28eb --- /dev/null +++ b/aoc/2021/13/part-one.zig @@ -0,0 +1,36 @@ +const ArenaAllocator = std.heap.ArenaAllocator; +const AutoHashMap = std.AutoHashMap; +const min = std.math.min; +const page_allocator = std.heap.page_allocator; +const parseUnsigned = std.fmt.parseUnsigned; +const print = std.debug.print; +const split = std.mem.split; +const std = @import("std"); +const tokenize = std.mem.tokenize; + +const Dot = struct { x: u12, y: u12 }; +inline fn mirror(a: u12, m: u12) u12 { return min(a, m + m - a); } +fn foldX(d: Dot, m: u12) Dot { return .{ .x = mirror(d.x, m), .y = d.y }; } +fn foldY(d: Dot, m: u12) Dot { return .{ .x = d.x, .y = mirror(d.y, m) }; } + +pub fn main() !void { + var arena = ArenaAllocator.init(page_allocator); + defer arena.deinit(); + var map = AutoHashMap(Dot, void).init(&arena.allocator); + defer map.deinit(); + defer print("{}\n", .{ map.count() }); + + var input = split(@embedFile("input"), "\n\n"); + var dots = tokenize(input.next().?, "\n"); + var folds = tokenize(input.next().?, "\n"); + const instruction = folds.next().?; + while (dots.next()) |line| { + var coord = tokenize(line, ","); + const dot = Dot{ .x = try parseUnsigned(u12, coord.next().?, 10), + .y = try parseUnsigned(u12, coord.next().?, 10) }; + try map.put(switch (instruction[11]) { + 'x' => foldX, 'y' => foldY, + else => unreachable, + } (dot, try parseUnsigned(u12, instruction[13..], 10)), {}); + } +} diff --git a/aoc/2021/13/part-two.zig b/aoc/2021/13/part-two.zig new file mode 100644 index 0000000..f42ebff --- /dev/null +++ b/aoc/2021/13/part-two.zig @@ -0,0 +1,31 @@ +const min = std.math.min; +const parseUnsigned = std.fmt.parseUnsigned; +const print = std.debug.print; +const split = std.mem.split; +const std = @import("std"); +const tokenize = std.mem.tokenize; + +const Dot = struct { x: u12, y: u12 }; +inline fn mirror(a: u12, m: u12) u12 { return min(a, m + m - a); } +fn foldX(d: Dot, m: u12) Dot { return .{ .x = mirror(d.x, m), .y = d.y }; } +fn foldY(d: Dot, m: u12) Dot { return .{ .x = d.x, .y = mirror(d.y, m) }; } + +pub fn main() !void { + var input = split(@embedFile("input"), "\n\n"); + var dots = tokenize(input.next().?, "\n"); + var folds = tokenize(input.next().?, "\n"); + var code = @bitCast([6][40]u8, ([_]u8{ ' ' } ** 39 ++ [_]u8{ '\n' }) ** 6); + defer print("{s}", .{ @bitCast([240]u8, code) }); + + while (dots.next()) |line| : (folds.reset()) { + var coord = tokenize(line, ","); + var dot = Dot{ .x = try parseUnsigned(u12, coord.next().?, 10), + .y = try parseUnsigned(u12, coord.next().?, 10) }; + while (folds.next()) |instruction| + dot = switch (instruction[11]) { + 'x' => foldX, 'y' => foldY, + else => unreachable, + } (dot, try parseUnsigned(u12, instruction[13..], 10)); + code[dot.y][dot.x] = '|'; + } +} -- cgit 1.4.1