Skip to content

Commit 8d35ddd

Browse files
authored
feat: day 12
* fix: day 8 and 11 * feat: part 1 * feat: day 12 with jackson
1 parent d6baef7 commit 8d35ddd

5 files changed

Lines changed: 131 additions & 9 deletions

File tree

AoC2015/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<version>5.9.3</version>
2222
<scope>test</scope>
2323
</dependency>
24+
<dependency>
25+
<groupId>com.fasterxml.jackson.core</groupId>
26+
<artifactId>jackson-databind</artifactId>
27+
<version>2.19.2</version>
28+
</dependency>
2429
</dependencies>
2530

2631
</project>

AoC2015/src/main/java/com/therifian/AoC2015/Day08.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ public static int part2(String input) {
5151
}
5252
str.append('"');
5353
sumEncoded += str.length();
54-
System.out.println(str);
5554
encoded.append(str).append("\n");
5655
}
57-
System.out.println(encoded);
58-
System.out.println(sumEncoded);
59-
System.out.println(sumLiteral);
6056
return sumEncoded- sumLiteral;
6157
}
6258
}

AoC2015/src/main/java/com/therifian/AoC2015/Day11.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,4 @@ private static boolean isPasswordValid(char[] password) {
5454
return consecutive && pairs >= 2;
5555
}
5656

57-
58-
public static void main(String[] args) {
59-
System.out.println(part1("hepxxyzz"));
60-
}
61-
6257
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.therifian.AoC2015;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public class Day12 {
8+
9+
public static int part1(String input) {
10+
ObjectMapper mapper = new ObjectMapper();
11+
JsonNode root;
12+
try {
13+
root = mapper.readTree(input);
14+
return sum(root, false);
15+
} catch (JsonProcessingException e) {
16+
e.printStackTrace();
17+
}
18+
return 0;
19+
}
20+
21+
public static int part2(String input) {
22+
ObjectMapper mapper = new ObjectMapper();
23+
JsonNode root;
24+
try {
25+
root = mapper.readTree(input);
26+
return sum(root, true);
27+
} catch (JsonProcessingException e) {
28+
e.printStackTrace();
29+
}
30+
return 0;
31+
}
32+
private static int sum(JsonNode node, boolean skip) {
33+
if (node.isNumber()) return node.asInt();
34+
if (node.isArray()) {
35+
int sum = 0;
36+
for (JsonNode child : node) sum += Day12.sum(child, skip);
37+
return sum;
38+
}
39+
if (node.isObject()) {
40+
int sum = 0;
41+
if (skip) {
42+
for (JsonNode child : node) {
43+
if (child.isTextual() && child.asText().equals("red")) return 0;
44+
}
45+
}
46+
for (JsonNode child : node) sum += Day12.sum(child, skip);
47+
return sum;
48+
}
49+
return 0;
50+
}
51+
52+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.therifian.AoC2015;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class Day12Test {
8+
9+
@Test
10+
void test1() {
11+
assertEquals(6, Day12.part1("[1,2,3]"));
12+
}
13+
14+
@Test
15+
void test2() {
16+
assertEquals(6, Day12.part1("{\"a\":2,\"b\":4}"));
17+
}
18+
19+
@Test
20+
void test3() {
21+
assertEquals(3, Day12.part1("{\"a\":{\"b\":4},\"c\":-1}"));
22+
}
23+
24+
@Test
25+
void test4() {
26+
assertEquals(0, Day12.part1("{\"a\":[-1,1]}"));
27+
}
28+
29+
@Test
30+
void test5() {
31+
assertEquals(0, Day12.part1("[-1,{\"a\":1}]"));
32+
}
33+
34+
@Test
35+
void test6() {
36+
assertEquals(0, Day12.part1("[]"));
37+
38+
}
39+
40+
@Test
41+
void test7() {
42+
assertEquals(0, Day12.part1("{}"));
43+
}
44+
45+
@Test
46+
void test8() {
47+
assertEquals(3, Day12.part1("[[[3]]]"));
48+
}
49+
50+
@Test
51+
void test9() {
52+
assertEquals(6, Day12.part2("[1,2,3]"));
53+
}
54+
55+
@Test
56+
void test10() {
57+
assertEquals(4, Day12.part2("[1,{\"c\":\"red\",\"b\":2},3]"));
58+
}
59+
60+
@Test
61+
void test11() {
62+
assertEquals(0, Day12.part2("{\"d\":\"red\",\"e\":[1,2,3,4],\"f\":5}"));
63+
}
64+
65+
@Test
66+
void test12() {
67+
assertEquals(6, Day12.part2("[1,\"red\",5]"));
68+
}
69+
70+
@Test
71+
void test13() {
72+
assertEquals(0, Day12.part2("{[1,{\"c\":\"red\",\"b\":2},3]}"));
73+
}
74+
}

0 commit comments

Comments
 (0)