Solve day 5

This commit is contained in:
Tyler Hallada 2022-12-05 16:49:47 -05:00
parent b3eb61cc5f
commit 9fb88be5f4
4 changed files with 695 additions and 2 deletions

162
src/day05.zig Normal file
View File

@ -0,0 +1,162 @@
const std = @import("std");
pub const input = @embedFile("input/day05.txt");
const test_input = @embedFile("input/day05_test1.txt");
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
const allocator = gpa.allocator();
const Stack = std.SinglyLinkedList(u8);
fn print_stacks(stacks: []Stack) void {
var i: usize = 0;
for (stacks) |stack| {
std.debug.print("{}: ", .{i + 1});
print_stack(stack);
i += 1;
}
}
fn print_stack(stack: Stack) void {
if (stack.first) |first| {
print_node(first);
} else {
std.debug.print("<empty>\n", .{});
}
}
fn print_node(node: *Stack.Node) void {
std.debug.print("{s}", .{&[_]u8{node.data}});
if (node.next) |next| {
std.debug.print(" -> ", .{});
print_node(next);
} else {
std.debug.print("\n", .{});
}
}
fn parse_stacks(lines: *std.mem.TokenIterator(u8)) ![9]Stack {
// TODO: is there a better way to initialize this?
var stacks: [9]Stack = [9]Stack{
Stack{},
Stack{},
Stack{},
Stack{},
Stack{},
Stack{},
Stack{},
Stack{},
Stack{},
};
while (lines.next()) |line| {
var i: usize = 0;
while (i < line.len) : (i += 4) {
const slot = line[i .. i + 3];
if (slot[0] == '[') {
const first = stacks[i / 4].first;
if (first) |f| {
var new_node = try allocator.create(Stack.Node);
new_node.* = Stack.Node{ .data = slot[1] };
f.findLast().insertAfter(new_node);
} else {
var new_node = try allocator.create(Stack.Node);
new_node.* = Stack.Node{ .data = slot[1] };
stacks[i / 4].prepend(new_node);
}
}
}
}
return stacks;
}
const Move = struct {
crates: usize,
from: usize,
to: usize,
};
fn parse_move(line: []const u8) !Move {
var parts = std.mem.tokenize(u8, line, " ");
_ = parts.next();
const crates = try std.fmt.parseInt(usize, parts.next().?, 10);
_ = parts.next();
const from = try std.fmt.parseInt(usize, parts.next().?, 10);
_ = parts.next();
const to = try std.fmt.parseInt(usize, parts.next().?, 10);
return Move{
.crates = crates,
.from = from - 1,
.to = to - 1,
};
}
fn get_top_crate_str(stacks: [9]Stack) ![]const u8 {
var message = std.ArrayList(u8).init(allocator);
var i: usize = 0;
while (i < 9) : (i += 1) {
if (stacks[i].first) |first| {
try message.append(first.data);
}
}
return message.toOwnedSlice();
}
pub fn solve_part1(data: []const u8) ![]const u8 {
var lines = std.mem.split(u8, data, "\n\n");
var stack_lines = std.mem.tokenize(u8, lines.next().?, "\n");
var stacks = try parse_stacks(&stack_lines);
var move_lines = std.mem.tokenize(u8, lines.next().?, "\n");
while (move_lines.next()) |line| {
const move = try parse_move(line);
var i: usize = 0;
while (i < move.crates) : (i += 1) {
const crate = stacks[move.from].popFirst().?;
stacks[move.to].prepend(crate);
}
}
return try get_top_crate_str(stacks);
}
pub fn solve_part2(data: []const u8) ![]const u8 {
var lines = std.mem.split(u8, data, "\n\n");
var stack_lines = std.mem.tokenize(u8, lines.next().?, "\n");
var stacks = try parse_stacks(&stack_lines);
var move_lines = std.mem.tokenize(u8, lines.next().?, "\n");
while (move_lines.next()) |line| {
const move = try parse_move(line);
// print_stacks(&stacks);
var i: usize = 0;
var temp = Stack{};
while (i < move.crates) : (i += 1) {
const crate = stacks[move.from].popFirst().?;
const first = temp.first;
if (first) |f| {
f.findLast().insertAfter(crate);
} else {
temp.prepend(crate);
}
// print_stack(temp);
}
if (stacks[move.to].first) |to_first| {
temp.first.?.findLast().next = to_first;
}
stacks[move.to].first = temp.first;
}
return try get_top_crate_str(stacks);
}
test "solves part1" {
try std.testing.expectEqualStrings(try solve_part1(test_input), "CMZ");
}
test "solves part2" {
try std.testing.expectEqualStrings(try solve_part2(test_input), "MCD");
}

511
src/input/day05.txt Normal file
View File

@ -0,0 +1,511 @@
[N] [R] [C]
[T] [J] [S] [J] [N]
[B] [Z] [H] [M] [Z] [D]
[S] [P] [G] [L] [H] [Z] [T]
[Q] [D] [F] [D] [V] [L] [S] [M]
[H] [F] [V] [J] [C] [W] [P] [W] [L]
[G] [S] [H] [Z] [Z] [T] [F] [V] [H]
[R] [H] [Z] [M] [T] [M] [T] [Q] [W]
1 2 3 4 5 6 7 8 9
move 3 from 9 to 7
move 4 from 4 to 5
move 2 from 4 to 6
move 4 from 7 to 5
move 3 from 7 to 3
move 2 from 5 to 9
move 5 from 6 to 3
move 5 from 9 to 1
move 3 from 8 to 4
move 3 from 4 to 6
move 8 from 1 to 8
move 1 from 8 to 6
move 2 from 8 to 2
move 5 from 8 to 4
move 1 from 8 to 1
move 6 from 6 to 4
move 1 from 7 to 9
move 5 from 1 to 7
move 1 from 1 to 2
move 2 from 9 to 8
move 6 from 4 to 9
move 1 from 6 to 8
move 3 from 2 to 7
move 4 from 2 to 8
move 4 from 9 to 3
move 6 from 5 to 4
move 7 from 8 to 1
move 10 from 4 to 1
move 12 from 1 to 5
move 1 from 4 to 9
move 1 from 2 to 3
move 2 from 9 to 1
move 1 from 9 to 3
move 1 from 6 to 7
move 1 from 9 to 1
move 3 from 1 to 3
move 9 from 5 to 9
move 2 from 2 to 7
move 2 from 7 to 4
move 3 from 9 to 4
move 7 from 5 to 7
move 5 from 1 to 3
move 2 from 4 to 5
move 1 from 4 to 6
move 1 from 6 to 9
move 4 from 9 to 2
move 12 from 7 to 9
move 2 from 4 to 9
move 6 from 5 to 9
move 3 from 7 to 6
move 12 from 9 to 6
move 5 from 9 to 1
move 1 from 7 to 6
move 14 from 6 to 1
move 20 from 3 to 5
move 5 from 9 to 5
move 3 from 2 to 8
move 1 from 6 to 4
move 1 from 9 to 2
move 1 from 4 to 6
move 1 from 2 to 6
move 16 from 1 to 5
move 1 from 2 to 1
move 12 from 5 to 6
move 1 from 8 to 4
move 29 from 5 to 1
move 5 from 6 to 9
move 20 from 1 to 3
move 4 from 1 to 3
move 11 from 3 to 8
move 1 from 4 to 3
move 4 from 9 to 8
move 7 from 1 to 8
move 2 from 3 to 2
move 2 from 6 to 7
move 1 from 9 to 8
move 10 from 3 to 5
move 1 from 6 to 1
move 1 from 7 to 2
move 3 from 1 to 2
move 6 from 2 to 4
move 2 from 6 to 3
move 4 from 6 to 5
move 1 from 6 to 2
move 1 from 2 to 9
move 6 from 5 to 2
move 1 from 9 to 3
move 24 from 8 to 7
move 1 from 4 to 8
move 5 from 5 to 4
move 1 from 4 to 8
move 1 from 8 to 7
move 2 from 8 to 9
move 1 from 9 to 7
move 6 from 2 to 4
move 10 from 3 to 7
move 3 from 5 to 3
move 1 from 9 to 8
move 3 from 3 to 8
move 4 from 8 to 7
move 1 from 4 to 6
move 1 from 6 to 4
move 13 from 4 to 3
move 17 from 7 to 6
move 1 from 6 to 3
move 2 from 4 to 8
move 3 from 7 to 5
move 14 from 6 to 7
move 1 from 5 to 9
move 1 from 5 to 9
move 2 from 6 to 7
move 1 from 5 to 1
move 1 from 1 to 6
move 1 from 9 to 3
move 29 from 7 to 4
move 10 from 4 to 3
move 6 from 7 to 5
move 1 from 6 to 5
move 1 from 9 to 7
move 1 from 7 to 2
move 4 from 3 to 2
move 1 from 2 to 9
move 1 from 8 to 5
move 11 from 3 to 4
move 24 from 4 to 7
move 2 from 2 to 5
move 10 from 3 to 2
move 6 from 2 to 1
move 5 from 4 to 7
move 1 from 9 to 2
move 3 from 5 to 1
move 1 from 4 to 6
move 4 from 2 to 3
move 5 from 5 to 7
move 2 from 5 to 3
move 32 from 7 to 5
move 16 from 5 to 1
move 1 from 1 to 2
move 3 from 2 to 9
move 1 from 8 to 6
move 3 from 7 to 6
move 1 from 2 to 4
move 5 from 6 to 8
move 5 from 8 to 6
move 2 from 9 to 3
move 1 from 7 to 5
move 9 from 5 to 4
move 1 from 9 to 1
move 2 from 3 to 1
move 4 from 3 to 6
move 1 from 3 to 8
move 6 from 4 to 6
move 6 from 5 to 9
move 1 from 9 to 6
move 1 from 5 to 1
move 1 from 5 to 4
move 1 from 3 to 6
move 1 from 8 to 3
move 1 from 4 to 2
move 1 from 2 to 3
move 17 from 6 to 4
move 4 from 1 to 8
move 3 from 9 to 6
move 1 from 8 to 4
move 1 from 9 to 7
move 2 from 6 to 2
move 1 from 7 to 8
move 12 from 1 to 9
move 8 from 9 to 2
move 1 from 6 to 9
move 6 from 2 to 8
move 2 from 8 to 3
move 18 from 4 to 9
move 2 from 1 to 6
move 1 from 6 to 5
move 3 from 4 to 3
move 7 from 3 to 8
move 4 from 2 to 7
move 1 from 4 to 6
move 2 from 6 to 4
move 13 from 9 to 6
move 1 from 5 to 2
move 5 from 9 to 3
move 9 from 1 to 2
move 1 from 1 to 8
move 1 from 2 to 6
move 3 from 7 to 6
move 2 from 2 to 6
move 9 from 8 to 6
move 1 from 7 to 8
move 1 from 8 to 7
move 2 from 4 to 6
move 5 from 3 to 6
move 17 from 6 to 9
move 7 from 8 to 4
move 4 from 2 to 3
move 17 from 6 to 2
move 1 from 6 to 4
move 1 from 7 to 8
move 1 from 8 to 9
move 24 from 9 to 6
move 4 from 3 to 1
move 1 from 1 to 5
move 20 from 6 to 4
move 4 from 6 to 9
move 1 from 5 to 7
move 2 from 4 to 2
move 1 from 9 to 7
move 25 from 4 to 3
move 1 from 4 to 2
move 2 from 1 to 6
move 3 from 9 to 4
move 2 from 4 to 7
move 2 from 7 to 5
move 1 from 4 to 2
move 1 from 6 to 3
move 1 from 1 to 5
move 5 from 3 to 9
move 1 from 5 to 6
move 10 from 2 to 8
move 9 from 2 to 5
move 21 from 3 to 6
move 1 from 7 to 6
move 2 from 6 to 5
move 5 from 9 to 7
move 6 from 7 to 8
move 19 from 6 to 9
move 1 from 6 to 1
move 8 from 8 to 1
move 1 from 6 to 1
move 2 from 8 to 5
move 5 from 9 to 2
move 6 from 8 to 2
move 2 from 9 to 7
move 9 from 9 to 4
move 7 from 2 to 4
move 1 from 6 to 4
move 14 from 5 to 9
move 1 from 1 to 8
move 1 from 7 to 9
move 4 from 2 to 9
move 16 from 4 to 6
move 3 from 2 to 8
move 1 from 6 to 2
move 2 from 8 to 9
move 1 from 8 to 7
move 1 from 8 to 3
move 3 from 2 to 7
move 1 from 3 to 9
move 8 from 9 to 3
move 4 from 7 to 8
move 1 from 5 to 4
move 4 from 6 to 3
move 1 from 4 to 2
move 9 from 3 to 8
move 10 from 9 to 5
move 8 from 6 to 7
move 13 from 8 to 4
move 8 from 5 to 2
move 3 from 6 to 3
move 7 from 9 to 6
move 7 from 7 to 2
move 2 from 4 to 6
move 5 from 6 to 2
move 3 from 1 to 5
move 5 from 5 to 8
move 4 from 6 to 2
move 4 from 1 to 8
move 15 from 2 to 6
move 11 from 4 to 9
move 12 from 6 to 8
move 1 from 6 to 9
move 5 from 3 to 7
move 2 from 2 to 6
move 6 from 7 to 1
move 3 from 1 to 3
move 1 from 4 to 1
move 1 from 3 to 9
move 1 from 3 to 9
move 1 from 7 to 6
move 1 from 3 to 2
move 4 from 2 to 6
move 4 from 2 to 7
move 1 from 2 to 6
move 4 from 1 to 6
move 12 from 6 to 7
move 2 from 6 to 1
move 8 from 9 to 6
move 1 from 7 to 4
move 14 from 8 to 1
move 8 from 1 to 5
move 1 from 3 to 9
move 5 from 9 to 5
move 1 from 8 to 9
move 1 from 9 to 2
move 1 from 9 to 3
move 5 from 8 to 3
move 12 from 5 to 4
move 1 from 9 to 2
move 6 from 7 to 3
move 7 from 3 to 2
move 1 from 5 to 1
move 1 from 8 to 3
move 2 from 1 to 3
move 2 from 6 to 9
move 5 from 6 to 5
move 5 from 1 to 7
move 4 from 4 to 1
move 7 from 2 to 8
move 4 from 3 to 8
move 1 from 9 to 3
move 1 from 9 to 5
move 4 from 1 to 8
move 10 from 7 to 9
move 1 from 6 to 7
move 2 from 8 to 6
move 6 from 4 to 2
move 5 from 3 to 1
move 2 from 6 to 3
move 2 from 7 to 1
move 5 from 2 to 5
move 2 from 7 to 1
move 7 from 5 to 7
move 2 from 5 to 6
move 2 from 5 to 3
move 3 from 2 to 9
move 9 from 9 to 3
move 1 from 6 to 4
move 3 from 3 to 1
move 9 from 8 to 2
move 6 from 3 to 6
move 8 from 7 to 9
move 4 from 9 to 8
move 14 from 1 to 5
move 1 from 9 to 2
move 1 from 1 to 5
move 2 from 3 to 6
move 12 from 5 to 3
move 2 from 2 to 8
move 7 from 6 to 2
move 12 from 2 to 8
move 2 from 6 to 2
move 6 from 9 to 6
move 1 from 1 to 2
move 1 from 9 to 3
move 2 from 5 to 9
move 1 from 9 to 2
move 1 from 9 to 4
move 1 from 3 to 2
move 2 from 6 to 7
move 2 from 6 to 9
move 5 from 4 to 2
move 14 from 3 to 9
move 15 from 9 to 4
move 1 from 7 to 4
move 10 from 8 to 6
move 1 from 5 to 9
move 2 from 9 to 5
move 10 from 8 to 1
move 1 from 7 to 4
move 5 from 1 to 2
move 2 from 1 to 5
move 3 from 4 to 6
move 4 from 5 to 8
move 5 from 8 to 6
move 14 from 2 to 9
move 2 from 6 to 7
move 3 from 2 to 9
move 3 from 1 to 7
move 1 from 7 to 3
move 3 from 7 to 1
move 1 from 3 to 6
move 1 from 7 to 6
move 1 from 8 to 9
move 2 from 1 to 4
move 1 from 1 to 2
move 16 from 9 to 4
move 7 from 4 to 8
move 5 from 8 to 1
move 2 from 8 to 3
move 2 from 1 to 7
move 13 from 6 to 7
move 2 from 2 to 3
move 4 from 7 to 4
move 6 from 4 to 5
move 4 from 7 to 6
move 3 from 1 to 2
move 2 from 2 to 6
move 3 from 3 to 8
move 5 from 5 to 3
move 2 from 9 to 6
move 3 from 3 to 7
move 1 from 8 to 1
move 22 from 4 to 8
move 1 from 4 to 3
move 9 from 6 to 3
move 1 from 2 to 1
move 4 from 3 to 4
move 2 from 4 to 5
move 1 from 1 to 7
move 4 from 3 to 7
move 2 from 6 to 1
move 1 from 6 to 7
move 18 from 8 to 7
move 2 from 6 to 5
move 2 from 3 to 4
move 1 from 5 to 4
move 30 from 7 to 6
move 2 from 1 to 3
move 18 from 6 to 8
move 12 from 6 to 4
move 13 from 4 to 9
move 2 from 3 to 8
move 1 from 6 to 2
move 3 from 7 to 2
move 1 from 1 to 2
move 2 from 5 to 9
move 8 from 8 to 1
move 1 from 7 to 8
move 7 from 1 to 3
move 2 from 4 to 9
move 1 from 1 to 6
move 4 from 2 to 1
move 16 from 8 to 1
move 1 from 2 to 6
move 2 from 4 to 8
move 2 from 5 to 1
move 4 from 3 to 7
move 3 from 7 to 1
move 1 from 6 to 8
move 1 from 8 to 9
move 1 from 7 to 3
move 6 from 3 to 5
move 1 from 3 to 8
move 1 from 6 to 9
move 16 from 9 to 5
move 4 from 5 to 3
move 15 from 5 to 1
move 1 from 5 to 8
move 3 from 9 to 8
move 9 from 8 to 5
move 6 from 5 to 1
move 4 from 5 to 6
move 2 from 6 to 4
move 1 from 6 to 4
move 1 from 8 to 4
move 3 from 3 to 6
move 3 from 6 to 8
move 1 from 6 to 8
move 21 from 1 to 9
move 4 from 8 to 5
move 3 from 5 to 7
move 2 from 5 to 1
move 2 from 4 to 8
move 2 from 8 to 2
move 2 from 7 to 8
move 1 from 7 to 9
move 1 from 8 to 7
move 5 from 1 to 8
move 1 from 7 to 8
move 4 from 8 to 4
move 2 from 4 to 5
move 1 from 2 to 7
move 1 from 2 to 7
move 2 from 7 to 6
move 2 from 6 to 9
move 1 from 4 to 9
move 1 from 3 to 4
move 16 from 1 to 5
move 16 from 5 to 7
move 2 from 5 to 4
move 14 from 9 to 6
move 5 from 4 to 3
move 3 from 3 to 6
move 5 from 1 to 4
move 2 from 4 to 7
move 7 from 9 to 4
move 2 from 9 to 7
move 10 from 6 to 9
move 8 from 4 to 6
move 1 from 8 to 4
move 1 from 1 to 9
move 14 from 6 to 3
move 10 from 3 to 2
move 3 from 7 to 8
move 6 from 3 to 1
move 2 from 7 to 9
move 5 from 7 to 9
move 10 from 9 to 1
move 2 from 4 to 3
move 1 from 2 to 1
move 16 from 1 to 4
move 1 from 6 to 1
move 2 from 3 to 9
move 3 from 8 to 5
move 8 from 7 to 1
move 3 from 5 to 9
move 7 from 4 to 6
move 7 from 1 to 5
move 2 from 8 to 3
move 1 from 7 to 8

View File

@ -0,0 +1,9 @@
[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

View File

@ -4,6 +4,7 @@ const day01 = @import("day01.zig");
const day02 = @import("day02.zig"); const day02 = @import("day02.zig");
const day03 = @import("day03.zig"); const day03 = @import("day03.zig");
const day04 = @import("day04.zig"); const day04 = @import("day04.zig");
const day05 = @import("day05.zig");
const utils = @import("utils.zig"); const utils = @import("utils.zig");
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void { fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
@ -13,13 +14,21 @@ fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.ti
var part1 = try day.solve_part1(day.input); var part1 = try day.solve_part1(day.input);
const part1_end = timer.read(); const part1_end = timer.read();
const part1_time = utils.HumanTime.new(part1_end - part1_start); const part1_time = utils.HumanTime.new(part1_end - part1_start);
if (@TypeOf(part1) == []const u8) {
try stdout.print("Part 1: {s} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
} else {
try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() }); try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
}
const part2_start = timer.read(); const part2_start = timer.read();
var part2 = try day.solve_part2(day.input); var part2 = try day.solve_part2(day.input);
const part2_end = timer.read(); const part2_end = timer.read();
const part2_time = utils.HumanTime.new(part2_end - part2_start); const part2_time = utils.HumanTime.new(part2_end - part2_start);
if (@TypeOf(part2) == []const u8) {
try stdout.print("Part 2: {s} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
} else {
try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() }); try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
}
} }
pub fn main() !void { pub fn main() !void {
@ -32,6 +41,7 @@ pub fn main() !void {
try solve_day(2, day02, &stdout, &timer); try solve_day(2, day02, &stdout, &timer);
try solve_day(3, day03, &stdout, &timer); try solve_day(3, day03, &stdout, &timer);
try solve_day(4, day04, &stdout, &timer); try solve_day(4, day04, &stdout, &timer);
try solve_day(5, day05, &stdout, &timer);
try bw.flush(); try bw.flush();
} }
@ -43,5 +53,6 @@ test {
_ = day02; _ = day02;
_ = day03; _ = day03;
_ = day04; _ = day04;
_ = day05;
_ = utils; _ = utils;
} }