Compare commits
No commits in common. "99ab8f2921923ec35cca21f75178ac48197552ac" and "cf4dea84d99b3353953c53213e68e8b69d39e6dc" have entirely different histories.
99ab8f2921
...
cf4dea84d9
3
.gitignore
vendored
3
.gitignore
vendored
@ -69,6 +69,3 @@ docgen_tmp/
|
||||
|
||||
### nix ###
|
||||
result
|
||||
|
||||
### local ###
|
||||
src/data
|
||||
|
@ -1,64 +1,4 @@
|
||||
const std = @import("std");
|
||||
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});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
pub fn main() !void {}
|
||||
|
Loading…
Reference in New Issue
Block a user