aboutsummaryrefslogtreecommitdiff
path: root/2022/03-rucksack-reorganization/first.zig
blob: 38c6d0e8c502b51af59d3ed149373b9024466cd6 (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
const std = @import("std");

const example = @embedFile("example");
const input = @embedFile("input");

pub fn main() anyerror!void {
    try std.testing.expectEqual(solve(example), 157);
    const result = try solve(input);
    try std.io.getStdOut().writer().print("{}\n", .{result});
}

fn solve(puzzle: []const u8) !u64 {
    var it = std.mem.tokenize(u8, puzzle, "\n");
    var tot: u64 = 0;
    while (it.next()) |line| {
        var i: usize = 0;
        var middle = line.len / 2;
        outer: while (i < middle) : (i += 1) {
            var j = middle;
            while (j < line.len) : (j += 1) {
                if (line[i] == line[j]) {
                    break :outer;
                }
            }
        }
        if (line[i] <= 'Z') {
            tot = tot + line[i] - 'A' + 27;
        } else {
            tot = tot + line[i] - 'a' + 1;
        }
    }
    return tot;
}