1
0

day01: include both part 1 and 2

This commit is contained in:
xeals 2023-12-01 23:57:42 +11:00
parent 87d3e72276
commit d5ee0f6aef
Signed by: xeals
SSH Key Fingerprint: SHA256:53xHRPqZPQewIgPNiVQ96gm8O4xBVzaxNj1LCSJpTf4

View File

@ -3,14 +3,43 @@ const util = @import("util.zig");
pub fn main() !void {
const input = @embedFile("data/day01.txt");
const sln = try solve(input);
std.debug.print("Solution: {d}", .{sln});
std.debug.print("{d}\n", .{try silver(input)});
std.debug.print("{d}\n", .{try gold(input)});
}
const zero = @as(u8, '0');
fn silver(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 cal_value = (first - zero) * 10 + (last - zero);
sum += cal_value;
}
return sum;
}
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 {
fn gold(input: []const u8) !u32 {
var sum: u32 = 0;
var it = std.mem.splitAny(u8, input, "\n");
@ -49,7 +78,18 @@ fn solve(input: []const u8) !u32 {
return sum;
}
test "sample" {
test "silver" {
const input =
\\1abc2
\\pqr3stu8vwx
\\a1b2c3d4e5f
\\treb7uchet
;
const sln = try silver(input);
try std.testing.expectEqual(@as(u32, 142), sln);
}
test "gold" {
const input =
\\two1nine
\\eightwothree
@ -59,6 +99,6 @@ test "sample" {
\\zoneight234
\\7pqrstsixteen
;
const sln = try solve(input);
const sln = try gold(input);
try std.testing.expectEqual(@as(u32, 281), sln);
}