summaryrefslogtreecommitdiff
path: root/src/day-3/part-2.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/day-3/part-2.zig')
-rw-r--r--src/day-3/part-2.zig45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/day-3/part-2.zig b/src/day-3/part-2.zig
new file mode 100644
index 0000000..df78ed1
--- /dev/null
+++ b/src/day-3/part-2.zig
@@ -0,0 +1,45 @@
+const std = @import("std");
+
+//returns index
+pub fn largest(line: []const u8, start: usize, end: usize) usize {
+ var idx: usize = start;
+
+ for(start..end) |i| {
+ if(line[i] > line[idx]){
+ idx = i;
+ }
+ }
+
+ return idx;
+}
+
+pub fn main() !void {
+ //var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+ //defer gpa.deinit();
+ //const alloc = gpa.allocator();
+
+ const path = "input.txt";
+ const fp = try std.fs.cwd().openFile(path, .{.mode = std.fs.File.OpenMode.read_only});
+ defer fp.close();
+ var reader = fp.reader();
+
+ var buffer: [256]u8 = undefined;
+
+ var total: u64 = 0;
+
+ const need = 12;
+ while(try reader.readUntilDelimiterOrEof(&buffer, '\n')) |line| {
+ var lastidx: usize = 0;
+ var n: u64 = 0;
+
+ for(1 .. need + 1) |i| {
+ lastidx = largest(line, lastidx, line.len - (need - i));
+ n += std.math.pow(u64, 10, need - i) * (line[lastidx] - '0');
+ lastidx += 1;
+ }
+
+ total += n;
+ }
+
+ std.debug.print("{d}\n", .{total});
+}