#import "Basic";
solve_day5 :: (test: bool) {
contents := read_entire_file(ifx test then "inputs/day5_test.txt" else "inputs/day5.txt");
sections := split(contents, "\n\n");
crates := split(sections[0], "\n");
instructions_split := split(sections[1], "\n");
columns_part1: [..][..]u8;
columns_part2: [..][..]u8;
instructions: [..]Crate_Instruction;
for 0..crates[0].count / 4 {
dummy: [..]u8;
array_add(*columns_part1, dummy);
array_add(*columns_part2, dummy);
}
ss := copy_string(" ");
for < row: array_view(crates, 0, crates.count - 1) {
for crate_id: 0..crates[0].count / 4 {
if row[4 * crate_id + 1] != #char " " {
ss[0] = row[4 * crate_id + 1];
array_add(*columns_part1[crate_id], row[4 * crate_id + 1]);
array_add(*columns_part2[crate_id], row[4 * crate_id + 1]);
}
}
}
for instruction_split: instructions_split {
splits := split(instruction_split, cast(u8)#char " ",, temp);
instruction := Crate_Instruction.{
move = string_to_int(splits[1], 10, u8),
from = string_to_int(splits[3], 10, u8) - 1,
to = string_to_int(splits[5], 10, u8) - 1,
};
array_add(*instructions, instruction);
}
part1 := 0;
part2 := 0;
for instruction: instructions {
for 0..instruction.move - 1 {
c := columns_part1[instruction.from].count;
if c != 0 {
array_add(*columns_part1[instruction.to], columns_part1[instruction.from][c - 1]);
array_unordered_remove_by_index(*columns_part1[instruction.from], c - 1);
}
}
p2_from := *columns_part2[instruction.from];
array_add(*columns_part2[instruction.to], ..array_view(<< p2_from, max(0, p2_from.count - instruction.move), instruction.move));
array_resize(p2_from, max(0, p2_from.count - instruction.move));
}
print("Part 1: ");
for columns_part1 {
ss[0] = it[it.count - 1];
print("%", ss);
}
print("\nPart 2: ");
for columns_part2 {
ss[0] = it[it.count - 1];
print("%", ss);
}
print("\n");
}
Crate_Instruction :: struct {
move: u8;
from: u8;
to: u8;
}