This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.java
102 lines (88 loc) · 3.23 KB
/
Day18.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package mineiwik.AoC_2020;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Stack;
/*
* Advent of Code 2020 - Day 18 - Operation Order
*/
public class Day18 extends Day {
private final String[] expressions;
private final ArrayList<Character> operators;
Day18(String day) throws IOException {
super(day);
expressions = input.replaceAll(" ", "").split("\\r?\\n");
operators = new ArrayList<>();
operators.add('+');
operators.add('*');
}
public String firstStar() {
long sum = 0;
for (String expression : expressions) {
Stack<Character> postfix = infixToPostfix(expression, new int[]{1, 1});
sum += calculatePostfix(postfix);
}
return String.valueOf(sum);
}
public String secondStar() {
long sum = 0;
for (String expression : expressions) {
Stack<Character> postfix = infixToPostfix(expression, new int[]{2, 1});
sum += calculatePostfix(postfix);
}
return String.valueOf(sum);
}
// Using the Shunting-yard algorithm
public Stack<Character> infixToPostfix (String expression, int[] precedence) {
Stack<Character> operator = new Stack<>();
Stack<Character> output = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
char current = expression.charAt(i);
if (Character.isDigit(current)) {
output.push(current);
} else if (operators.contains(current)) {
while (!operator.empty()
&& operator.peek() != '('
&& (precedence[operators.indexOf(operator.peek())] > precedence[operators.indexOf(current)]
|| precedence[operators.indexOf(operator.peek())] == precedence[operators.indexOf(current)])) {
output.push(operator.pop());
}
operator.push(current);
} else if (current == '(') {
operator.push(current);
} else if (current == ')') {
while (operator.peek() != '(') {
output.push(operator.pop());
}
if (operator.peek() == '(') {
operator.pop();
}
}
}
while (!operator.empty()) {
output.push(operator.pop());
}
Stack<Character> upsideDown = new Stack<>();
while (!output.empty()) upsideDown.push(output.pop());
return upsideDown;
}
public long calculatePostfix (Stack<Character> input) {
Stack<Long> temp = new Stack<>();
while(!input.empty()) {
if (Character.isDigit(input.peek())) {
temp.push((long) (input.pop() - 48));
}
else if (operators.contains(input.peek())){
char operand = input.pop();
switch (operand) {
case'+':
temp.push(temp.pop() + temp.pop());
break;
case'*':
temp.push(temp.pop() * temp.pop());
break;
}
}
}
return temp.pop();
}
}