From ec97bd8ad7f3d397e5b4c893fa318764f6244eee Mon Sep 17 00:00:00 2001 From: xeals Date: Fri, 1 Dec 2023 16:28:46 +1100 Subject: [PATCH] day01: part1 --- .gitignore | 3 +++ src/data/.gitkeep | 0 src/day01.zig | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/data/.gitkeep diff --git a/.gitignore b/.gitignore index 9876873..fbf8fb8 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,6 @@ docgen_tmp/ ### nix ### result + +### local ### +src/data diff --git a/src/data/.gitkeep b/src/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/day01.zig b/src/day01.zig index ff7c164..470d7f4 100644 --- a/src/day01.zig +++ b/src/day01.zig @@ -1,4 +1,48 @@ 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("Part 1: {d}", .{sln}); +} + +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 (line) |char| { + if (char < '0' or char > '9') { + continue; + } + if (first == 0) { + first = char; + } + last = char; + } + + const zero = @as(u8, '0'); + const cal_value = (first - zero) * 10 + (last - zero); + sum += cal_value; + } + + return sum; +} + +test "sample" { + const input = + \\1abc2 + \\pqr3stu8vwx + \\a1b2c3d4e5f + \\treb7uchet + ; + const sln = try solve(input); + try std.testing.expectEqual(@as(u32, 142), sln); +}