aboutsummaryrefslogtreecommitdiffstats
path: root/src/12/main.zig
blob: 12fd5f06334f975a46f4e33cc60b1717018c7cb7 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const std = @import("std");
const aoc = @import("aoc");
const Pos = aoc.Pos;
const Dir = aoc.Dir;

const Ship = struct { pos: Pos, waypoint: Pos, dir: usize };

fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
    _ = args;

    var ship = Ship{ .pos = Pos{ .x = 0, .y = 0 }, .waypoint = Pos{ .x = 0, .y = 0 }, .dir = 0 };
    var lineit = std.mem.tokenize(u8, input, "\n");
    while (lineit.next()) |line| {
        const val = try std.fmt.parseInt(u32, line[1..], 10);
        switch (line[0]) {
            'N' => ship.pos = ship.pos.add(Dir.North.mult(val)),
            'S' => ship.pos = ship.pos.add(Dir.South.mult(val)),
            'E' => ship.pos = ship.pos.add(Dir.East.mult(val)),
            'W' => ship.pos = ship.pos.add(Dir.West.mult(val)),
            'L' => ship.dir = Dir.nextCCW(ship.dir, @divExact(val, 90)),
            'R' => ship.dir = Dir.nextCW(ship.dir, @divExact(val, 90)),
            'F' => ship.pos = ship.pos.add(Dir.dirs[ship.dir].mult(val)),
            else => {
                return aoc.Error.InvalidInput;
            },
        }
    }

    const answer = std.math.absCast(ship.pos.x) + std.math.absCast(ship.pos.y);

    return try std.fmt.allocPrint(allocator, "{d}", .{answer});
}

fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
    _ = args;

    var ship = Ship{ .pos = Pos{ .x = 0, .y = 0 }, .waypoint = Pos{ .x = 10, .y = 1 }, .dir = 0 };
    var lineit = std.mem.tokenize(u8, input, "\n");
    while (lineit.next()) |line| {
        const val = try std.fmt.parseInt(u32, line[1..], 10);
        switch (line[0]) {
            'N' => ship.waypoint = ship.waypoint.add(Dir.North.mult(val)),
            'S' => ship.waypoint = ship.waypoint.add(Dir.South.mult(val)),
            'E' => ship.waypoint = ship.waypoint.add(Dir.East.mult(val)),
            'W' => ship.waypoint = ship.waypoint.add(Dir.West.mult(val)),
            'L' => {
                var new = Dir.rotCCW(ship.waypoint, @divExact(val, 90));
                ship.waypoint = new;
            },
            'R' => {
                var new = Dir.rotCW(ship.waypoint, @divExact(val, 90));
                ship.waypoint = new;
            },
            'F' => ship.pos = ship.pos.add(ship.waypoint.mult(val)),
            else => {
                return aoc.Error.InvalidInput;
            },
        }
        aoc.debugfmt("{s} {} {} {} {}\n", .{ line, ship.waypoint.x, ship.waypoint.y, ship.pos.x, ship.pos.y });
    }

    const answer = std.math.absCast(ship.pos.x) + std.math.absCast(ship.pos.y);

    return try std.fmt.allocPrint(allocator, "{d}", .{answer});
}

pub const main = aoc.main(part1, part2, .{ "1191", "61053" });