From 5e5d59b58c32c648b5a384c62c3824eff18205d1 Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Mon, 5 Dec 2022 09:12:27 -0600
Subject: [PATCH] Completed Year 2022 Day 5

---
 2022/README.md                                |  7 +++++
 .../java/edu/unl/cse/bohn/year2022/Day5.java  | 30 ++++++++++++++-----
 2 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/2022/README.md b/2022/README.md
index 2398981..214ea26 100644
--- a/2022/README.md
+++ b/2022/README.md
@@ -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)
diff --git a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
index dfa5b32..7fcadf1 100644
--- a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
+++ b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day5.java
@@ -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,16 +68,28 @@ 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;
-                    char crate = stacks.get(source).pop();
-                    stacks.get(destination).push(crate);
+                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);
+                    }
                 }
             }
         }
-- 
GitLab