-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeDay7Part1.java
More file actions
138 lines (103 loc) · 3.37 KB
/
Copy pathadventofcodeDay7Part1.java
File metadata and controls
138 lines (103 loc) · 3.37 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File; // Import the File class
import java.io.IOException;
import java.util.List;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.Map;
public class directedGraph
{
Map<String, LinkedList<String>> adj;
public directedGraph() {
adj = new HashMap<String, LinkedList<String>>();
}
public void addNode(String node)
{
adj.putIfAbsent(node, new LinkedList<String>());
}
public void addNeighbor(String v1,String v2) {
this.addNode(v1);
adj.get(v1).add(v2);
// System.out.print("node ");
// System.out.print(v1);
// System.out.print(" ");
// System.out.println(v2);
}
public List<String> getNeighbors(String v) {
return adj.get(v);
}
public void BFS(String s)
{
// Mark all the vertices as not visited(By default
// set as false)
Map<String, Boolean> visited = new HashMap<String,Boolean>();
// Create a queue for BFS
LinkedList<String> queue = new LinkedList<String>();
// Mark the current node as visited and enqueue it
visited.put(s, true);
queue.add(s);
List<String> neighbors;
System.out.println(queue.size());
Integer count = 0;
while (queue.size() != 0)
{
System.out.print("queue: ");
System.out.println(queue);
s = queue.poll();
System.out.print("search node: ");
System.out.println(s);
neighbors = getNeighbors(s);
if (neighbors != null){
for(int i =0; i< neighbors.size(); i++){
System.out.print("neigh: ");
System.out.println(neighbors.get(i));
// System.out.println(neighbors.get(i));
if (!visited.containsKey(neighbors.get(i))){
visited.put(neighbors.get(i), false);
}
if (!visited.get(neighbors.get(i))){
visited.put(neighbors.get(i), true);
queue.add(neighbors.get(i));
count ++;
}
}
}
System.out.println();
}
System.out.println(count);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("input.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
String [] tokens;
String containerBag;
String [] includedBags;
String removeDot;
String removeSpace;
String newBag;
directedGraph g = new directedGraph();
while( (line = br.readLine()) != null )
{
tokens = line.split("contain");
containerBag = tokens[0].replace("bags", "bag");
containerBag = containerBag.replace(" ","");
g.addNode( tokens[0]);
removeDot = tokens[1].replace(".","");
includedBags = removeDot.split(",");
for(int i=0; i< includedBags.length; i++){
newBag = includedBags[i];
newBag = newBag.replace("bags", "bag");
newBag = newBag.replace(" ","");
newBag = newBag.split("", 2)[1];
g.addNode( newBag);
g.addNeighbor(newBag, containerBag);
}
}
g.BFS("shinygoldbag");
br.close();
}
}