diff options
author | Leah Neukirchen <leah@vuxu.org> | 2022-12-06 14:23:44 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2022-12-06 14:23:44 +0100 |
commit | 0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c (patch) | |
tree | 58fca570328f59ca08dc92b3cea90dcf8eacc730 /day05.zig | |
parent | 68ee54cfaa49f438bb7ad93e7c595e72c64dfbbd (diff) | |
download | adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.tar.gz adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.tar.xz adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.zip |
day05
Diffstat (limited to 'day05.zig')
-rw-r--r-- | day05.zig | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/day05.zig b/day05.zig new file mode 100644 index 0000000..3a80a39 --- /dev/null +++ b/day05.zig @@ -0,0 +1,60 @@ +const std = @import("std"); +const data = @embedFile("day05"); + +const Stack = std.BoundedArray(u8, 64); + +pub fn main() !void { + var lines = std.mem.split(u8, data, "\n"); + + var stacks = [_]Stack{try Stack.init(0)} ** 9; + + while (lines.next()) |line| { + if (line.len == 0) + break; + + for (stacks) |*stack, j| { + var c = line[1 + (j * 4)]; + if (c != ' ') { + try stack.append(c); + } + } + } + + for (stacks) |*stack| { + std.mem.reverse(u8, stack.slice()); + } + + var stacks2 = stacks; + + while (lines.next()) |line| { + if (line.len == 0) + break; + + var parts = std.mem.tokenize(u8, line, " "); + _ = parts.next(); + var n = try std.fmt.parseInt(usize, parts.next().?, 10); + _ = parts.next(); + var from = try std.fmt.parseInt(usize, parts.next().?, 10) - 1; + _ = parts.next(); + var to = try std.fmt.parseInt(usize, parts.next().?, 10) - 1; + + var j: usize = 0; + while (j < n) : (j += 1) { + try stacks[to].append(stacks[from].pop()); + } + + var f = stacks2[from].slice(); + try stacks2[to].appendSlice(f[f.len - n .. f.len]); + try stacks2[from].resize(f.len - n); + } + + for (stacks) |*stack| { + std.debug.print("{c}", .{stack.pop()}); + } + std.debug.print("\n", .{}); + + for (stacks2) |*stack| { + std.debug.print("{c}", .{stack.pop()}); + } + std.debug.print("\n", .{}); +} |