Compare commits
2 Commits
cf4dea84d9
...
99ab8f2921
Author | SHA1 | Date | |
---|---|---|---|
99ab8f2921 | |||
ec97bd8ad7 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -69,3 +69,6 @@ docgen_tmp/
|
||||
|
||||
### nix ###
|
||||
result
|
||||
|
||||
### local ###
|
||||
src/data
|
||||
|
0
src/data/.gitkeep
Normal file
0
src/data/.gitkeep
Normal file
@ -1,4 +1,64 @@
|
||||
const std = @import("std");
|
||||
const util = @import("util.zig");
|
||||
|
||||
pub fn main() !void {}
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user