2022-03 in zig

This commit is contained in:
Julien Dessaux 2022-12-03 14:43:57 +01:00
parent 447d72d027
commit 02a8114847
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 384 additions and 0 deletions

View file

@ -0,0 +1,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;
}