diff options
-rw-r--r-- | main.zig | 2 | ||||
-rw-r--r-- | src/day06.zig | 121 |
2 files changed, 123 insertions, 0 deletions
@@ -6,6 +6,7 @@ const day02 = @import("src/day02.zig"); const day03 = @import("src/day03.zig"); const day04 = @import("src/day04.zig"); const day05 = @import("src/day05.zig"); +const day06 = @import("src/day06.zig"); pub fn main() !void { if (std.os.argv.len != 3) { @@ -39,6 +40,7 @@ pub fn main() !void { 3 => day03.solve(args[2], buffer, allocator), 4 => day04.solve(args[2], buffer, allocator), 5 => day05.solve(args[2], buffer, allocator), + 6 => day06.solve(args[2], buffer, allocator), else => print("Day not yet implemented", .{}), }; } diff --git a/src/day06.zig b/src/day06.zig new file mode 100644 index 0000000..bbea665 --- /dev/null +++ b/src/day06.zig @@ -0,0 +1,121 @@ +const std = @import("std"); +const mecha = @import("mecha"); +const print = std.debug.print; + +pub fn solve(part: []u8, buffer: []u8, allocator: std.mem.Allocator) !void { + var lines = std.mem.splitScalar(u8, buffer, '\n'); + var grid = std.ArrayList([]u8).init(allocator); + defer grid.deinit(); + var x: i32 = 0; + var y: i32 = 0; + + var row: usize = 0; + while (lines.next()) |line| : (row += 1) { + for (line, 0..) |element, i| { + if (element == '^') { + x = @intCast(i); + y = @intCast(row); + } + } + try grid.append(@constCast(line)); + } + + if (std.mem.eql(u8, part, "1")) { + try part1(&grid, x, y, allocator); + } else { + try part2(&grid, x, y, allocator); + } +} + +fn locations(grid: *std.ArrayList([]u8), startx: i32, starty: i32, allocator: std.mem.Allocator) !std.AutoHashMap(std.meta.Tuple(&.{ i32, i32 }), void) { + var dirx: i32 = 0; + var diry: i32 = -1; + var x = startx; + var y = starty; + var unique = std.AutoHashMap(std.meta.Tuple(&.{ i32, i32 }), void).init(allocator); + errdefer unique.deinit(); + + while (y >= 0 and y < grid.items.len and x >= 0 and x < grid.items[@intCast(y)].len) { + if (grid.items[@intCast(y)][@intCast(x)] == '#') { + x -= dirx; + y -= diry; + const temp = dirx; + dirx = -diry; + diry = temp; + } + + try unique.put(.{ x, y }, {}); + + x += dirx; + y += diry; + } + + return unique; +} + +fn part1(grid: *std.ArrayList([]u8), startx: i32, starty: i32, allocator: std.mem.Allocator) !void { + var locs = try locations(grid, startx, starty, allocator); + defer locs.deinit(); + print("{d}\n", .{locs.count()}); +} + +//----------------------------PART 2--------------------------------------------- + +fn directionToIndex(dirx: i32, diry: i32) i32 { + if (dirx == 0 and diry == -1) { + return 0; + } else if (dirx == 1 and diry == 0) { + return 1; + } else if (dirx == 0 and diry == 1) { + return 2; + } else { + return 3; + } +} +fn part2(grid: *std.ArrayList([]u8), startx: i32, starty: i32, allocator: std.mem.Allocator) !void { + var locs = try locations(grid, startx, starty, allocator); + defer locs.deinit(); + var total: usize = 0; + + var iterator = locs.keyIterator(); + var history = try allocator.alloc(bool, grid.items.len * grid.items[0].len * 4); + defer allocator.free(history); + + const width: i32 = @intCast(grid.items[0].len); + + while (iterator.next()) |location| { + grid.items[@intCast(location[1])][@intCast(location[0])] = '#'; + var dirx: i32 = 0; + var diry: i32 = -1; + var x = startx; + var y = starty; + + for (history) |*elem| { + elem.* = false; + } + + while (y >= 0 and y < grid.items.len and x >= 0 and x < grid.items[@intCast(y)].len) { + const index = y * width * 4 + x * 4 + directionToIndex(dirx, diry); + if (history[@intCast(index)]) { + total += 1; + break; + } + if (grid.items[@intCast(y)][@intCast(x)] == '#') { + x -= dirx; + y -= diry; + const temp = dirx; + dirx = -diry; + diry = temp; + } + + history[@intCast(index)] = true; + + x += dirx; + y += diry; + } + + grid.items[@intCast(location[1])][@intCast(location[0])] = '.'; + } + + print("{d}\n", .{total}); +} |