Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ed14b0d

Browse files
Solved Day 8 (2015) - Matchsticks
1 parent 46f230e commit ed14b0d

File tree

1 file changed

+45
-3
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+45
-3
lines changed
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.stream.Stream;
5+
import com.sbaars.adventofcode.network.Submit;
46

57
public class Day8 extends Day2015 {
68

@@ -9,16 +11,56 @@ public Day8() {
911
}
1012

1113
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);
1319
}
1420

1521
@Override
1622
public Object part1() {
17-
return "";
23+
return dayStream().mapToInt(s -> s.length() - countMemoryChars(s)).sum();
1824
}
1925

2026
@Override
2127
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();
2365
}
2466
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /