adventofcode/2022/day05/Day05A.java
Fennel Kora 50165e756d 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
2022-12-05 15:50:55 -05:00

47 lines
1.7 KiB
Java

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;
}
}