-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.java
174 lines (151 loc) · 5.32 KB
/
Day13.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package aoc.day.days;
import aoc.day.Day;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Day13 extends Day {
@Override
public Object part1() {
List<List<Object>> list = parseInput();
int result = 0;
int pairCount = 1;
for (int i = 0; i < list.size(); i+=2) {
if (compareTo(list.get(i), list.get(i+1))==-1) {
result += pairCount;
}
pairCount++;
}
return result;
}
@Override
public Object part2() {
List<List<Object>> list = parseInput();
List<Object> d1 = parseList("[[2]]");
List<Object> d2 = parseList("[[6]]");
list.add(d1);
list.add(d2);
Collections.sort(list, (o1, o2) -> compareTo(o1, o2));
int divider1 = 1;
int divider2 = 1;
for (int i = 0; i < list.size(); i++){
if (compareTo(list.get(i),parseList("[[2]]"))==-1) {
divider1++;
}else {
System.out.println();
}
if (compareTo(list.get(i),parseList("[[6]]"))==-1) {
divider2++;
}
}
return divider1*divider2;
}
public int compareTo(List<Object> left, List<Object> right) {
if (left.size() == right.size() && left.equals(right)) {
return 0;
}
int minLength = Math.min(left.size(), right.size());
for (int i = 0; i < minLength; i++) {
Object leftValue = left.get(i);
Object rightValue = right.get(i);
// If both values are integers, compare them directly
if (leftValue instanceof Integer && rightValue instanceof Integer) {
int leftInt = (Integer) leftValue;
int rightInt = (Integer) rightValue;
if (leftInt < rightInt) {
return -1;
} else if (leftInt > rightInt) {
return 1;
}
}
// If both values are lists, compare them recursively
if (leftValue instanceof List && rightValue instanceof List) {
int result = compareTo((List<Object>) leftValue, (List<Object>) rightValue);
if (result != 0) {
return result;
}
}
// If exactly one value is an integer, convert it to a list and retry the comparison
if (leftValue instanceof Integer && rightValue instanceof List) {
List<Object> leftList = new ArrayList<Object>();
leftList.add(leftValue);
int result = compareTo(leftList, (List<Object>) rightValue);
if (result != 0) {
return result;
}
}
if (leftValue instanceof List && rightValue instanceof Integer) {
List<Object> rightList = new ArrayList<Object>();
rightList.add(rightValue);
int result = compareTo((List<Object>) leftValue, rightList);
if (result != 0) {
return result;
}
}
}
if (left.size() < right.size()) {
return -1;
} else if (left.size() > right.size()) {
return 1;
}else {
return 0;
}
}
public List<List<Object>> parseInput(){
List<String> rawInput = getDayInput();
List<List<Object>> list = new ArrayList<>();
for (int i = 0; i < rawInput.size(); i++) {
if (!rawInput.get(i).equals("")){
list.add(parseList(rawInput.get(i)));
}
}
return list;
}
public int findClosingBracket(int startIndex, String rawInput){
int j = startIndex;
int closedBrackets = 0;
do{
if (rawInput.charAt(j) == '[') {
closedBrackets++;
} else if (rawInput.charAt(j) == ']') {
closedBrackets--;
}
j++;
}while (closedBrackets != 0);
return j;
}
public List<Object> parseList(String input) {
//parse input
//[[1],[2,3,4]]
if (input.charAt(0) == '[' && input.charAt(input.length() - 1) == ']') {
input = input.substring(1, input.length() - 1);
}
String numberChars = "";
List<Object> list = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '[') {
int endIndex = findClosingBracket(i, input);
String substring = input.substring(i, endIndex);
list.add(parseList(substring));
i = endIndex;
} else if (c == ']') {
//end of list
System.out.println("ERROR end of list should not happen");
} else if (c == ',') {
//end of number
if (!numberChars.equals("")) {
list.add(Integer.parseInt(numberChars));
numberChars = "";
}
numberChars="";
} else {
//number
numberChars += c;
}
}
if (!numberChars.equals("")) {
list.add(Integer.parseInt(numberChars));
}
return list;
}
}