summaryrefslogtreecommitdiff
path: root/src/day02.zig
blob: e87b234d44d3e9e07ca8fdbe1b27ea56ad9da278 (plain)
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
const std = @import("std");
const stdin = std.io.getStdIn();
const print = std.debug.print;

pub fn solve(part: []u8, buffer: []u8) !void {
    var lines = std.mem.splitScalar(u8, buffer, '\n');
    if (std.mem.eql(u8, part, "1")) {
        try part1(&lines);
    } else {
        try part2(&lines);
    }
}

fn part1(lines: *std.mem.SplitIterator(u8, .scalar)) !void {
    var count: usize = 0;
    lines: while (lines.next()) |line| {
        if (line.len > 0) {
            var components = std.mem.splitScalar(u8, line, ' ');
            var previous: i32 = try std.fmt.parseInt(i32, components.next().?, 10);
            var increasing: i32 = 0;
            while (components.next()) |number_s| {
                const number = try std.fmt.parseInt(i32, number_s, 10);
                if (increasing == 0) {
                    if (previous > number) {
                        increasing = -1;
                    } else {
                        increasing = 1;
                    }
                }

                const difference = (number - previous) * increasing;
                if (difference < 1 or difference > 3) {
                    continue :lines;
                }
                previous = number;
            }
            count += 1;
        }
    }

    print("{d}\n", .{count});
}

fn part2(lines: *std.mem.SplitIterator(u8, .scalar)) !void {
    var count: usize = 0;
    while (lines.next()) |line| {
        if (line.len > 0) {
            var components = std.mem.splitScalar(u8, line, ' ');

            var i: usize = 0;
            var j: usize = 0;

            counter: while (i <= j) {
                j = 0;
                components.reset();

                if (i == 0) {
                    _ = components.next();
                }

                var previous: i32 = try std.fmt.parseInt(i32, components.next().?, 10);
                var increasing: i32 = 0;
                while (components.next()) |number_s| {
                    if (i != j) {
                        const number = try std.fmt.parseInt(i32, number_s, 10);
                        if (increasing == 0) {
                            if (previous > number) {
                                increasing = -1;
                            } else {
                                increasing = 1;
                            }
                        }

                        const difference = (number - previous) * increasing;
                        if (difference < 1 or difference > 3) {
                            i += 1;
                            continue :counter;
                        }
                        previous = number;
                    }

                    j += 1;
                }

                count += 1;
                break;
            }
        }
    }
    print("{d}\n", .{count});
}