1
0

Compare commits

..

No commits in common. "44ccf7b6e29b799f594ace40aafea1c2c9fb93f1" and "87d3e7227671c3b7ebf629b9829bbf12267f82ec" have entirely different histories.

View File

@ -1,17 +1,16 @@
const std = @import("std"); const std = @import("std");
const util = @import("util.zig"); const util = @import("util.zig");
const numbers = [_][]const u8{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
pub fn main() !void { pub fn main() !void {
const input = @embedFile("data/day01.txt"); const input = @embedFile("data/day01.txt");
std.debug.print("{d}\n", .{try solve(input, &[_][]u8{})}); const sln = try solve(input);
std.debug.print("{d}\n", .{try solve(input, &numbers)}); 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'); const zero = @as(u8, '0');
fn solve(input: []const u8, words: []const []const u8) !u32 { fn solve(input: []const u8) !u32 {
var sum: u32 = 0; var sum: u32 = 0;
var it = std.mem.splitAny(u8, input, "\n"); var it = std.mem.splitAny(u8, input, "\n");
@ -28,9 +27,9 @@ fn solve(input: []const u8, words: []const []const u8) !u32 {
if (line[offset] >= '0' and line[offset] <= '9') { if (line[offset] >= '0' and line[offset] <= '9') {
n = line[offset]; n = line[offset];
} else { } else {
for (words, 0..) |word, value| { inline for (numbers, 0..) |word, value| {
if (std.mem.startsWith(u8, line[offset..], word)) { if (std.mem.startsWith(u8, line[offset..], word)) {
n = @as(u8, @intCast(value)) + zero; n = value + zero;
break; break;
} }
} }
@ -50,17 +49,7 @@ fn solve(input: []const u8, words: []const []const u8) !u32 {
return sum; return sum;
} }
test "silver" { test "sample" {
const input =
\\1abc2
\\pqr3stu8vwx
\\a1b2c3d4e5f
\\treb7uchet
;
try std.testing.expectEqual(@as(u32, 142), try solve(input, &[_][]u8{}));
}
test "gold" {
const input = const input =
\\two1nine \\two1nine
\\eightwothree \\eightwothree
@ -70,5 +59,6 @@ test "gold" {
\\zoneight234 \\zoneight234
\\7pqrstsixteen \\7pqrstsixteen
; ;
try std.testing.expectEqual(@as(u32, 281), try solve(input, &numbers)); const sln = try solve(input);
try std.testing.expectEqual(@as(u32, 281), sln);
} }