about summary refs log tree commit diff
path: root/day03.zig
blob: ddd458b4ed9c50f8dbe903a9b53eb52c7e872b60 (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
const std = @import("std");
const data = @embedFile("day03");

fn priority(c: u8) i32 {
    return switch (c) {
        'a'...'z' => c - 'a' + 1,
        'A'...'Z' => c - 'A' + 1 + 26,
        else => unreachable,
    };
}

pub fn main() !void {
    var part1: i32 = 0;
    var part2: i32 = 0;

    var lines = std.mem.split(u8, data, "\n");

    part1: while (lines.next()) |line| {
        if (line.len == 0)
            break;

        var mid: usize = line.len / 2;
        for (line[0..mid]) |c1| {
            for (line[mid..line.len]) |c2| {
                if (c1 == c2) {
                    part1 += priority(c1);
                    continue :part1;
                }
            }
        }
    }

    lines = std.mem.split(u8, data, "\n");

    part2: while (lines.next()) |line1| {
        if (line1.len == 0)
            break;
        var line2 = lines.next().?;
        var line3 = lines.next().?;

        for (line1) |c1| {
            for (line2) |c2| {
                if (c1 == c2) {
                    for (line3) |c3| {
                        if (c1 == c3) {
                            part2 += priority(c1);
                            continue :part2;
                        }
                    }
                }
            }
        }
    }

    std.debug.print("{} {}\n", .{ part1, part2 });
}