-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay12.java
78 lines (65 loc) · 1.92 KB
/
Day12.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.sbaars.adventofcode.year17.days;
import com.sbaars.adventofcode.year17.Day2017;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day12 extends Day2017 {
public Day12() {
super(12);
}
public static void main(String[] args) {
new Day12().printParts();
}
private Map<Integer, Set<Integer>> buildGraph() {
Map<Integer, Set<Integer>> graph = new HashMap<>();
Pattern pattern = Pattern.compile("(\\d+) <-> (.+)");
// Build the graph
for (String line : dayStrings()) {
Matcher m = pattern.matcher(line);
if (m.find()) {
int program = Integer.parseInt(m.group(1));
Set<Integer> connections = new HashSet<>();
for (String conn : m.group(2).split(", ")) {
connections.add(Integer.parseInt(conn));
}
graph.put(program, connections);
}
}
return graph;
}
private Set<Integer> findGroup(Map<Integer, Set<Integer>> graph, int start, Set<Integer> visited) {
Queue<Integer> queue = new LinkedList<>();
Set<Integer> group = new HashSet<>();
queue.add(start);
group.add(start);
while (!queue.isEmpty()) {
int current = queue.poll();
for (int neighbor : graph.get(current)) {
if (!group.contains(neighbor)) {
group.add(neighbor);
queue.add(neighbor);
}
}
}
visited.addAll(group);
return group;
}
@Override
public Object part1() {
Map<Integer, Set<Integer>> graph = buildGraph();
return findGroup(graph, 0, new HashSet<>()).size();
}
@Override
public Object part2() {
Map<Integer, Set<Integer>> graph = buildGraph();
Set<Integer> visited = new HashSet<>();
int groups = 0;
for (int program : graph.keySet()) {
if (!visited.contains(program)) {
findGroup(graph, program, visited);
groups++;
}
}
return groups;
}
}