File tree 2 files changed +45
-0
lines changed
main/java/com/therifian/AoC2015
test/java/com/therifian/AoC2015
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -23,4 +23,40 @@ public static int part1(String input) {
23
23
24
24
return sumLiteral - sumMemory ;
25
25
}
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
+ }
26
62
}
Original file line number Diff line number Diff line change @@ -14,4 +14,13 @@ void test1() {
14
14
"\" \\ x27\" " ;
15
15
assertEquals (12 , Day08 .part1 (input ));
16
16
}
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
+ }
17
26
}
You can’t perform that action at this time.
0 commit comments