Compare commits
No commits in common. "44ccf7b6e29b799f594ace40aafea1c2c9fb93f1" and "87d3e7227671c3b7ebf629b9829bbf12267f82ec" have entirely different histories.
44ccf7b6e2
...
87d3e72276
@ -1,17 +1,16 @@
|
||||
const std = @import("std");
|
||||
const util = @import("util.zig");
|
||||
|
||||
const numbers = [_][]const u8{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
|
||||
|
||||
pub fn main() !void {
|
||||
const input = @embedFile("data/day01.txt");
|
||||
std.debug.print("{d}\n", .{try solve(input, &[_][]u8{})});
|
||||
std.debug.print("{d}\n", .{try solve(input, &numbers)});
|
||||
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, words: []const []const u8) !u32 {
|
||||
fn solve(input: []const u8) !u32 {
|
||||
var sum: u32 = 0;
|
||||
|
||||
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') {
|
||||
n = line[offset];
|
||||
} else {
|
||||
for (words, 0..) |word, value| {
|
||||
inline for (numbers, 0..) |word, value| {
|
||||
if (std.mem.startsWith(u8, line[offset..], word)) {
|
||||
n = @as(u8, @intCast(value)) + zero;
|
||||
n = value + zero;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -50,17 +49,7 @@ fn solve(input: []const u8, words: []const []const u8) !u32 {
|
||||
return sum;
|
||||
}
|
||||
|
||||
test "silver" {
|
||||
const input =
|
||||
\\1abc2
|
||||
\\pqr3stu8vwx
|
||||
\\a1b2c3d4e5f
|
||||
\\treb7uchet
|
||||
;
|
||||
try std.testing.expectEqual(@as(u32, 142), try solve(input, &[_][]u8{}));
|
||||
}
|
||||
|
||||
test "gold" {
|
||||
test "sample" {
|
||||
const input =
|
||||
\\two1nine
|
||||
\\eightwothree
|
||||
@ -70,5 +59,6 @@ test "gold" {
|
||||
\\zoneight234
|
||||
\\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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user