Compare commits
2 Commits
87d3e72276
...
44ccf7b6e2
Author | SHA1 | Date | |
---|---|---|---|
44ccf7b6e2 | |||
d5ee0f6aef |
@ -1,16 +1,17 @@
|
|||||||
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");
|
||||||
const sln = try solve(input);
|
std.debug.print("{d}\n", .{try solve(input, &[_][]u8{})});
|
||||||
std.debug.print("Solution: {d}", .{sln});
|
std.debug.print("{d}\n", .{try solve(input, &numbers)});
|
||||||
}
|
}
|
||||||
|
|
||||||
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) !u32 {
|
fn solve(input: []const u8, words: []const []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");
|
||||||
@ -27,9 +28,9 @@ fn solve(input: []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 {
|
||||||
inline for (numbers, 0..) |word, value| {
|
for (words, 0..) |word, value| {
|
||||||
if (std.mem.startsWith(u8, line[offset..], word)) {
|
if (std.mem.startsWith(u8, line[offset..], word)) {
|
||||||
n = value + zero;
|
n = @as(u8, @intCast(value)) + zero;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,7 +50,17 @@ fn solve(input: []const u8) !u32 {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "sample" {
|
test "silver" {
|
||||||
|
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
|
||||||
@ -59,6 +70,5 @@ test "sample" {
|
|||||||
\\zoneight234
|
\\zoneight234
|
||||||
\\7pqrstsixteen
|
\\7pqrstsixteen
|
||||||
;
|
;
|
||||||
const sln = try solve(input);
|
try std.testing.expectEqual(@as(u32, 281), try solve(input, &numbers));
|
||||||
try std.testing.expectEqual(@as(u32, 281), sln);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user