This commit is contained in:
Kalle Carlbark 2023-12-01 21:51:50 +01:00
parent 1ab7b46425
commit 5fad9922cf
No known key found for this signature in database
2 changed files with 1054 additions and 1 deletions

1000
src/data/day01.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -9,8 +9,61 @@ const util = @import("util.zig");
const gpa = util.gpa; const gpa = util.gpa;
const data = @embedFile("data/day01.txt"); const data = @embedFile("data/day01.txt");
const data2 = @embedFile("data/day01_part2.txt");
pub fn main() !void {} pub fn main() !void {
const allocator = std.heap.page_allocator;
try part01(allocator, data);
}
fn find_first_digit_number(word: []const u8) !u8 {
var i: usize = 0;
while (i < word.len) : (i += 1) {
if (std.ascii.isDigit(word[i])) {
return word[i];
}
}
return word[i];
}
fn find_last_digit_number(word: []const u8) !u8 {
var i: usize = word.len - 1;
while (i < word.len) : (i -= 1) {
if (std.ascii.isDigit(word[i])) {
std.debug.print("last {}\n", .{word[i]});
return word[i];
}
}
return word[i];
}
fn part01(allocator: std.mem.Allocator, day01: []const u8) !void {
var splits = std.mem.split(u8, day01, "\n");
var total: u32 = 0;
while (splits.next()) |line| {
if (line.len <= 0) {
continue;
}
std.debug.print("Line: {s}\n", .{line});
const first_digit_char = try find_first_digit_number(line);
const last_digit_char = try find_last_digit_number(line);
const first_and_last = try std.fmt.allocPrint(allocator, "{c}{c}", .{ first_digit_char, last_digit_char });
std.debug.print("{c} + {c}\n", .{ first_digit_char, last_digit_char });
var digits: u32 = 0;
digits = try std.fmt.parseInt(u32, first_and_last, 10);
total += digits;
}
std.debug.print("First Total: {d}\n", .{total});
}
// Useful stdlib functions // Useful stdlib functions
const tokenizeAny = std.mem.tokenizeAny; const tokenizeAny = std.mem.tokenizeAny;