diff options
Diffstat (limited to '2022')
-rw-r--r-- | 2022/05-supply-stacks/example | 10 | ||||
-rw-r--r-- | 2022/05-supply-stacks/first.zig | 62 | ||||
-rw-r--r-- | 2022/05-supply-stacks/input | 514 | ||||
-rw-r--r-- | 2022/05-supply-stacks/second.zig | 61 |
4 files changed, 647 insertions, 0 deletions
diff --git a/2022/05-supply-stacks/example b/2022/05-supply-stacks/example new file mode 100644 index 0000000..edacdb9 --- /dev/null +++ b/2022/05-supply-stacks/example @@ -0,0 +1,10 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 + diff --git a/2022/05-supply-stacks/first.zig b/2022/05-supply-stacks/first.zig new file mode 100644 index 0000000..d06fac8 --- /dev/null +++ b/2022/05-supply-stacks/first.zig @@ -0,0 +1,62 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var output = try solve(example, allocator); + try std.testing.expect(std.mem.eql(u8, output, "CMZ")); + + const result = try solve(input, allocator); + try std.io.getStdOut().writer().print("{s}\n", .{result}); +} + +fn solve(puzzle: []const u8, allocator: std.mem.Allocator) ![]u8 { + var it = std.mem.tokenize(u8, puzzle, "\n"); + var line = it.next() orelse unreachable; + // Initializations from the first line + const size = (line.len + 1) / 4; + var output = try allocator.alloc(u8, size); + var stacks = try allocator.alloc(std.ArrayList(u8), size); + for (stacks) |*stack| { + stack.* = std.ArrayList(u8).init(allocator); + } + // parse stacks + while (true) { + var i: usize = 0; + while (i < size) : (i += 1) { + const v = line[i * 4 + 1]; + if (v != ' ') { + try stacks[i].insert(0, v); + } + } + line = it.next() orelse unreachable; + if (line[0] != '[') { // we got the line with numbers + break; + } + } + // process rules + while (it.next()) |l| { + var elts = std.mem.split(u8, l, " "); + _ = elts.next() orelse unreachable; // the move word + var n = try std.fmt.parseInt(u8, elts.next() orelse unreachable, 10); + _ = elts.next() orelse unreachable; // the from word + const s = try std.fmt.parseInt(u8, elts.next() orelse unreachable, 10) - 1; + _ = elts.next() orelse unreachable; // the to word + const d = try std.fmt.parseInt(u8, elts.next() orelse unreachable, 10) - 1; + while (n > 0) : (n -= 1) { + const v = stacks[s].pop(); + try stacks[d].append(v); + } + } + // generate output + var i: usize = 0; + while (i < size) : (i += 1) { + output[i] = stacks[i].pop(); + } + return output; +} diff --git a/2022/05-supply-stacks/input b/2022/05-supply-stacks/input new file mode 100644 index 0000000..66b41d3 --- /dev/null +++ b/2022/05-supply-stacks/input @@ -0,0 +1,514 @@ +[N] [C] [Z] +[Q] [G] [V] [S] [V] +[L] [C] [M] [T] [W] [L] +[S] [H] [L] [C] [D] [H] [S] +[C] [V] [F] [D] [D] [B] [Q] [F] +[Z] [T] [Z] [T] [C] [J] [G] [S] [Q] +[P] [P] [C] [W] [W] [F] [W] [J] [C] +[T] [L] [D] [G] [P] [P] [V] [N] [R] + 1 2 3 4 5 6 7 8 9 + +move 6 from 2 to 1 +move 4 from 6 to 3 +move 1 from 6 to 5 +move 8 from 3 to 8 +move 13 from 8 to 2 +move 2 from 7 to 6 +move 10 from 1 to 6 +move 3 from 2 to 8 +move 5 from 4 to 2 +move 15 from 6 to 5 +move 1 from 1 to 4 +move 2 from 7 to 3 +move 2 from 4 to 2 +move 12 from 5 to 1 +move 4 from 8 to 9 +move 15 from 1 to 3 +move 10 from 9 to 7 +move 1 from 5 to 1 +move 1 from 4 to 8 +move 3 from 7 to 6 +move 8 from 2 to 6 +move 1 from 9 to 8 +move 5 from 2 to 3 +move 1 from 4 to 1 +move 16 from 3 to 1 +move 2 from 2 to 7 +move 13 from 1 to 6 +move 1 from 2 to 4 +move 2 from 2 to 9 +move 1 from 4 to 7 +move 2 from 8 to 2 +move 2 from 2 to 9 +move 1 from 6 to 8 +move 2 from 3 to 8 +move 2 from 1 to 9 +move 1 from 3 to 9 +move 1 from 3 to 2 +move 5 from 5 to 1 +move 2 from 9 to 3 +move 1 from 2 to 3 +move 2 from 1 to 3 +move 3 from 3 to 2 +move 1 from 5 to 7 +move 2 from 7 to 6 +move 2 from 8 to 3 +move 1 from 8 to 9 +move 6 from 3 to 4 +move 3 from 9 to 6 +move 8 from 6 to 4 +move 1 from 2 to 3 +move 1 from 2 to 6 +move 1 from 2 to 9 +move 1 from 3 to 9 +move 5 from 9 to 5 +move 7 from 7 to 4 +move 14 from 4 to 6 +move 1 from 5 to 3 +move 5 from 1 to 9 +move 4 from 5 to 4 +move 1 from 1 to 7 +move 1 from 3 to 8 +move 1 from 8 to 4 +move 4 from 9 to 7 +move 6 from 6 to 5 +move 10 from 4 to 6 +move 1 from 9 to 6 +move 1 from 4 to 3 +move 1 from 3 to 6 +move 1 from 4 to 2 +move 35 from 6 to 3 +move 1 from 2 to 3 +move 4 from 5 to 8 +move 2 from 5 to 4 +move 3 from 8 to 2 +move 2 from 4 to 8 +move 26 from 3 to 8 +move 3 from 2 to 9 +move 6 from 3 to 5 +move 3 from 5 to 7 +move 3 from 7 to 4 +move 3 from 4 to 5 +move 1 from 9 to 5 +move 6 from 5 to 1 +move 2 from 8 to 6 +move 11 from 8 to 5 +move 9 from 5 to 4 +move 1 from 9 to 7 +move 2 from 7 to 9 +move 3 from 1 to 4 +move 1 from 5 to 7 +move 8 from 6 to 1 +move 5 from 7 to 9 +move 7 from 9 to 2 +move 3 from 2 to 9 +move 3 from 7 to 1 +move 4 from 9 to 8 +move 2 from 5 to 6 +move 2 from 2 to 8 +move 2 from 6 to 9 +move 13 from 8 to 1 +move 1 from 2 to 8 +move 3 from 3 to 5 +move 1 from 9 to 8 +move 3 from 5 to 4 +move 1 from 9 to 3 +move 1 from 2 to 3 +move 4 from 8 to 2 +move 3 from 2 to 4 +move 19 from 1 to 2 +move 8 from 1 to 8 +move 1 from 4 to 3 +move 1 from 4 to 1 +move 7 from 2 to 1 +move 1 from 3 to 1 +move 2 from 3 to 1 +move 15 from 4 to 5 +move 1 from 1 to 7 +move 11 from 2 to 8 +move 2 from 2 to 9 +move 1 from 3 to 5 +move 2 from 9 to 4 +move 12 from 8 to 3 +move 16 from 5 to 1 +move 3 from 4 to 3 +move 1 from 7 to 5 +move 2 from 8 to 6 +move 1 from 5 to 4 +move 1 from 4 to 9 +move 18 from 1 to 9 +move 8 from 3 to 8 +move 9 from 8 to 2 +move 4 from 9 to 2 +move 8 from 1 to 2 +move 2 from 6 to 4 +move 17 from 2 to 1 +move 1 from 4 to 5 +move 3 from 2 to 6 +move 1 from 2 to 9 +move 2 from 6 to 1 +move 3 from 3 to 6 +move 1 from 4 to 6 +move 2 from 3 to 2 +move 16 from 9 to 5 +move 14 from 5 to 4 +move 3 from 5 to 8 +move 1 from 2 to 4 +move 4 from 8 to 6 +move 1 from 2 to 8 +move 1 from 3 to 9 +move 1 from 3 to 9 +move 2 from 9 to 1 +move 10 from 8 to 7 +move 7 from 6 to 9 +move 16 from 1 to 5 +move 7 from 4 to 3 +move 1 from 8 to 4 +move 5 from 4 to 2 +move 1 from 5 to 9 +move 5 from 9 to 1 +move 5 from 1 to 2 +move 2 from 9 to 7 +move 1 from 1 to 7 +move 1 from 6 to 8 +move 4 from 4 to 5 +move 1 from 6 to 9 +move 9 from 2 to 1 +move 11 from 5 to 6 +move 2 from 9 to 2 +move 4 from 3 to 4 +move 4 from 4 to 6 +move 1 from 3 to 4 +move 11 from 7 to 4 +move 9 from 4 to 7 +move 11 from 7 to 2 +move 2 from 3 to 5 +move 2 from 4 to 8 +move 7 from 5 to 2 +move 1 from 8 to 3 +move 1 from 5 to 1 +move 1 from 3 to 7 +move 6 from 2 to 9 +move 1 from 8 to 9 +move 6 from 9 to 2 +move 15 from 6 to 2 +move 1 from 7 to 2 +move 31 from 2 to 7 +move 22 from 7 to 3 +move 2 from 5 to 1 +move 3 from 7 to 4 +move 1 from 4 to 9 +move 3 from 4 to 3 +move 1 from 8 to 6 +move 1 from 9 to 6 +move 15 from 1 to 5 +move 1 from 9 to 5 +move 1 from 1 to 8 +move 2 from 6 to 8 +move 1 from 8 to 4 +move 1 from 4 to 6 +move 1 from 6 to 9 +move 10 from 3 to 1 +move 1 from 9 to 7 +move 2 from 7 to 8 +move 10 from 5 to 1 +move 12 from 1 to 4 +move 1 from 3 to 8 +move 11 from 4 to 8 +move 1 from 8 to 3 +move 5 from 5 to 8 +move 1 from 5 to 8 +move 6 from 8 to 6 +move 3 from 2 to 1 +move 1 from 6 to 2 +move 5 from 1 to 6 +move 3 from 1 to 4 +move 3 from 2 to 8 +move 1 from 2 to 9 +move 8 from 3 to 5 +move 2 from 1 to 3 +move 3 from 7 to 5 +move 2 from 3 to 5 +move 3 from 5 to 2 +move 1 from 7 to 9 +move 2 from 9 to 1 +move 1 from 6 to 9 +move 2 from 4 to 8 +move 5 from 6 to 5 +move 1 from 6 to 7 +move 1 from 9 to 8 +move 3 from 6 to 5 +move 7 from 8 to 9 +move 5 from 9 to 1 +move 2 from 4 to 8 +move 11 from 5 to 9 +move 3 from 2 to 3 +move 2 from 5 to 8 +move 4 from 3 to 7 +move 11 from 9 to 5 +move 3 from 7 to 5 +move 1 from 3 to 5 +move 8 from 1 to 4 +move 5 from 3 to 9 +move 15 from 5 to 4 +move 8 from 4 to 1 +move 12 from 8 to 1 +move 4 from 5 to 8 +move 12 from 4 to 5 +move 3 from 7 to 2 +move 11 from 5 to 7 +move 8 from 8 to 7 +move 7 from 9 to 8 +move 2 from 5 to 7 +move 4 from 7 to 8 +move 9 from 8 to 4 +move 11 from 4 to 5 +move 6 from 7 to 8 +move 9 from 8 to 7 +move 18 from 7 to 5 +move 1 from 8 to 1 +move 4 from 1 to 5 +move 1 from 7 to 2 +move 6 from 1 to 9 +move 1 from 2 to 4 +move 1 from 4 to 3 +move 3 from 1 to 7 +move 1 from 4 to 2 +move 3 from 2 to 5 +move 2 from 9 to 5 +move 1 from 2 to 6 +move 4 from 7 to 8 +move 1 from 6 to 2 +move 1 from 2 to 4 +move 4 from 8 to 5 +move 3 from 9 to 7 +move 1 from 9 to 5 +move 1 from 4 to 3 +move 2 from 3 to 8 +move 2 from 7 to 4 +move 28 from 5 to 8 +move 1 from 8 to 9 +move 1 from 9 to 3 +move 6 from 5 to 6 +move 5 from 5 to 2 +move 1 from 3 to 4 +move 1 from 7 to 4 +move 1 from 5 to 6 +move 16 from 8 to 3 +move 7 from 1 to 8 +move 4 from 4 to 9 +move 1 from 2 to 4 +move 3 from 2 to 3 +move 6 from 6 to 8 +move 10 from 3 to 8 +move 1 from 2 to 7 +move 1 from 6 to 7 +move 11 from 8 to 5 +move 2 from 7 to 8 +move 1 from 1 to 9 +move 5 from 9 to 5 +move 4 from 3 to 2 +move 1 from 4 to 2 +move 1 from 3 to 8 +move 3 from 8 to 2 +move 19 from 8 to 7 +move 6 from 7 to 6 +move 4 from 5 to 2 +move 9 from 7 to 5 +move 1 from 7 to 1 +move 5 from 6 to 9 +move 1 from 7 to 4 +move 1 from 6 to 7 +move 1 from 4 to 7 +move 1 from 1 to 2 +move 2 from 7 to 3 +move 6 from 5 to 9 +move 9 from 9 to 1 +move 17 from 5 to 4 +move 2 from 3 to 1 +move 13 from 4 to 7 +move 3 from 3 to 5 +move 7 from 1 to 4 +move 1 from 5 to 8 +move 2 from 5 to 2 +move 6 from 7 to 3 +move 1 from 5 to 7 +move 1 from 9 to 1 +move 2 from 3 to 2 +move 1 from 9 to 3 +move 9 from 7 to 3 +move 10 from 3 to 5 +move 8 from 4 to 2 +move 1 from 4 to 1 +move 13 from 2 to 4 +move 5 from 4 to 3 +move 1 from 5 to 9 +move 1 from 7 to 2 +move 6 from 4 to 2 +move 4 from 1 to 8 +move 3 from 4 to 6 +move 9 from 8 to 9 +move 17 from 2 to 3 +move 2 from 8 to 6 +move 1 from 4 to 3 +move 2 from 6 to 3 +move 2 from 1 to 3 +move 13 from 3 to 4 +move 8 from 9 to 8 +move 7 from 4 to 6 +move 3 from 5 to 6 +move 5 from 8 to 2 +move 9 from 6 to 1 +move 7 from 2 to 4 +move 2 from 6 to 9 +move 1 from 1 to 5 +move 18 from 3 to 8 +move 5 from 1 to 3 +move 1 from 6 to 1 +move 9 from 4 to 7 +move 11 from 8 to 7 +move 5 from 7 to 5 +move 2 from 4 to 5 +move 1 from 6 to 2 +move 13 from 7 to 8 +move 1 from 4 to 9 +move 1 from 9 to 6 +move 4 from 1 to 5 +move 1 from 7 to 6 +move 9 from 5 to 7 +move 8 from 5 to 6 +move 10 from 7 to 2 +move 1 from 5 to 7 +move 1 from 7 to 1 +move 17 from 8 to 2 +move 9 from 6 to 7 +move 6 from 7 to 1 +move 2 from 7 to 2 +move 1 from 4 to 2 +move 12 from 2 to 8 +move 7 from 1 to 2 +move 6 from 8 to 6 +move 3 from 8 to 2 +move 1 from 7 to 2 +move 2 from 3 to 4 +move 1 from 4 to 9 +move 2 from 3 to 5 +move 2 from 3 to 7 +move 1 from 4 to 6 +move 2 from 7 to 1 +move 7 from 2 to 7 +move 6 from 7 to 1 +move 1 from 5 to 2 +move 6 from 8 to 4 +move 4 from 9 to 7 +move 1 from 5 to 2 +move 3 from 8 to 1 +move 1 from 9 to 4 +move 1 from 7 to 8 +move 1 from 8 to 1 +move 4 from 7 to 8 +move 1 from 4 to 2 +move 3 from 6 to 9 +move 2 from 9 to 7 +move 1 from 9 to 3 +move 2 from 4 to 3 +move 2 from 8 to 3 +move 5 from 3 to 4 +move 4 from 6 to 2 +move 8 from 2 to 9 +move 1 from 6 to 5 +move 10 from 2 to 3 +move 2 from 8 to 3 +move 8 from 9 to 3 +move 9 from 2 to 5 +move 1 from 2 to 4 +move 1 from 2 to 3 +move 7 from 5 to 6 +move 1 from 5 to 7 +move 13 from 3 to 4 +move 2 from 7 to 8 +move 5 from 3 to 1 +move 1 from 5 to 3 +move 1 from 8 to 5 +move 1 from 2 to 8 +move 1 from 7 to 9 +move 1 from 4 to 2 +move 15 from 4 to 8 +move 6 from 4 to 7 +move 6 from 7 to 8 +move 1 from 6 to 5 +move 1 from 4 to 6 +move 1 from 9 to 6 +move 2 from 5 to 2 +move 6 from 6 to 4 +move 6 from 1 to 8 +move 6 from 4 to 9 +move 2 from 6 to 1 +move 1 from 2 to 9 +move 26 from 8 to 1 +move 4 from 3 to 7 +move 2 from 2 to 5 +move 16 from 1 to 4 +move 3 from 9 to 8 +move 3 from 8 to 7 +move 3 from 5 to 1 +move 2 from 9 to 2 +move 1 from 9 to 7 +move 1 from 9 to 1 +move 8 from 4 to 1 +move 4 from 4 to 9 +move 1 from 2 to 3 +move 1 from 3 to 7 +move 2 from 8 to 2 +move 3 from 4 to 2 +move 1 from 4 to 7 +move 9 from 7 to 5 +move 1 from 9 to 8 +move 2 from 9 to 8 +move 5 from 5 to 7 +move 1 from 9 to 5 +move 6 from 2 to 6 +move 1 from 8 to 2 +move 5 from 6 to 5 +move 1 from 7 to 4 +move 3 from 8 to 9 +move 3 from 9 to 7 +move 1 from 6 to 4 +move 2 from 4 to 1 +move 2 from 5 to 8 +move 1 from 2 to 9 +move 2 from 8 to 9 +move 3 from 9 to 3 +move 8 from 7 to 3 +move 4 from 5 to 8 +move 1 from 3 to 9 +move 3 from 5 to 8 +move 1 from 5 to 3 +move 6 from 8 to 6 +move 3 from 3 to 9 +move 5 from 3 to 2 +move 5 from 6 to 4 +move 14 from 1 to 5 +move 8 from 5 to 6 +move 2 from 3 to 2 +move 4 from 9 to 1 +move 1 from 8 to 7 +move 7 from 2 to 3 +move 6 from 3 to 7 +move 3 from 5 to 3 +move 1 from 3 to 9 +move 12 from 1 to 5 +move 1 from 9 to 7 +move 2 from 3 to 1 +move 1 from 7 to 8 +move 1 from 8 to 7 +move 2 from 3 to 6 +move 2 from 1 to 9 +move 2 from 5 to 6 +move 2 from 9 to 7 +move 9 from 7 to 3 +move 7 from 1 to 5 +move 5 from 5 to 2 +move 8 from 6 to 8 +move 5 from 8 to 9 + diff --git a/2022/05-supply-stacks/second.zig b/2022/05-supply-stacks/second.zig new file mode 100644 index 0000000..580f69b --- /dev/null +++ b/2022/05-supply-stacks/second.zig @@ -0,0 +1,61 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var output = try solve(example, allocator); + try std.testing.expect(std.mem.eql(u8, output, "MCD")); + + const result = try solve(input, allocator); + try std.io.getStdOut().writer().print("{s}\n", .{result}); +} + +fn solve(puzzle: []const u8, allocator: std.mem.Allocator) ![]u8 { + var it = std.mem.tokenize(u8, puzzle, "\n"); + var line = it.next() orelse unreachable; + // Initializations from the first line + const size = (line.len + 1) / 4; + var output = try allocator.alloc(u8, size); + var stacks = try allocator.alloc(std.ArrayList(u8), size); + for (stacks) |*stack| { + stack.* = std.ArrayList(u8).init(allocator); + } + // parse stacks + while (true) { + var i: usize = 0; + while (i < size) : (i += 1) { + const v = line[i * 4 + 1]; + if (v != ' ') { + try stacks[i].insert(0, v); + } + } + line = it.next() orelse unreachable; + if (line[0] != '[') { // we got the line with numbers + break; + } + } + // process rules + while (it.next()) |l| { + var elts = std.mem.split(u8, l, " "); + _ = elts.next() orelse unreachable; // the move word + var n = try std.fmt.parseInt(u8, elts.next() orelse unreachable, 10); + _ = elts.next() orelse unreachable; // the from word + const s = try std.fmt.parseInt(u8, elts.next() orelse unreachable, 10) - 1; + _ = elts.next() orelse unreachable; // the to word + const d = try std.fmt.parseInt(u8, elts.next() orelse unreachable, 10) - 1; + const nl = stacks[s].items.len - n; + try stacks[d].appendSlice(stacks[s].items[nl..]); + stacks[s].items.len = nl; + } + // generate output + var i: usize = 0; + while (i < size) : (i += 1) { + output[i] = stacks[i].pop(); + } + return output; +} |