summaryrefslogtreecommitdiff
path: root/src/day04.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/day04.zig')
-rw-r--r--src/day04.zig99
1 files changed, 99 insertions, 0 deletions
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});
+}