diff options
Diffstat (limited to 'src/day07.zig')
| -rw-r--r-- | src/day07.zig | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/day07.zig b/src/day07.zig new file mode 100644 index 0000000..620aaa3 --- /dev/null +++ b/src/day07.zig @@ -0,0 +1,88 @@ +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; + + const first = try reader.takeDelimiter('\n'); + + var space = try allocator.alloc(bool, first.?.len); + defer allocator.free(space); + + for (space) |*b| b.* = false; + + space[(first.?.len - 1) / 2] = true; + + while (reader.takeDelimiter('\n')) |line| { + if (line == null) { + break; + } + + for (line.?,0..) |e, i| { + if (e == '^' and space[i]) { + space[i] = false; + space[i-1] = true; + space[i+1] = true; + output += 1; + } + } + + } else |_| {} + + print("{d}\n", .{output}); +} + +fn quantum(space: [][]u8, pos: usize, y: usize, output: *usize) void { + + if (space.len == y){ + return; + } + + if (space[y][pos] == '^') { + quantum(space, pos - 1, y + 1, output); + output.* += 1; + quantum(space, pos + 1, y + 1, output); + } else { + quantum(space, pos, y + 1, output); + } +} + + +fn part2(reader: *std.Io.Reader, allocator: std.mem.Allocator) !void { + var output: usize = 1; + + const first = try reader.takeDelimiter('\n'); + + var space = try allocator.alloc(usize, first.?.len); + defer allocator.free(space); + + for (space) |*b| b.* = 0; + + space[(first.?.len - 1) / 2] = 1; + + while (reader.takeDelimiter('\n')) |line| { + if (line == null) { + break; + } + + for (line.?,0..) |e, i| { + if (e == '^' and space[i] > 0) { + space[i-1] += space[i]; + space[i+1] += space[i]; + output += space[i]; + space[i] = 0; + } + } + + } else |_| {} + + print("{d}\n", .{output}); +} |
