1
0

Compare commits

..

2 Commits

Author SHA1 Message Date
99ab8f2921
day02: part2
Some checks failed
Build and test / test (push) Failing after 2m50s
2023-12-01 16:54:28 +11:00
ec97bd8ad7
day01: part1 2023-12-01 16:28:46 +11:00
3 changed files with 64 additions and 1 deletions

3
.gitignore vendored
View File

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

0
src/data/.gitkeep Normal file
View File

View File

@ -1,4 +1,64 @@
const std = @import("std"); const std = @import("std");
const util = @import("util.zig"); 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("Solution: {d}", .{sln});
}
const numbers = [_][]const u8{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
const zero = @as(u8, '0');
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 (0..line.len) |offset| {
var n: u8 = 0; // ascii value
if (line[offset] >= '0' and line[offset] <= '9') {
n = line[offset];
} else {
inline for (numbers, 0..) |word, value| {
if (std.mem.startsWith(u8, line[offset..], word)) {
n = value + zero;
break;
}
}
}
if (n != 0) {
if (first == 0) {
first = n;
}
last = n;
}
}
const cal_value = (first - zero) * 10 + (last - zero);
sum += cal_value;
}
return sum;
}
test "sample" {
const input =
\\two1nine
\\eightwothree
\\abcone2threexyz
\\xtwone3four
\\4nineeightseven2
\\zoneight234
\\7pqrstsixteen
;
const sln = try solve(input);
try std.testing.expectEqual(@as(u32, 281), sln);
}