summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2025-12-02 05:41:54 -0600
committerame <[email protected]>2025-12-02 05:41:54 -0600
commitf99714019cb330a563189e9d0d27b9faffbee706 (patch)
tree9e5e01a4451d0f2e1ca7489c7edb2c981dacc783
parentb1bcf07c564298ac1bd1e87f9c7bbddb60fb1c62 (diff)
day 2
-rw-r--r--src/day-2/input.txt1
-rw-r--r--src/day-2/part-1.zig54
-rw-r--r--src/day-2/part-2.zig59
3 files changed, 114 insertions, 0 deletions
diff --git a/src/day-2/input.txt b/src/day-2/input.txt
new file mode 100644
index 0000000..c893dde
--- /dev/null
+++ b/src/day-2/input.txt
@@ -0,0 +1 @@
+18623-26004,226779-293422,65855-88510,868-1423,248115026-248337139,903911-926580,97-121,67636417-67796062,24-47,6968-10197,193-242,3769-5052,5140337-5233474,2894097247-2894150301,979582-1016336,502-646,9132195-9191022,266-378,58-91,736828-868857,622792-694076,6767592127-6767717303,2920-3656,8811329-8931031,107384-147042,941220-969217,3-17,360063-562672,7979763615-7979843972,1890-2660,23170346-23308802
diff --git a/src/day-2/part-1.zig b/src/day-2/part-1.zig
new file mode 100644
index 0000000..ce3e1f5
--- /dev/null
+++ b/src/day-2/part-1.zig
@@ -0,0 +1,54 @@
+const std = @import("std");
+
+pub fn repeats(num: u64) !bool {
+ var buf: [20]u8 = undefined;
+
+ const str = try std.fmt.bufPrint(&buf, "{}", .{num});
+ if(@mod(str.len, 2) == 1) return false;
+
+ for(0..str.len/2) |find| {
+ const bind = find + str.len/2;
+ if(str[find] != str[bind]) return false;
+ //std.debug.print("{} {c}, {} {c}\n", .{find, str[find], bind, str[bind]});
+
+ }
+
+ return true;
+}
+
+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;
+
+ while(try reader.readUntilDelimiterOrEof(&buffer, ',')) |l| {
+ var line = l;
+ if(line[line.len - 1] == '\n') {
+ line = line[0 .. line.len - 1];
+ }
+
+ if(std.mem.indexOf(u8, line, "-")) |ind| {
+ const start = try std.fmt.parseInt(u64, line[0 .. ind], 10);
+ const end = try std.fmt.parseInt(u64, line[ind + 1 ..], 10);
+
+ //std.debug.print("{d} - {d}\n", .{start, end});
+
+ for(start .. end + 1) |n| {
+ if(try repeats(n)){
+ total += n;
+ //std.debug.print("{}\n", .{n});
+ }
+ }
+ }
+ }
+ std.debug.print("{d}\n", .{total});
+}
diff --git a/src/day-2/part-2.zig b/src/day-2/part-2.zig
new file mode 100644
index 0000000..ce61ab2
--- /dev/null
+++ b/src/day-2/part-2.zig
@@ -0,0 +1,59 @@
+const std = @import("std");
+
+pub fn repeats(num: u64) !bool {
+ var buf: [20]u8 = undefined;
+
+ const str = try std.fmt.bufPrint(&buf, "{}", .{num});
+
+ for(1 .. str.len) |window| {
+ if(@mod(str.len, window) == 0){
+ const match = str[0 .. window];
+ var i: usize = 0;
+ while(i < str.len) : (i += window) {
+ if(!std.mem.eql(u8, match, str[i .. i + window])){
+ break;
+ }
+ }
+ if(i == str.len) return true;
+ }
+ }
+
+ return false;
+}
+
+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;
+
+ while(try reader.readUntilDelimiterOrEof(&buffer, ',')) |l| {
+ var line = l;
+ if(line[line.len - 1] == '\n') {
+ line = line[0 .. line.len - 1];
+ }
+
+ if(std.mem.indexOf(u8, line, "-")) |ind| {
+ const start = try std.fmt.parseInt(u64, line[0 .. ind], 10);
+ const end = try std.fmt.parseInt(u64, line[ind + 1 ..], 10);
+
+ //std.debug.print("{d} - {d}\n", .{start, end});
+
+ for(start .. end + 1) |n| {
+ if(try repeats(n)){
+ total += n;
+ //std.debug.print("{}\n", .{n});
+ }
+ }
+ }
+ }
+ std.debug.print("{d}\n", .{total});
+}