aoc2022/day3/puzzle2.zig

65 lines
1.8 KiB
Zig
Raw Normal View History

2022-12-03 23:47:20 +00:00
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const print = std.debug.print;
const input = @embedFile("input");
pub fn main() !void {
const checked_len : usize = 'z' + 1;
var checked_1 = [_]bool{false} ** checked_len;
var checked_2 = [_]bool{false} ** checked_len;
var total_points : usize = 0;
var line_count : u3 = 0;
// Split the input
var input_iter = mem.split(u8, input, "\n");
while (input_iter.next()) |line| {
if (line.len == 0) continue;
switch (line_count) {
0 => {
for (line) |letter| {
checked_1[letter] = true;
}
},
1 => {
for (line) |letter| {
checked_2[letter] = true;
}
},
else => {
for (line) |letter| {
if (checked_1[letter] and checked_2[letter]) {
if (letter >= 'a' and letter <= 'z') {
total_points += letter % 'a' + 1;
// std.debug.print("{c}:{}\n", .{letter, letter % 'a' + 1});
}
if (letter >= 'A' and letter <= 'Z') {
total_points += letter % 'A' + 27;
// std.debug.print("{c}:{}\n", .{letter, letter % 'A' + 27});
}
// Only one item is repeated
break;
}
}
// Reset the checked letters
checked_1 = [_]bool{false} ** checked_len;
checked_2 = [_]bool{false} ** checked_len;
}
}
line_count = (line_count + 1) % 3;
}
std.debug.print("Points: {}\n", .{total_points});
}