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 eaa8cf1

Browse files
Solved Day 10 (2015) - Elves Look, Elves Say
1 parent eb17d1e commit eaa8cf1

File tree

1 file changed

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

1 file changed

+34
-3
lines changed

‎src/main/java/com/sbaars/adventofcode/year15/days/Day10.java‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,52 @@
33
import com.sbaars.adventofcode.year15.Day2015;
44

55
public class Day10 extends Day2015 {
6+
67
public Day10() {
78
super(10);
89
}
910

1011
public static void main(String[] args) {
11-
new Day10().printParts();
12+
Day10 day = new Day10();
13+
day.printParts();
14+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 10, 1);
15+
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 10, 2);
1216
}
1317

1418
@Override
1519
public Object part1() {
16-
return "";
20+
return lookAndSay(day().trim(), 40).length();
1721
}
1822

1923
@Override
2024
public Object part2() {
21-
return "";
25+
return lookAndSay(day().trim(), 50).length();
26+
}
27+
28+
private String lookAndSay(String input, int times) {
29+
String result = input;
30+
for (int i = 0; i < times; i++) {
31+
result = lookAndSayOnce(result);
32+
}
33+
return result;
34+
}
35+
36+
private String lookAndSayOnce(String input) {
37+
StringBuilder result = new StringBuilder();
38+
int count = 1;
39+
char current = input.charAt(0);
40+
41+
for (int i = 1; i < input.length(); i++) {
42+
if (input.charAt(i) == current) {
43+
count++;
44+
} else {
45+
result.append(count).append(current);
46+
current = input.charAt(i);
47+
count = 1;
48+
}
49+
}
50+
result.append(count).append(current);
51+
52+
return result.toString();
2253
}
2354
}

0 commit comments

Comments
(0)

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