Skip to content

Commit 71a5809

Browse files
committed
feat: 08 2015 part 2
1 parent 3976a5d commit 71a5809

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Diff for: AoC2015/src/main/java/com/therifian/AoC2015/Day08.java

+36
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,40 @@ public static int part1(String input) {
2323

2424
return sumLiteral - sumMemory;
2525
}
26+
27+
public static int part2(String input) {
28+
int sumLiteral = 0 , sumEncoded = 0;
29+
String[] lines = input.split("\n");
30+
StringBuilder encoded = new StringBuilder();
31+
32+
for (String line: lines) {
33+
sumLiteral += line.length();
34+
StringBuilder str = new StringBuilder();
35+
int i = 0;
36+
str.append('"');
37+
while (i < line.length()) {
38+
char current = line.charAt(i);
39+
switch (current) {
40+
case '"':
41+
str.append('\\').append('"');
42+
break;
43+
case '\\':
44+
str.append("\\").append("\\");
45+
break;
46+
default:
47+
str.append(current);
48+
break;
49+
}
50+
i++;
51+
}
52+
str.append('"');
53+
sumEncoded += str.length();
54+
System.out.println(str);
55+
encoded.append(str).append("\n");
56+
}
57+
System.out.println(encoded);
58+
System.out.println(sumEncoded);
59+
System.out.println(sumLiteral);
60+
return sumEncoded- sumLiteral;
61+
}
2662
}

Diff for: AoC2015/src/test/java/com/therifian/AoC2015/Day08Test.java

+9
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ void test1() {
1414
"\"\\x27\"";
1515
assertEquals(12, Day08.part1(input));
1616
}
17+
18+
@Test
19+
void test2() {
20+
String input = "\"\"\n" +
21+
"\"abc\"\n" +
22+
"\"aaa\\\"aaa\"\n" +
23+
"\"\\x27\"";
24+
assertEquals(19, Day08.part2(input));
25+
}
1726
}

0 commit comments

Comments
 (0)