diff --git a/2022/day05/Day05A.class b/2022/day05/Day05A.class new file mode 100644 index 0000000..369ec86 Binary files /dev/null and b/2022/day05/Day05A.class differ diff --git a/2022/day05/Day05A.java b/2022/day05/Day05A.java new file mode 100644 index 0000000..444bb54 --- /dev/null +++ b/2022/day05/Day05A.java @@ -0,0 +1,47 @@ +import java.nio.file.Paths; +import java.util.Scanner; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Stack; + +public class Day05A { + public static void main (String[] args) { + ArrayList> cargoList = new ArrayList>(); + try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) { + String cargoLine = inputFile.nextLine(); + while (!cargoLine.isEmpty()) { + Stack tempStack = new Stack(); + for (int i = 0; i < cargoLine.length(); i++) { + tempStack.push(cargoLine.charAt(i)); + } + cargoList.add(tempStack); + cargoLine = inputFile.nextLine(); + } + + while (inputFile.hasNext()) { + int[] moveInstruction = getInstructionList(inputFile.nextLine()); + for (int i = 0; i < moveInstruction[0]; i++) { + cargoList.get(moveInstruction[2]-1).push(cargoList.get(moveInstruction[1]-1).pop()); + } + } + + StringBuilder topCrates = new StringBuilder(); + for (int i = 0; i < cargoList.size(); i++) { + topCrates.append(cargoList.get(i).peek()); + } + + System.out.println(topCrates); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + public static int[] getInstructionList (String s) { + String[] instruction = s.split(" "); + int[] numericInstructions = {Integer.valueOf(instruction[1]), Integer.valueOf(instruction[3]), Integer.valueOf(instruction[5])}; + return numericInstructions; + } +} diff --git a/2022/day05/Day05B.class b/2022/day05/Day05B.class new file mode 100644 index 0000000..566d7e4 Binary files /dev/null and b/2022/day05/Day05B.class differ diff --git a/2022/day05/Day05B.java b/2022/day05/Day05B.java new file mode 100644 index 0000000..441e611 --- /dev/null +++ b/2022/day05/Day05B.java @@ -0,0 +1,52 @@ +import java.nio.file.Paths; +import java.util.Scanner; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Stack; + +public class Day05B { + public static void main (String[] args) { + ArrayList> cargoList = new ArrayList>(); + try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) { + String cargoLine = inputFile.nextLine(); + while (!cargoLine.isEmpty()) { + Stack tempStack = new Stack(); + for (int i = 0; i < cargoLine.length(); i++) { + tempStack.push(cargoLine.charAt(i)); + } + cargoList.add(tempStack); + cargoLine = inputFile.nextLine(); + } + + while (inputFile.hasNext()) { + int[] moveInstruction = getInstructionList(inputFile.nextLine()); + Stack tempStack = new Stack(); + for (int i = 0; i < moveInstruction[0]; i++) { + tempStack.push(cargoList.get(moveInstruction[1]-1).pop()); + } + + while (!tempStack.empty()) { + cargoList.get(moveInstruction[2]-1).push(tempStack.pop()); + } + } + + StringBuilder topCrates = new StringBuilder(); + for (int i = 0; i < cargoList.size(); i++) { + topCrates.append(cargoList.get(i).peek()); + } + + System.out.println(topCrates); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + public static int[] getInstructionList (String s) { + String[] instruction = s.split(" "); + int[] numericInstructions = {Integer.valueOf(instruction[1]), Integer.valueOf(instruction[3]), Integer.valueOf(instruction[5])}; + return numericInstructions; + } +} diff --git a/2022/day05/day05_algo.txt b/2022/day05/day05_algo.txt new file mode 100644 index 0000000..6ece2f2 --- /dev/null +++ b/2022/day05/day05_algo.txt @@ -0,0 +1,24 @@ +Hand-reformatted initial state input. Andy can't tell me how to live my life. + +=====Reading input state into 2D array===== +Open file +Loop - for incrementer i = 0, while line read in is not empty, i++ + Inner Loop to iterate through read string + push char to stack + End inner loop + Add now complete stack to list +End Loop + +=====processing move instructions===== +Outer Loop - while file has contents + read line into split string, array of 3 ints, position 1 is quantity, position 2 is source stack, position 3 is destination stack. (moveInstruction) + inner loop - for incrementer i = 0, while i < moveInstruction[0], i++ + push to cargoList[moveInstruction[2]] char that is popped from cargoList[moveInstruction[1]] + End inner loop +End outer loop + +loop - for incrementer i = 0, while i is less than the first dimension of the two dimensional array, i++ + peek top crate of each stack, assign to string topCratesList +end loop + +print topCratesList to screen \ No newline at end of file