-
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
11 changed files
with
135 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
87 changes: 87 additions & 0 deletions
87
adventofcode/src/main/java/org/ck/adventofcode/year2017/Day12.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,87 @@ | ||
package org.ck.adventofcode.year2017; | ||
|
||
import java.util.*; | ||
import java.util.function.ToIntFunction; | ||
import java.util.stream.Collectors; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20171201, | ||
name = "Day 12: Digital Plumber", | ||
url = "https://adventofcode.com/2017/day/12", | ||
category = "2017") | ||
@Solution( | ||
id = 20171202, | ||
name = "Day 12: Digital Plumber - Part 2", | ||
url = "https://adventofcode.com/2017/day/12#part2", | ||
category = "2017") | ||
public class Day12 extends AOCSolution { | ||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in, Day12::getReachableFromZero); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run(in, Day12::getGroupCount); | ||
} | ||
|
||
private void run(final Scanner in, final ToIntFunction<Map<Integer, Set<Integer>>> getResult) { | ||
final Map<Integer, Set<Integer>> programs = new HashMap<>(); | ||
|
||
while (in.hasNextLine()) { | ||
final String[] parts = in.nextLine().split(" <-> "); | ||
|
||
programs.put( | ||
Integer.parseInt(parts[0]), | ||
Arrays.stream(parts[1].split(", ")).map(Integer::parseInt).collect(Collectors.toSet())); | ||
} | ||
|
||
print(getResult.applyAsInt(programs)); | ||
} | ||
|
||
private static int getReachableFromZero(final Map<Integer, Set<Integer>> programs) { | ||
final Set<Integer> reachable = new HashSet<>(); | ||
final Queue<Integer> queue = new LinkedList<>(); | ||
queue.add(0); | ||
|
||
while (!queue.isEmpty()) { | ||
final int current = queue.poll(); | ||
if (reachable.contains(current)) { | ||
continue; | ||
} | ||
|
||
reachable.add(current); | ||
queue.addAll(programs.get(current)); | ||
} | ||
|
||
return reachable.size(); | ||
} | ||
|
||
private static int getGroupCount(Map<Integer, Set<Integer>> programs) { | ||
final Set<Integer> reached = new HashSet<>(); | ||
|
||
int groups = 0; | ||
for (final int program : programs.keySet()) { | ||
if (!reached.contains(program)) { | ||
++groups; | ||
|
||
final Queue<Integer> queue = new LinkedList<>(); | ||
queue.add(program); | ||
|
||
while (!queue.isEmpty()) { | ||
final int current = queue.poll(); | ||
if (reached.contains(current)) { | ||
continue; | ||
} | ||
|
||
reached.add(current); | ||
queue.addAll(programs.get(current)); | ||
} | ||
} | ||
} | ||
|
||
return groups; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
adventofcode/src/test/java/org/ck/adventofcode/year2017/Day12Test.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 Day12Test extends BaseAOCTest { | ||
@ParameterizedTest | ||
@ValueSource(strings = {"01a"}) | ||
void testPartOneExamples(final String name) throws Exception { | ||
runTest(new Day12()::partOne, "day12/%s".formatted(name)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"02a"}) | ||
void testPartTwoExamples(final String name) throws Exception { | ||
runTest(new Day12()::partTwo, "day12/%s".formatted(name)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day12/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 @@ | ||
130 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day12/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/day12/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 @@ | ||
6 |
7 changes: 7 additions & 0 deletions
7
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day12/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,7 @@ | ||
0 <-> 2 | ||
1 <-> 1 | ||
2 <-> 0, 3, 4 | ||
3 <-> 2, 4 | ||
4 <-> 2, 3, 6 | ||
5 <-> 6 | ||
6 <-> 4, 5 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day12/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 @@ | ||
189 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day12/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/day12/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 @@ | ||
2 |
7 changes: 7 additions & 0 deletions
7
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day12/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,7 @@ | ||
0 <-> 2 | ||
1 <-> 1 | ||
2 <-> 0, 3, 4 | ||
3 <-> 2, 4 | ||
4 <-> 2, 3, 6 | ||
5 <-> 6 | ||
6 <-> 4, 5 |