add binaries and code for 2022 Day 5
Changes to be committed: new file: 2022/day05/Day05A.class new file: 2022/day05/Day05A.java new file: 2022/day05/Day05B.class new file: 2022/day05/Day05B.java new file: 2022/day05/day05_algo.txt
This commit is contained in:
parent
2253aa6c07
commit
50165e756d
5 changed files with 123 additions and 0 deletions
BIN
2022/day05/Day05A.class
Normal file
BIN
2022/day05/Day05A.class
Normal file
Binary file not shown.
47
2022/day05/Day05A.java
Normal file
47
2022/day05/Day05A.java
Normal file
|
@ -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<Stack<Character>> cargoList = new ArrayList<Stack<Character>>();
|
||||
try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) {
|
||||
String cargoLine = inputFile.nextLine();
|
||||
while (!cargoLine.isEmpty()) {
|
||||
Stack<Character> tempStack = new Stack<Character>();
|
||||
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;
|
||||
}
|
||||
}
|
BIN
2022/day05/Day05B.class
Normal file
BIN
2022/day05/Day05B.class
Normal file
Binary file not shown.
52
2022/day05/Day05B.java
Normal file
52
2022/day05/Day05B.java
Normal file
|
@ -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<Stack<Character>> cargoList = new ArrayList<Stack<Character>>();
|
||||
try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) {
|
||||
String cargoLine = inputFile.nextLine();
|
||||
while (!cargoLine.isEmpty()) {
|
||||
Stack<Character> tempStack = new Stack<Character>();
|
||||
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<Character> tempStack = new Stack<Character>();
|
||||
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;
|
||||
}
|
||||
}
|
24
2022/day05/day05_algo.txt
Normal file
24
2022/day05/day05_algo.txt
Normal file
|
@ -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
|
Loading…
Reference in a new issue