1
0

day01: part1

This commit is contained in:
xeals 2023-12-01 16:28:46 +11:00
parent cf4dea84d9
commit ec97bd8ad7
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc
3 changed files with 48 additions and 1 deletions

3
.gitignore vendored
View File

@ -69,3 +69,6 @@ docgen_tmp/
### nix ###
result
### local ###
src/data

0
src/data/.gitkeep Normal file
View File

View File

@ -1,4 +1,48 @@
const std = @import("std");
const util = @import("util.zig");
pub fn main() !void {}
pub fn main() !void {
const input = @embedFile("data/day01.txt");
const sln = try solve(input);
std.debug.print("Part 1: {d}", .{sln});
}
fn solve(input: []const u8) !u32 {
var sum: u32 = 0;
var it = std.mem.splitAny(u8, input, "\n");
while (it.next()) |line| {
if (std.mem.eql(u8, "", line)) {
continue;
}
var first: u8 = 0;
var last: u8 = 0;
for (line) |char| {
if (char < '0' or char > '9') {
continue;
}
if (first == 0) {
first = char;
}
last = char;
}
const zero = @as(u8, '0');
const cal_value = (first - zero) * 10 + (last - zero);
sum += cal_value;
}
return sum;
}
test "sample" {
const input =
\\1abc2
\\pqr3stu8vwx
\\a1b2c3d4e5f
\\treb7uchet
;
const sln = try solve(input);
try std.testing.expectEqual(@as(u32, 142), sln);
}