Skip to content

Commit

Permalink
aoc 2017 day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 11, 2024
1 parent c349296 commit d943f03
Show file tree
Hide file tree
Showing 39 changed files with 155 additions and 1 deletion.
10 changes: 9 additions & 1 deletion adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@
[20162402tests]: src/test/java/org/ck/adventofcode/year2016/Day24Test.java
[20162501tests]: src/test/java/org/ck/adventofcode/year2016/Day25Test.java

# 2017 (16/16)
# 2017 (18/18)

| # | Name | Solution | Test |
|---------:|---------------------------------------------------------------------|:------------------------------------:|:---------------------------------:|
Expand All @@ -426,6 +426,8 @@
| 20170702 | [Day 7: Recursive Circus - Part 2][20170702] | ✅[💾][20170702solution] | ✅[💾][20170702tests] |
| 20170801 | [Day 8: I Heard You Like Registers][20170801] | ✅[💾][20170801solution] | ✅[💾][20170801tests] |
| 20170802 | [Day 8: I Heard You Like Registers - Part 2][20170802] | ✅[💾][20170802solution] | ✅[💾][20170802tests] |
| 20170901 | [Day 9: Stream Processing][20170901] | ✅[💾][20170901solution] | ✅[💾][20170901tests] |
| 20170902 | [Day 9: Stream Processing - Part 2][20170902] | ✅[💾][20170902solution] | ✅[💾][20170902tests] |

[20170101]: https://adventofcode.com/2017/day/1
[20170102]: https://adventofcode.com/2017/day/1#part2
Expand All @@ -443,6 +445,8 @@
[20170702]: https://adventofcode.com/2017/day/7#part2
[20170801]: https://adventofcode.com/2017/day/8
[20170802]: https://adventofcode.com/2017/day/8#part2
[20170901]: https://adventofcode.com/2017/day/9
[20170902]: https://adventofcode.com/2017/day/9#part2

[20170101solution]: src/main/java/org/ck/adventofcode/year2017/Day01.java
[20170102solution]: src/main/java/org/ck/adventofcode/year2017/Day01.java
Expand All @@ -460,6 +464,8 @@
[20170702solution]: src/main/java/org/ck/adventofcode/year2017/Day07.java
[20170801solution]: src/main/java/org/ck/adventofcode/year2017/Day08.java
[20170802solution]: src/main/java/org/ck/adventofcode/year2017/Day08.java
[20170901solution]: src/main/java/org/ck/adventofcode/year2017/Day09.java
[20170902solution]: src/main/java/org/ck/adventofcode/year2017/Day09.java

[20170101tests]: src/test/java/org/ck/adventofcode/year2017/Day01Test.java
[20170102tests]: src/test/java/org/ck/adventofcode/year2017/Day01Test.java
Expand All @@ -477,6 +483,8 @@
[20170702tests]: src/test/java/org/ck/adventofcode/year2017/Day07Test.java
[20170801tests]: src/test/java/org/ck/adventofcode/year2017/Day08Test.java
[20170802tests]: src/test/java/org/ck/adventofcode/year2017/Day08Test.java
[20170901tests]: src/test/java/org/ck/adventofcode/year2017/Day09Test.java
[20170902tests]: src/test/java/org/ck/adventofcode/year2017/Day09Test.java

# 2019 (29/30)

Expand Down
91 changes: 91 additions & 0 deletions adventofcode/src/main/java/org/ck/adventofcode/year2017/Day09.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.ck.adventofcode.year2017;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.ToIntFunction;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20170901,
name = "Day 9: Stream Processing",
url = "https://adventofcode.com/2017/day/9",
category = "2017")
@Solution(
id = 20170902,
name = "Day 9: Stream Processing - Part 2",
url = "https://adventofcode.com/2017/day/9#part2",
category = "2017")
public class Day09 extends AOCSolution {
@Override
protected void runPartOne(final Scanner in) {
run(in, result -> count(result.result(), 1));
}

@Override
protected void runPartTwo(final Scanner in) {
run(in, ParseResult::garbageCount);
}

private void run(final Scanner in, final ToIntFunction<ParseResult> getResult) {
final String line = in.nextLine();

print(getResult.applyAsInt(parse(line, 1)));
}

private int count(final List<?> result, final int value) {
int count = value;

for (Object o : result) {
if (o instanceof List<?> list) {
count += count(list, value + 1);
}
}

return count;
}

private static ParseResult parse(final String line, final int start) {
final List<Object> result = new ArrayList<>();
int index = start;
int garbageCount = 0;

while (line.charAt(index) != '}') {
if (line.charAt(index) == '{') {
final ParseResult parseResult = parse(line, index + 1);

result.add(parseResult.result());
garbageCount += parseResult.garbageCount();
index = parseResult.newIndex() + 1;
} else if (line.charAt(index) == ',') {
++index;
} else if (line.charAt(index) == '<') {
final ParseResult parseResult = parseGarbage(line, index + 1);

garbageCount += parseResult.garbageCount();
index = parseResult.newIndex() + 1;
}
}

return new ParseResult(result, index, garbageCount);
}

private static ParseResult parseGarbage(final String line, final int start) {
int index = start;
int garbageCount = 0;

while (line.charAt(index) != '>') {
if (line.charAt(index) == '!') {
index += 2;
} else {
++garbageCount;
++index;
}
}

return new ParseResult(null, index, garbageCount);
}

private record ParseResult(List<Object> result, int newIndex, int garbageCount) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.ck.adventofcode.year2017;

import org.ck.adventofcode.util.BaseAOCTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class Day09Test extends BaseAOCTest {
@ParameterizedTest
@ValueSource(strings = {"01", "01a", "01b", "01c", "01d", "01e", "01f", "01g", "01h"})
void testOne(String name) throws Exception {
runTest(new Day09()::partOne, "day09/%s".formatted(name));
}

@ParameterizedTest
@ValueSource(strings = {"02", "02a", "02b", "02c", "02d", "02e", "02f", "02g", "02h"})
void testTwo(String name) throws Exception {
runTest(new Day09()::partTwo, "day09/%s".formatted(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12505

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{},{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{},{},{{}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{<a>,<a>,<a>,<a>}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<ab>},{<ab>},{<ab>},{<ab>}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<!!>},{<!!>},{<!!>},{<!!>}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<a!>},{<a!>},{<a!>},{<ab>}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6671

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{},{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{},{},{{}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{<a>,<a>,<a>,<a>}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<ab>},{<ab>},{<ab>},{<ab>}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<!!>},{<!!>},{<!!>},{<!!>}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{<a!>},{<a!>},{<a!>},{<ab>}}

0 comments on commit d943f03

Please sign in to comment.