From 516e6ed4a9066fa43d6159b2a0ec58416ab28013 Mon Sep 17 00:00:00 2001 From: xander Date: Sun, 7 Dec 2025 13:12:06 +0100 Subject: solve up to day 7 --- src/day04.zig | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/day04.zig (limited to 'src/day04.zig') diff --git a/src/day04.zig b/src/day04.zig new file mode 100644 index 0000000..5d7b455 --- /dev/null +++ b/src/day04.zig @@ -0,0 +1,99 @@ +const std = @import("std"); +const print = std.debug.print; + +pub fn solve(part: []u8, reader: *std.Io.Reader, allocator: std.mem.Allocator) !void { + if (std.mem.eql(u8, part, "1")) { + try part1(reader, allocator); + } else { + try part2(reader, allocator); + } +} + + +fn part1(reader: *std.Io.Reader, allocator: std.mem.Allocator) !void { + var output: usize = 0; + var list = try std.ArrayList([]u8).initCapacity(allocator, 0); + defer list.deinit(allocator); + + while (reader.takeDelimiter('\n')) |line| { + if (line == null){ + break; + } + + try list.append(allocator, line.?); + } else |_| { + + } + + + for (list.items,0..) |row, y| { + for (row,0..) |item,x| { + if (item != '@'){ + continue; + } + + var adjacent: usize = 0; + if (y > 0 and x > 0 and list.items[y - 1][x - 1] == '@') adjacent += 1; + if (y > 0 and list.items[y - 1][x] == '@') adjacent += 1; + if (y > 0 and x < row.len - 1 and list.items[y - 1][x + 1] == '@') adjacent += 1; + if (x > 0 and list.items[y][x - 1] == '@') adjacent += 1; + if (x < row.len - 1 and list.items[y][x + 1] == '@') adjacent += 1; + if (y < list.items.len - 1 and x > 0 and list.items[y + 1][x - 1] == '@') adjacent += 1; + if (y < list.items.len - 1 and list.items[y + 1][x] == '@') adjacent += 1; + if (y < list.items.len - 1 and x < row.len - 1 and list.items[y + 1][x + 1] == '@') adjacent += 1; + + if (adjacent < 4){ + + output += 1; + } + } + } + + print("{d}\n", .{output}); +} + +fn part2(reader: *std.Io.Reader, allocator: std.mem.Allocator) !void { + var output: usize = 0; + var list = try std.ArrayList([]u8).initCapacity(allocator, 0); + defer list.deinit(allocator); + + while (reader.takeDelimiter('\n')) |line| { + if (line == null){ + break; + } + + try list.append(allocator, line.?); + } else |_| { + + } + + + var previous_output: usize = 1; + while (output != previous_output) { + previous_output = output; + for (list.items,0..) |row, y| { + for (row,0..) |item,x| { + if (item != '@'){ + continue; + } + + var adjacent: usize = 0; + if (y > 0 and x > 0 and list.items[y - 1][x - 1] == '@') adjacent += 1; + if (y > 0 and list.items[y - 1][x] == '@') adjacent += 1; + if (y > 0 and x < row.len - 1 and list.items[y - 1][x + 1] == '@') adjacent += 1; + if (x > 0 and list.items[y][x - 1] == '@') adjacent += 1; + if (x < row.len - 1 and list.items[y][x + 1] == '@') adjacent += 1; + if (y < list.items.len - 1 and x > 0 and list.items[y + 1][x - 1] == '@') adjacent += 1; + if (y < list.items.len - 1 and list.items[y + 1][x] == '@') adjacent += 1; + if (y < list.items.len - 1 and x < row.len - 1 and list.items[y + 1][x + 1] == '@') adjacent += 1; + + if (adjacent < 4){ + list.items[y][x] = '.'; + output += 1; + } + } + } + } + + print("{d}\n", .{output}); +} -- cgit v1.2.3