summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcominixo <cominixo.dev>2025-12-03 21:25:01 +0000
committercominixo <cominixo.dev>2025-12-03 21:25:01 +0000
commit3e7e92c195969ee015a406eeb769072caccebfc9 (patch)
treed4337656c3df8789a76dc82da855dd8d0c0cadb9
parent917950b60c78609d1d966580bad4884a3ceffb40 (diff)
-rw-r--r--src/days/3/3.zig52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/days/3/3.zig b/src/days/3/3.zig
new file mode 100644
index 0000000..66c6f9c
--- /dev/null
+++ b/src/days/3/3.zig
@@ -0,0 +1,52 @@
+const std = @import("std");
+
+pub fn main() !void {
+
+ const buf = @embedFile("input");
+
+ var lines = std.mem.tokenizeScalar(u8, buf, '\n');
+
+ var sum: u64 = 0;
+
+ const n = 12; // n = 2 for p1
+
+ var digits_ind: [n]?usize = undefined;
+
+ while (lines.next()) |line| {
+
+ digits_ind[0] = 0;
+ for (1..n) |i| {
+ digits_ind[i] = null;
+ }
+
+ outer: for (1..line.len) |i| {
+
+ const c = line[i];
+
+ for (0..@min(n, i)) |digit| {
+
+ const index = digits_ind[digit] orelse line.len;
+ const char = if (index < line.len) line[index] else 0;
+
+ if ((c > char and i < line.len-(n-1-digit)) or char == 0) {
+ digits_ind[digit] = i;
+
+ for (digit+1..n) |j| {
+ digits_ind[j] = null;
+ }
+ continue :outer;
+ }
+ }
+ }
+
+ var num: u64 = 0;
+ for (digits_ind, 0..) |ind, i| {
+ num += (line[ind.?] - '0')*std.math.pow(u64, 10, n-i-1);
+ }
+
+
+ sum += num;
+ }
+
+ std.debug.print("{}\n", .{sum});
+}