B7WMDH6SSILSWYJDOVVAFQKVY7OKG4Z43EORSTLYKT54KLYUVDGQC
[C] [S] [H]
[F] [B] [C] [S] [W]
[B] [W] [W] [M] [S] [B]
[L] [H] [G] [L] [P] [F] [Q]
[D] [P] [J] [F] [T] [G] [M] [T]
[P] [G] [B] [N] [L] [W] [P] [W] [R]
[Z] [V] [W] [J] [J] [C] [T] [S] [C]
[S] [N] [F] [G] [W] [B] [H] [F] [N]
1 2 3 4 5 6 7 8 9
move 2 from 5 to 9
move 3 from 1 to 7
move 2 from 3 to 9
move 6 from 9 to 5
move 2 from 3 to 8
move 9 from 7 to 8
move 15 from 8 to 9
move 3 from 1 to 6
move 6 from 4 to 2
move 6 from 5 to 6
move 1 from 4 to 2
move 14 from 6 to 2
move 2 from 1 to 5
move 1 from 7 to 3
move 1 from 4 to 8
move 2 from 5 to 6
move 25 from 2 to 4
move 2 from 6 to 4
move 1 from 8 to 1
move 2 from 9 to 1
move 1 from 6 to 1
move 2 from 1 to 7
move 1 from 7 to 3
move 2 from 1 to 8
move 1 from 2 to 6
move 1 from 3 to 8
move 4 from 5 to 6
move 1 from 5 to 3
move 1 from 9 to 6
move 2 from 3 to 4
move 1 from 2 to 6
move 12 from 9 to 7
move 1 from 9 to 1
move 1 from 5 to 8
move 1 from 3 to 8
move 28 from 4 to 5
move 1 from 4 to 3
move 1 from 2 to 6
move 1 from 3 to 9
move 12 from 7 to 2
move 1 from 9 to 6
move 6 from 6 to 4
move 1 from 7 to 4
move 1 from 1 to 2
move 28 from 5 to 1
move 2 from 2 to 8
move 3 from 8 to 2
move 7 from 4 to 1
move 4 from 8 to 6
move 9 from 2 to 8
move 7 from 6 to 5
move 3 from 5 to 9
move 1 from 9 to 7
move 1 from 7 to 1
move 5 from 8 to 4
move 4 from 1 to 9
move 6 from 9 to 4
move 5 from 1 to 5
move 5 from 2 to 3
move 4 from 8 to 2
move 5 from 1 to 4
move 4 from 5 to 9
move 9 from 4 to 9
move 10 from 9 to 8
move 1 from 9 to 1
move 2 from 2 to 8
move 4 from 3 to 8
move 1 from 2 to 3
move 2 from 9 to 2
move 1 from 2 to 6
move 4 from 4 to 3
move 3 from 5 to 1
move 12 from 1 to 4
move 1 from 5 to 3
move 1 from 5 to 3
move 5 from 8 to 5
move 7 from 8 to 5
move 8 from 3 to 4
move 1 from 5 to 1
move 1 from 6 to 7
move 2 from 1 to 6
move 8 from 5 to 9
move 2 from 5 to 1
move 9 from 1 to 4
move 20 from 4 to 2
move 1 from 5 to 2
move 4 from 4 to 2
move 5 from 9 to 2
move 2 from 8 to 9
move 23 from 2 to 4
move 2 from 2 to 5
move 5 from 1 to 2
move 28 from 4 to 3
move 2 from 8 to 1
move 2 from 5 to 7
move 1 from 6 to 9
move 1 from 4 to 8
move 1 from 8 to 9
move 1 from 4 to 6
move 2 from 7 to 2
move 13 from 3 to 4
move 5 from 9 to 7
move 1 from 9 to 6
move 14 from 2 to 6
move 1 from 4 to 1
move 10 from 3 to 2
move 1 from 6 to 9
move 2 from 3 to 2
move 3 from 1 to 9
move 1 from 3 to 5
move 3 from 9 to 3
move 6 from 7 to 4
move 1 from 9 to 4
move 1 from 9 to 2
move 1 from 5 to 3
move 5 from 3 to 1
move 17 from 4 to 7
move 2 from 2 to 8
move 1 from 3 to 9
move 1 from 8 to 2
move 1 from 9 to 6
move 4 from 6 to 2
move 10 from 6 to 5
move 4 from 1 to 5
move 15 from 2 to 9
move 1 from 8 to 6
move 1 from 2 to 8
move 6 from 9 to 2
move 3 from 4 to 8
move 11 from 7 to 1
move 6 from 9 to 6
move 1 from 6 to 2
move 3 from 9 to 3
move 6 from 2 to 7
move 6 from 7 to 8
move 7 from 1 to 9
move 4 from 1 to 6
move 2 from 1 to 2
move 4 from 6 to 7
move 1 from 2 to 9
move 1 from 2 to 3
move 1 from 2 to 1
move 6 from 8 to 4
move 2 from 6 to 7
move 13 from 5 to 9
move 1 from 5 to 4
move 3 from 4 to 7
move 1 from 1 to 7
move 14 from 9 to 2
move 2 from 9 to 3
move 3 from 8 to 5
move 4 from 3 to 4
move 8 from 4 to 1
move 7 from 1 to 9
move 5 from 6 to 9
move 4 from 9 to 2
move 1 from 1 to 9
move 17 from 2 to 4
move 1 from 6 to 3
move 4 from 7 to 5
move 5 from 7 to 5
move 1 from 6 to 4
move 1 from 8 to 3
move 5 from 7 to 1
move 2 from 7 to 6
move 2 from 3 to 6
move 1 from 2 to 9
move 7 from 9 to 6
move 2 from 3 to 7
move 8 from 6 to 4
move 3 from 9 to 2
move 1 from 6 to 4
move 26 from 4 to 8
move 2 from 7 to 8
move 5 from 5 to 9
move 2 from 6 to 7
move 4 from 9 to 1
move 2 from 7 to 5
move 14 from 8 to 6
move 3 from 2 to 8
move 3 from 6 to 8
move 3 from 6 to 1
move 10 from 8 to 4
move 5 from 9 to 4
move 3 from 8 to 5
move 1 from 8 to 2
move 12 from 4 to 8
move 1 from 9 to 3
move 6 from 6 to 4
move 6 from 8 to 2
move 1 from 3 to 8
move 1 from 8 to 4
move 10 from 1 to 9
move 2 from 1 to 3
move 7 from 4 to 9
move 1 from 2 to 1
move 11 from 8 to 9
move 1 from 3 to 9
move 2 from 2 to 7
move 1 from 3 to 6
move 2 from 7 to 9
move 2 from 4 to 6
move 4 from 6 to 4
move 2 from 2 to 8
move 2 from 8 to 4
move 1 from 1 to 7
move 2 from 2 to 8
move 9 from 5 to 2
move 3 from 5 to 9
move 1 from 8 to 3
move 30 from 9 to 7
move 1 from 6 to 2
move 7 from 4 to 8
move 13 from 7 to 2
move 8 from 7 to 4
move 2 from 4 to 8
move 8 from 8 to 1
move 1 from 8 to 3
move 2 from 8 to 9
move 1 from 3 to 7
move 5 from 7 to 6
move 1 from 3 to 1
move 7 from 4 to 8
move 20 from 2 to 6
move 2 from 2 to 7
move 1 from 9 to 5
move 4 from 7 to 6
move 3 from 7 to 8
move 1 from 7 to 2
move 7 from 8 to 6
move 3 from 6 to 7
move 4 from 9 to 1
move 1 from 2 to 6
move 1 from 9 to 7
move 1 from 2 to 8
move 1 from 7 to 6
move 3 from 6 to 3
move 4 from 8 to 1
move 8 from 6 to 4
move 3 from 7 to 2
move 1 from 3 to 2
move 1 from 4 to 5
move 2 from 3 to 5
move 1 from 4 to 6
move 4 from 1 to 5
move 4 from 2 to 9
move 2 from 1 to 6
move 4 from 9 to 2
move 3 from 2 to 8
move 2 from 8 to 4
move 13 from 6 to 1
move 4 from 5 to 2
move 14 from 6 to 3
move 1 from 2 to 7
move 2 from 2 to 4
move 1 from 8 to 6
move 1 from 6 to 3
move 1 from 7 to 4
move 1 from 2 to 3
move 1 from 2 to 6
move 11 from 4 to 6
move 2 from 5 to 4
move 1 from 5 to 6
move 12 from 3 to 6
move 1 from 3 to 7
move 1 from 5 to 7
move 3 from 3 to 6
move 2 from 7 to 5
move 2 from 5 to 2
move 8 from 6 to 7
move 24 from 1 to 3
move 1 from 4 to 6
move 10 from 3 to 1
move 6 from 1 to 8
move 1 from 6 to 3
move 1 from 4 to 2
move 1 from 3 to 1
move 2 from 2 to 1
move 1 from 7 to 6
move 2 from 7 to 5
move 4 from 3 to 7
move 1 from 2 to 3
move 6 from 1 to 6
move 3 from 7 to 5
move 4 from 7 to 8
move 1 from 1 to 2
move 1 from 2 to 7
move 8 from 3 to 4
move 3 from 4 to 7
move 6 from 8 to 6
move 2 from 3 to 2
move 1 from 3 to 9
move 5 from 5 to 1
move 2 from 8 to 2
move 1 from 9 to 2
move 4 from 1 to 3
move 3 from 2 to 9
move 1 from 1 to 2
move 2 from 9 to 7
move 2 from 2 to 9
move 8 from 7 to 5
move 33 from 6 to 5
move 20 from 5 to 9
move 21 from 5 to 7
move 17 from 7 to 6
move 10 from 6 to 9
move 5 from 4 to 7
move 2 from 3 to 9
move 1 from 2 to 3
move 2 from 7 to 3
move 3 from 9 to 5
move 23 from 9 to 7
move 8 from 9 to 6
move 1 from 9 to 1
move 1 from 5 to 3
move 1 from 8 to 9
move 5 from 6 to 8
move 1 from 9 to 6
move 18 from 7 to 2
move 6 from 7 to 4
move 6 from 4 to 8
move 5 from 7 to 4
move 6 from 6 to 3
move 1 from 4 to 2
move 10 from 2 to 1
move 1 from 2 to 4
move 7 from 1 to 6
move 1 from 7 to 1
move 11 from 6 to 2
move 1 from 6 to 8
move 12 from 3 to 1
move 8 from 1 to 8
move 2 from 5 to 2
move 12 from 8 to 6
move 15 from 2 to 4
move 7 from 4 to 5
move 4 from 5 to 9
move 4 from 9 to 4
move 5 from 4 to 6
move 2 from 5 to 2
move 1 from 2 to 5
move 2 from 5 to 4
move 2 from 1 to 3
move 4 from 1 to 5
move 2 from 8 to 4
move 5 from 2 to 9
move 17 from 6 to 8
move 1 from 3 to 2
move 2 from 5 to 4
move 1 from 3 to 8
move 1 from 1 to 6
move 2 from 5 to 6
move 3 from 9 to 5
move 1 from 5 to 1
move 3 from 1 to 8
move 26 from 8 to 4
move 1 from 5 to 3
move 3 from 2 to 7
move 1 from 5 to 7
move 21 from 4 to 9
move 19 from 4 to 5
move 3 from 4 to 3
move 2 from 7 to 5
move 1 from 8 to 2
move 1 from 6 to 2
move 1 from 8 to 9
move 1 from 6 to 7
move 1 from 2 to 4
move 1 from 4 to 7
move 1 from 2 to 7
move 1 from 7 to 1
move 1 from 1 to 6
move 1 from 3 to 5
move 2 from 6 to 3
move 13 from 5 to 8
move 1 from 4 to 2
move 3 from 5 to 4
move 5 from 5 to 4
move 5 from 8 to 9
move 9 from 9 to 3
move 2 from 7 to 1
move 6 from 4 to 2
move 8 from 9 to 4
move 1 from 2 to 7
move 12 from 9 to 8
move 1 from 4 to 2
move 3 from 7 to 3
move 11 from 8 to 5
move 5 from 8 to 6
move 3 from 6 to 5
move 2 from 4 to 1
move 13 from 5 to 3
move 1 from 1 to 7
move 2 from 1 to 8
move 3 from 4 to 9
move 1 from 1 to 7
move 1 from 2 to 4
move 2 from 7 to 3
move 1 from 5 to 3
move 4 from 4 to 2
move 1 from 4 to 9
move 30 from 3 to 2
move 1 from 9 to 7
move 6 from 8 to 6
move 1 from 7 to 6
move 1 from 5 to 1
move 1 from 3 to 5
move 30 from 2 to 3
move 1 from 1 to 9
move 2 from 9 to 2
move 9 from 6 to 9
move 2 from 2 to 9
move 1 from 5 to 1
move 5 from 9 to 7
move 8 from 2 to 5
move 1 from 1 to 9
move 3 from 9 to 1
move 5 from 3 to 6
move 8 from 5 to 9
move 13 from 3 to 9
move 3 from 1 to 7
move 5 from 7 to 9
move 17 from 9 to 6
move 1 from 7 to 6
move 6 from 3 to 9
move 1 from 2 to 1
move 2 from 7 to 1
move 1 from 2 to 5
move 21 from 9 to 2
move 4 from 3 to 6
move 6 from 6 to 5
move 7 from 5 to 9
move 2 from 3 to 8
move 3 from 1 to 3
move 4 from 6 to 5
move 1 from 8 to 1
move 1 from 8 to 2
move 4 from 5 to 2
move 4 from 9 to 1
move 4 from 3 to 5
move 2 from 1 to 7
move 1 from 7 to 4
move 3 from 9 to 5
move 25 from 2 to 9
move 18 from 9 to 1
move 1 from 4 to 5
move 1 from 3 to 8
move 4 from 5 to 6
move 2 from 9 to 3
move 17 from 1 to 5
move 1 from 2 to 7
move 2 from 3 to 5
move 3 from 1 to 8
move 5 from 9 to 2
move 4 from 8 to 9
move 12 from 5 to 2
move 1 from 1 to 8
move 3 from 9 to 5
move 1 from 8 to 2
move 2 from 7 to 2
move 1 from 9 to 5
move 9 from 5 to 2
move 6 from 6 to 2
move 15 from 6 to 2
move 5 from 5 to 9
move 1 from 5 to 9
move 3 from 9 to 2
move 3 from 9 to 1
move 1 from 1 to 9
move 1 from 9 to 1
move 19 from 2 to 8
move 2 from 1 to 9
move 33 from 2 to 6
move 4 from 6 to 4
move 1 from 2 to 6
move 1 from 9 to 8
move 3 from 4 to 8
move 18 from 8 to 3
move 1 from 4 to 9
move 10 from 3 to 9
move 1 from 1 to 4
move 24 from 6 to 3
move 1 from 4 to 3
move 2 from 8 to 7
move 8 from 9 to 3
move 5 from 6 to 7
move 35 from 3 to 2
move 7 from 7 to 1
move 3 from 1 to 3
move 33 from 2 to 6
move 6 from 3 to 7
move 5 from 7 to 3
move 1 from 1 to 4
move 1 from 7 to 8
move 1 from 4 to 8
move 1 from 3 to 2
move 30 from 6 to 5
move 2 from 1 to 6
move 5 from 8 to 1
move 1 from 9 to 2
move 2 from 6 to 4
move 4 from 1 to 7
move 21 from 5 to 8
const std = @import("std");
const PATH = "input/day05.txt";
const Str = []const u8;
const CRATE_COLS = 9;
pub fn first(allocator: std.mem.Allocator) !Str {
var stack = try parseInput(allocator, @embedFile(PATH));
defer {
for (stack.crates) |*cr| {
cr.deinit();
}
allocator.free(stack.moves);
}
for (stack.moves) |m| {
var idx: usize = 0;
while (idx < m.piece) : (idx += 1) {
const item = stack.crates[m.from].orderedRemove(stack.crates[m.from].items.len - 1);
try stack.crates[m.to].append(item);
}
}
var ret = std.ArrayList(u8).init(allocator);
defer ret.deinit();
for (stack.crates) |*col| {
try ret.append(col.orderedRemove(col.items.len - 1));
}
return try ret.toOwnedSlice();
}
pub fn second(allocator: std.mem.Allocator) !Str {
var stack = try parseInput(allocator, @embedFile(PATH));
defer {
for (stack.crates) |*cr| {
cr.deinit();
}
allocator.free(stack.moves);
}
for (stack.moves) |m| {
const items = stack.crates[m.from].items[stack.crates[m.from].items.len - m.piece ..];
try stack.crates[m.to].appendSlice(items);
var idx: usize = 0;
while (idx < m.piece) : (idx += 1) {
_ = stack.crates[m.from].orderedRemove(stack.crates[m.from].items.len - 1);
}
}
var ret = std.ArrayList(u8).init(allocator);
defer ret.deinit();
for (stack.crates) |*col| {
try ret.append(col.orderedRemove(col.items.len - 1));
}
return try ret.toOwnedSlice();
}
test "day05a" {
var ret = try first(std.testing.allocator);
defer std.testing.allocator.free(ret);
try std.testing.expectEqualStrings("FWSHSPJWM", ret);
}
test "day05b" {
var ret = try second(std.testing.allocator);
defer std.testing.allocator.free(ret);
try std.testing.expectEqualStrings("PWPWHGFZS", ret);
}
const Move = struct {
piece: usize,
from: usize,
to: usize,
};
const Stacks = struct {
crates: [CRATE_COLS]std.ArrayList(u8),
moves: []Move,
};
fn parseInput(allocator: std.mem.Allocator, in: Str) !Stacks {
var st: Stacks = undefined;
for (st.crates) |*cr| {
cr.* = std.ArrayList(u8).init(allocator);
}
// parse crates
var col: usize = 0;
var lines = std.mem.tokenize(u8, in, "\n");
LINES: while (lines.next()) |line| {
col = 0;
var idx: usize = 0;
while (idx < line.len) : (idx += 1) {
switch (line[idx]) {
' ' => {
if ((idx == 3) or (idx > 3 and (idx - 3) % 4 == 0)) col += 1;
},
'[', ']' => {},
'A'...'Z' => |crate| {
try st.crates[col].insert(0, crate);
},
else => break :LINES,
}
}
}
// parse moves
var moves = std.ArrayList(Move).init(allocator);
defer moves.deinit();
while (lines.next()) |line| {
var words = std.mem.tokenize(u8, line, " ");
var m: Move = undefined;
_ = words.next(); // move
m.piece = try std.fmt.parseUnsigned(usize, words.next().?, 10);
_ = words.next(); // from
m.from = try std.fmt.parseUnsigned(usize, words.next().?, 10) - 1;
_ = words.next(); // to
m.to = try std.fmt.parseUnsigned(usize, words.next().?, 10) - 1;
try moves.append(m);
}
st.moves = try moves.toOwnedSlice();
return st;
}
const test_input =
\\ [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
;