summaryrefslogtreecommitdiff
path: root/aoc24/src/day2.zig
blob: 656fd414a4dd133b26404d259f44b95bbf841dd9 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const std = @import("std");

test "part 1" {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    const allocator = arena.allocator();
    const input = try parse(allocator,
        \\7 6 4 2 1
        \\1 2 7 8 9
        \\9 7 6 2 1
        \\1 3 2 4 5
        \\8 6 4 4 1
        \\1 3 6 7 9
    );
    const value = try part1(allocator, input);
    const ok = value == 2;
    if (!ok) std.debug.print("got {}\n", .{value});
    std.debug.assert(ok);
}

test "part 2" {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    const allocator = arena.allocator();
    const input = try parse(allocator,
        \\7 6 4 2 1
        \\1 2 7 8 9
        \\9 7 6 2 1
        \\1 3 2 4 5
        \\8 6 4 4 1
        \\1 3 6 7 9
    );
    const value = try part2(allocator, input);
    const ok = value == 4;
    if (!ok) std.debug.print("got {}\n", .{value});
    std.debug.assert(ok);
}

pub fn parse(
    allocator: std.mem.Allocator,
    input: []const u8,
) !std.ArrayList(std.ArrayList(u32)) {
    var lines = std.mem.split(u8, input, "\n");

    var reports = std.ArrayList(std.ArrayList(u32)).init(allocator);

    while (lines.next()) |line| {
        if (line.len == 0) continue;
        var report = std.ArrayList(u32).init(allocator);
        var words = std.mem.split(u8, line, " ");
        while (words.next()) |word| {
            try report.append(try std.fmt.parseInt(u32, word, 10));
        }
        try reports.append(report);
    }

    return reports;
}

pub fn part1(
    _: std.mem.Allocator,
    reports: std.ArrayList(std.ArrayList(u32)),
) !u32 {
    var safe: u32 = 0;
    for (reports.items) |report| {
        const increasing = report.items[0] < report.items[1];
        for (report.items[0 .. report.items.len - 1], report.items[1..]) |prev, curr| {
            if ((prev < curr) != increasing) break;
            if (prev == curr) break;
            if (@max(prev, curr) - @min(prev, curr) > 3) break;
        } else {
            safe += 1;
        }
    }

    return safe;
}

pub fn part2(
    _: std.mem.Allocator,
    reports: std.ArrayList(std.ArrayList(u32)),
) !u32 {
    var safe: u32 = 0;
    for (reports.items) |report| {
        var is_safe = false;
        for ([_]bool{ false, true }) |increasing| for (0..report.items.len) |skipped| {
            const first: u32 = if (skipped == 0) 1 else 0;
            var prev = report.items[first];
            for (first + 1..report.items.len) |i| {
                if (i == skipped) continue;
                const curr = report.items[i];
                if ((prev < curr) != increasing or prev == curr or @max(prev, curr) - @min(prev, curr) > 3) {
                    break;
                }
                prev = curr;
            } else {
                is_safe = true;
            }
        };
        if (is_safe) safe += 1;
        // std.debug.print("{any} is {s}safe\n", .{ report.items, if (is_safe) "" else "un" });
    }

    return safe;
}