From dd7b53a590fc3058e8c4e75e9c2cd25b90bfa0b1 Mon Sep 17 00:00:00 2001 From: Dallin Urness Date: Fri, 12 Dec 2025 23:40:23 -0700 Subject: [PATCH 1/2] day12 --- .../communitySolutions/12/dalurness.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/content/communitySolutions/12/dalurness.md diff --git a/src/content/communitySolutions/12/dalurness.md b/src/content/communitySolutions/12/dalurness.md new file mode 100644 index 0000000..89e5331 --- /dev/null +++ b/src/content/communitySolutions/12/dalurness.md @@ -0,0 +1,65 @@ +--- +descriptions: ["zig"] +--- + +## 2025 + +```zig +const std = @import("std"); + +pub fn main() !void { + var filename_buf: [16]u8 = undefined; + for (0..5) |i| { + const filename = try std.fmt.bufPrint(&filename_buf, "./src/input{}.txt", .{i}); + var file = try std.fs.cwd().openFile(filename, .{ .mode = .read_only }); + defer file.close(); + try processFile(&file); + } +} +fn processFile(file: *std.fs.File) !void { + const allocator = std.heap.page_allocator; + + var buf: [1024 * 1024]u8 = undefined; + var reader = file.reader(&buf); + + var subs = std.ArrayList(std.ArrayList(u8)).empty; + var message = std.ArrayList(u8).empty; + while (reader.interface.takeDelimiterExclusive('\n')) |line| { + if (std.mem.indexOf(u8, line, &[_]u8{ '=', '>' })) |ind| { + const str_start = ind + 4; + const str_end = line.len - 1; + + var list = std.ArrayList(u8).empty; + try list.append(allocator, line[0]); + try list.appendSlice(allocator, line[str_start..str_end]); + + // need to properly replace \n in keys with the actual character for it to work right + while (std.mem.indexOf(u8, list.items, &[_]u8{ '\\', 'n' })) |index| { + list.orderedRemoveMany(&[_]usize{ index, index + 1 }); + try list.insert(allocator, index, '\n'); + } + + try subs.append(allocator, list); + } else { + try message.appendSlice(allocator, line); + try message.append(allocator, '\n'); + } + } else |err| if (err != error.EndOfStream) return err; + + // go through keys in reverse replacing all usages + var i: usize = subs.items.len; + while (i > 0) : (i -= 1) { + const index = i - 1; + const char = subs.items[index].items[0]; + while (std.mem.indexOfScalar(u8, message.items, char)) |ind| { + _ = message.orderedRemove(ind); + try message.insertSlice(allocator, ind, subs.items[index].items[1..]); + } + } + + for (message.items) |c| { + std.debug.print("{c}", .{c}); + } + std.debug.print("\n\n\n\n======================\n\n\n\n", .{}); +} +``` From bc11abd597157efa421d86b5aa7bdf2fc1cf4c61 Mon Sep 17 00:00:00 2001 From: Dallin Urness Date: Mon, 15 Dec 2025 00:00:42 -0700 Subject: [PATCH 2/2] cleanup memory --- src/content/communitySolutions/12/dalurness.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/content/communitySolutions/12/dalurness.md b/src/content/communitySolutions/12/dalurness.md index 89e5331..42e9f75 100644 --- a/src/content/communitySolutions/12/dalurness.md +++ b/src/content/communitySolutions/12/dalurness.md @@ -7,6 +7,7 @@ descriptions: ["zig"] ```zig const std = @import("std"); +const allocator = std.heap.page_allocator; pub fn main() !void { var filename_buf: [16]u8 = undefined; for (0..5) |i| { @@ -17,8 +18,6 @@ pub fn main() !void { } } fn processFile(file: *std.fs.File) !void { - const allocator = std.heap.page_allocator; - var buf: [1024 * 1024]u8 = undefined; var reader = file.reader(&buf); @@ -61,5 +60,12 @@ fn processFile(file: *std.fs.File) !void { std.debug.print("{c}", .{c}); } std.debug.print("\n\n\n\n======================\n\n\n\n", .{}); + + // cleanup + for (subs.items) |*list| { + list.deinit(allocator); + } + subs.deinit(allocator); + message.deinit(allocator); } ```