-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5solution.java
More file actions
80 lines (77 loc) · 2.57 KB
/
Copy pathday5solution.java
File metadata and controls
80 lines (77 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class day5 {
public static int len = 9;
public static List<String[]> commands;
public static void main(String[] args) throws FileNotFoundException {
commands = commands(getinput("day5input"));
List<Stack<String>> table = inputStacks(getinput("day5Table"));
part1(table);//A,X=rock, B,Y =paper, C,Z=Scissors
table = inputStacks(getinput("day5Table"));
part2(table);
}
public static void part1(List<Stack<String>> table) {
for(String[] c: commands) {
for(int i=0; i < Integer.valueOf(c[0]);i++) {
table.get(Integer.valueOf(c[2])-1).push(table.get(Integer.valueOf(c[1])-1).pop());
}
}
for(Stack<String> stack : table) {
System.out.print(stack.peek());
}
}
public static void part2(List<Stack<String>> table) {
List<String> crates = new ArrayList<String>();
for(String[] c: commands) {
crates = new ArrayList<String>();
for(int i=0; i < Integer.valueOf(c[0]); i++) {
crates.add(table.get(Integer.valueOf(c[1])-1).pop());
}
for(int i=Integer.valueOf(c[0]); i > 0 ;i--) {
table.get(Integer.valueOf(c[2])-1).push(crates.get(i-1));
}
}
System.out.println();
for(Stack<String> stack : table) {
System.out.print(stack.peek());
}
}
public static List<String[]> commands(List<String> input){
List<String[]> commands = new ArrayList<String[]>();
for(String line : input) {
line = line.replaceAll("move ", "");
line = line.replaceAll("from ", "");
line = line.replaceAll("to ","");
commands.add(line.split("[\s]+"));
}
return commands;
}
public static List<Stack<String>> inputStacks(List<String> tableInput){
List<Stack<String>> table = new ArrayList<Stack<String>>();
String r = "";
for(int i = 0; i < len; i++) {
table.add(new Stack<String>());
}
for(int i = tableInput.size()-2; i>-1;i--) {//loop through each row
r = tableInput.get(i).replaceAll(" ", "[e] ").replaceAll("]", "").replaceAll("\\[", "").replace(" ", "");
for(int j = 0; j<r.length();j++) {
if(r.charAt(j) != 'e') {
table.get(j).push(String.valueOf(r.charAt(j)));//add letter to the stack
}
}
}
return table;
}
public static List<String> getinput(String filename) throws FileNotFoundException {
Scanner reader = new Scanner(new File("C:\\Users\\USER\\eclipse-workspace\\advent2022\\src\\inputs\\"+filename+".txt"));
List<String> list = new ArrayList<String>();
while (reader.hasNextLine())
list.add(reader.nextLine());
return list;
}
}
}