1
1
package com .sbaars .adventofcode .year15 .days ;
2
2
3
3
import com .sbaars .adventofcode .year15 .Day2015 ;
4
+ import java .util .stream .Stream ;
5
+ import com .sbaars .adventofcode .network .Submit ;
4
6
5
7
public class Day8 extends Day2015 {
6
8
@@ -9,16 +11,56 @@ public Day8() {
9
11
}
10
12
11
13
public static void main (String [] args ) {
12
- new Day8 ().printParts ();
14
+ Day8 day = new Day8 ();
15
+ day .printParts ();
16
+ Submit submit = new Submit ();
17
+ submit .submit (day .part1 (), 2015 , 8 , 1 );
18
+ submit .submit (day .part2 (), 2015 , 8 , 2 );
13
19
}
14
20
15
21
@ Override
16
22
public Object part1 () {
17
- return "" ;
23
+ return dayStream (). mapToInt ( s -> s . length () - countMemoryChars ( s )). sum () ;
18
24
}
19
25
20
26
@ Override
21
27
public Object part2 () {
22
- return "" ;
28
+ return dayStream ().mapToInt (s -> countEncodedChars (s ) - s .length ()).sum ();
29
+ }
30
+
31
+ private int countMemoryChars (String s ) {
32
+ // Remove quotes at start and end
33
+ String content = s .substring (1 , s .length () - 1 );
34
+ int count = 0 ;
35
+ for (int i = 0 ; i < content .length (); i ++) {
36
+ if (content .charAt (i ) == '\\' ) {
37
+ if (i + 1 < content .length ()) {
38
+ char next = content .charAt (i + 1 );
39
+ if (next == '\\' || next == '"' ) {
40
+ i ++; // Skip the escaped character
41
+ } else if (next == 'x' && i + 3 < content .length ()) {
42
+ i += 3 ; // Skip the hex sequence
43
+ }
44
+ }
45
+ }
46
+ count ++;
47
+ }
48
+ return count ;
49
+ }
50
+
51
+ private int countEncodedChars (String s ) {
52
+ int count = 2 ; // Opening and closing quotes
53
+ for (char c : s .toCharArray ()) {
54
+ if (c == '"' || c == '\\' ) {
55
+ count += 2 ; // Each quote or backslash needs to be escaped
56
+ } else {
57
+ count ++; // Regular character
58
+ }
59
+ }
60
+ return count ;
61
+ }
62
+
63
+ protected Stream <String > dayStream () {
64
+ return day ().lines ();
23
65
}
24
66
}
0 commit comments