day01: part1
This commit is contained in:
parent
cf4dea84d9
commit
ec97bd8ad7
3
.gitignore
vendored
3
.gitignore
vendored
@ -69,3 +69,6 @@ docgen_tmp/
|
||||
|
||||
### nix ###
|
||||
result
|
||||
|
||||
### local ###
|
||||
src/data
|
||||
|
0
src/data/.gitkeep
Normal file
0
src/data/.gitkeep
Normal 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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user