Skip to content
Snippets Groups Projects
Commit 5e5d59b5 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Completed Year 2022 Day 5

parent 64c5ea1e
No related branches found
No related tags found
No related merge requests found
......@@ -150,8 +150,15 @@ Why do I have the feeling that there'll be some version of
### Part 2
The difference here is how we move multiple crates from one stack to another.
### Refactoring opportunity
With the difference between the two problems occurring inside a nested loop, extracting the commonality into a helper
method (more than I've already done) isn't going to work. Passing in a pointer function would do nicely, but doing the
Java equivalent is a PITA for such a small payoff. I think I'll use a boolean to indicate which of two crate movement
techniques should be used.
## Day 6
(coming soon)
......
......@@ -27,7 +27,7 @@ public class Day5 extends Puzzle {
@Override
public long computePart1(List<String> data) {
List<Stack<Character>> stacks = createStacks(data);
rearrangeCrates(stacks, data);
rearrangeCrates(stacks, data, true);
String topsOfStacks = getTopsOfStacks(stacks);
System.out.println("Crates at the top of the stacks: " + topsOfStacks);
return 0;
......@@ -35,6 +35,10 @@ public class Day5 extends Puzzle {
@Override
public long computePart2(List<String> data) {
List<Stack<Character>> stacks = createStacks(data);
rearrangeCrates(stacks, data, false);
String topsOfStacks = getTopsOfStacks(stacks);
System.out.println("Crates at the top of the stacks: " + topsOfStacks);
return 0;
}
......@@ -64,20 +68,32 @@ public class Day5 extends Puzzle {
return Collections.unmodifiableList(stacks);
}
private void rearrangeCrates(List<Stack<Character>> stacks, List<String> data) {
private void rearrangeCrates(List<Stack<Character>> stacks, List<String> data, boolean moveOneCrateAtATime) {
Stack<Character> crane = new Stack<>();
for (String instruction : data) {
if (instruction.startsWith("move")) { // then it really is an instruction
String[] tokens = instruction.strip().split(" ");
int numberOfCratesToMove = Integer.parseInt(tokens[1]);
for (int i = 0; i < numberOfCratesToMove; i++) {
int source = Integer.parseInt(tokens[3]) - 1; // -1 because the original stacks were 1-indexed
int destination = Integer.parseInt(tokens[5]) - 1;
if (moveOneCrateAtATime) {
for (int i = 0; i < numberOfCratesToMove; i++) {
char crate = stacks.get(source).pop();
stacks.get(destination).push(crate);
}
} else {
for (int i = 0; i < numberOfCratesToMove; i++) {
char crate = stacks.get(source).pop();
crane.push(crate);
}
while (!crane.empty()) {
char crate = crane.pop();
stacks.get(destination).push(crate);
}
}
}
}
}
private String getTopsOfStacks(List<Stack<Character>> stacks) {
StringBuilder stringBuilder = new StringBuilder();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment