-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
adventofcode/src/main/java/org/ck/adventofcode/year2017/Day09.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
19 changes: 19 additions & 0 deletions
19
adventofcode/src/test/java/org/ck/adventofcode/year2017/Day09Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12505 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01.txt
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01a.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01a.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01b.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01b.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{{}}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01c.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01c.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{},{}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01d.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
16 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01d.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{{},{},{{}}}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01e.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01e.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{<a>,<a>,<a>,<a>} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01f.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
9 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01f.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<ab>},{<ab>},{<ab>},{<ab>}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01g.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
9 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01g.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<!!>},{<!!>},{<!!>},{<!!>}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01h.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/01h.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<a!>},{<a!>},{<a!>},{<ab>}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6671 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02.txt
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02a.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02a.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02b.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02b.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{{}}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02c.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02c.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{},{}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02d.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02d.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{{},{},{{}}}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02e.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02e.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{<a>,<a>,<a>,<a>} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02f.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
8 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02f.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<ab>},{<ab>},{<ab>},{<ab>}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02g.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02g.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<!!>},{<!!>},{<!!>},{<!!>}} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02h.result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
17 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day09/02h.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{<a!>},{<a!>},{<a!>},{<ab>}} |