about summary refs log tree commit diff
path: root/aoc/2021/13/part-one.zig
blob: bdd28ebab742361b1f578b0a9759ed3eb74d348d (plain) (blame)
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
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)), {});
    }
}