|
1 | 1 | package com.sbaars.adventofcode.year15.days;
|
2 | 2 |
|
3 | 3 | import com.sbaars.adventofcode.year15.Day2015;
|
| 4 | +import java.util.regex.Matcher; |
| 5 | +import java.util.regex.Pattern; |
4 | 6 |
|
5 | 7 | public class Day12 extends Day2015 {
|
| 8 | + |
6 | 9 | public Day12() {
|
7 | 10 | super(12);
|
8 | 11 | }
|
9 | 12 |
|
10 | 13 | 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); |
12 | 18 | }
|
13 | 19 |
|
14 | 20 | @Override
|
15 | 21 | 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; |
17 | 29 | }
|
18 | 30 |
|
19 | 31 | @Override
|
20 | 32 | 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; |
22 | 119 | }
|
23 | 120 | }
|
0 commit comments