Skip to content

Commit

Permalink
aoc 2017 day 12
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 15, 2024
1 parent 0462229 commit 4ea1bb5
Show file tree
Hide file tree
Showing 11 changed files with 135 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 (22/22)
# 2017 (24/24)

| # | Name | Solution | Test |
|---------:|---------------------------------------------------------------------|:------------------------------------:|:---------------------------------:|
Expand All @@ -432,6 +432,8 @@
| 20171002 | [Day 10: Knot Hash - Part 2][20171002] | ✅[💾][20171002solution] | ✅[💾][20171002tests] |
| 20171101 | [Day 11: Hex Ed][20171101] | ✅[💾][20171101solution] | ✅[💾][20171101tests] |
| 20171102 | [Day 11: Hex Ed - Part 2][20171102] | ✅[💾][20171102solution] | ✅[💾][20171102tests] |
| 20171201 | [Day 12: Digital Plumber][20171201] | ✅[💾][20171201solution] | ✅[💾][20171201tests] |
| 20171202 | [Day 12: Digital Plumber - Part 2][20171202] | ✅[💾][20171202solution] | ✅[💾][20171202tests] |

[20170101]: https://adventofcode.com/2017/day/1
[20170102]: https://adventofcode.com/2017/day/1#part2
Expand All @@ -455,6 +457,8 @@
[20171002]: https://adventofcode.com/2017/day/10#part2
[20171101]: https://adventofcode.com/2017/day/11
[20171102]: https://adventofcode.com/2017/day/11#part2
[20171201]: https://adventofcode.com/2017/day/12
[20171202]: https://adventofcode.com/2017/day/12#part2

[20170101solution]: src/main/java/org/ck/adventofcode/year2017/Day01.java
[20170102solution]: src/main/java/org/ck/adventofcode/year2017/Day01.java
Expand All @@ -478,6 +482,8 @@
[20171002solution]: src/main/java/org/ck/adventofcode/year2017/Day10.java
[20171101solution]: src/main/java/org/ck/adventofcode/year2017/Day11.java
[20171102solution]: src/main/java/org/ck/adventofcode/year2017/Day11.java
[20171201solution]: src/main/java/org/ck/adventofcode/year2017/Day12.java
[20171202solution]: src/main/java/org/ck/adventofcode/year2017/Day12.java

[20170101tests]: src/test/java/org/ck/adventofcode/year2017/Day01Test.java
[20170102tests]: src/test/java/org/ck/adventofcode/year2017/Day01Test.java
Expand All @@ -501,6 +507,8 @@
[20171002tests]: src/test/java/org/ck/adventofcode/year2017/Day10Test.java
[20171101tests]: src/test/java/org/ck/adventofcode/year2017/Day11Test.java
[20171102tests]: src/test/java/org/ck/adventofcode/year2017/Day11Test.java
[20171201tests]: src/test/java/org/ck/adventofcode/year2017/Day12Test.java
[20171202tests]: src/test/java/org/ck/adventofcode/year2017/Day12Test.java

# 2019 (29/30)

Expand Down
87 changes: 87 additions & 0 deletions adventofcode/src/main/java/org/ck/adventofcode/year2017/Day12.java
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;
}
}
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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
130

Large diffs are not rendered by default.

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,7 @@
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
189

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
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

0 comments on commit 4ea1bb5

Please sign in to comment.