Skip to content

Commit ec6728b

Browse files
committed
d12 progress
1 parent d669796 commit ec6728b

File tree

1 file changed

+100
-3
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+100
-3
lines changed
Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,120 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
46

57
public class Day12 extends Day2015 {
8+
69
public Day12() {
710
super(12);
811
}
912

1013
public static void main(String[] args) {
11-
new Day12().printParts();
14+
Day12 day = new Day12();
15+
day.printParts();
16+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 12, 1);
17+
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 12, 2);
1218
}
1319

1420
@Override
1521
public Object part1() {
16-
return "";
22+
Pattern pattern = Pattern.compile("-?\\d+");
23+
Matcher matcher = pattern.matcher(day());
24+
int sum = 0;
25+
while (matcher.find()) {
26+
sum += Integer.parseInt(matcher.group());
27+
}
28+
return sum;
1729
}
1830

1931
@Override
2032
public Object part2() {
21-
return "";
33+
return sumJson(day());
34+
}
35+
36+
private int sumJson(String json) {
37+
if (json.startsWith("[")) {
38+
return sumArray(json);
39+
} else if (json.startsWith("{")) {
40+
return sumObject(json);
41+
} else {
42+
try {
43+
return Integer.parseInt(json);
44+
} catch (NumberFormatException e) {
45+
return 0;
46+
}
47+
}
48+
}
49+
50+
private int sumArray(String json) {
51+
int sum = 0;
52+
int depth = 0;
53+
int start = 1;
54+
boolean inString = false;
55+
56+
for (int i = 1; i < json.length() - 1; i++) {
57+
char c = json.charAt(i);
58+
if (c == '"') {
59+
inString = !inString;
60+
}
61+
if (inString) continue;
62+
63+
if (c == '[' || c == '{') {
64+
depth++;
65+
} else if (c == ']' || c == '}') {
66+
depth--;
67+
} else if (c == ',' && depth == 0) {
68+
sum += sumJson(json.substring(start, i).trim());
69+
start = i + 1;
70+
}
71+
}
72+
if (start < json.length() - 1) {
73+
sum += sumJson(json.substring(start, json.length() - 1).trim());
74+
}
75+
return sum;
76+
}
77+
78+
private int sumObject(String json) {
79+
// Check if object contains "red" as a value
80+
Pattern redPattern = Pattern.compile(":\\s*\"red\"");
81+
Matcher redMatcher = redPattern.matcher(json);
82+
if (redMatcher.find()) {
83+
return 0;
84+
}
85+
86+
int sum = 0;
87+
int depth = 0;
88+
int start = 1;
89+
boolean inString = false;
90+
91+
for (int i = 1; i < json.length() - 1; i++) {
92+
char c = json.charAt(i);
93+
if (c == '"') {
94+
inString = !inString;
95+
}
96+
if (inString) continue;
97+
98+
if (c == '[' || c == '{') {
99+
depth++;
100+
} else if (c == ']' || c == '}') {
101+
depth--;
102+
} else if (c == ',' && depth == 0) {
103+
String part = json.substring(start, i).trim();
104+
int colonIndex = part.indexOf(':');
105+
if (colonIndex != -1) {
106+
sum += sumJson(part.substring(colonIndex + 1).trim());
107+
}
108+
start = i + 1;
109+
}
110+
}
111+
if (start < json.length() - 1) {
112+
String part = json.substring(start, json.length() - 1).trim();
113+
int colonIndex = part.indexOf(':');
114+
if (colonIndex != -1) {
115+
sum += sumJson(part.substring(colonIndex + 1).trim());
116+
}
117+
}
118+
return sum;
22119
}
23120
}

0 commit comments

Comments
 (0)