-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay12.java
135 lines (118 loc) · 3.15 KB
/
Day12.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
package com.sbaars.adventofcode.year15.days;
import com.sbaars.adventofcode.year15.Day2015;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day12 extends Day2015 {
public Day12() {
super(12);
}
public static void main(String[] args) {
Day12 day = new Day12();
day.printParts();
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 12, 1);
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 12, 2);
}
@Override
public Object part1() {
Pattern pattern = Pattern.compile("-?\\d+");
Matcher matcher = pattern.matcher(day());
int sum = 0;
while (matcher.find()) {
sum += Integer.parseInt(matcher.group());
}
return sum;
}
@Override
public Object part2() {
return sumJson(day());
}
private int sumJson(String json) {
if (json.startsWith("[")) {
return sumArray(json);
} else if (json.startsWith("{")) {
return sumObject(json);
} else {
try {
return Integer.parseInt(json);
} catch (NumberFormatException e) {
return 0;
}
}
}
private int sumArray(String json) {
int sum = 0;
int depth = 0;
int start = 1;
boolean inString = false;
for (int i = 1; i < json.length() - 1; i++) {
char c = json.charAt(i);
if (c == '"') {
inString = !inString;
}
if (inString) continue;
if (c == '[' || c == '{') {
depth++;
} else if (c == ']' || c == '}') {
depth--;
} else if (c == ',' && depth == 0) {
sum += sumJson(json.substring(start, i).trim());
start = i + 1;
}
}
if (start < json.length() - 1) {
sum += sumJson(json.substring(start, json.length() - 1).trim());
}
return sum;
}
private int sumObject(String json) {
int sum = 0;
int depth = 0;
int start = 1;
boolean inString = false;
boolean hasRed = false;
StringBuilder currentValue = new StringBuilder();
for (int i = 1; i < json.length() - 1; i++) {
char c = json.charAt(i);
if (c == '"') {
if (!inString && depth == 0) {
currentValue.setLength(0);
}
inString = !inString;
continue;
}
if (inString) {
currentValue.append(c);
continue;
}
if (c == '[' || c == '{') {
depth++;
} else if (c == ']' || c == '}') {
depth--;
} else if (c == ':' && depth == 0) {
String value = currentValue.toString();
currentValue.setLength(0);
start = i + 1;
} else if (c == ',' && depth == 0) {
String value = json.substring(start, i).trim();
if (value.equals("\"red\"")) {
return 0;
}
if (!value.isEmpty()) {
sum += sumJson(value);
}
start = i + 1;
}
}
// Process the last value
if (start < json.length() - 1) {
String value = json.substring(start, json.length() - 1).trim();
if (value.equals("\"red\"")) {
return 0;
}
if (!value.isEmpty()) {
sum += sumJson(value);
}
}
return sum;
}
}