Initial commit

This commit is contained in:
Tobias Eidelpes 2022-09-20 20:51:23 +02:00
commit d64b8c1359
5 changed files with 667 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

48
Cargo.lock generated Normal file
View File

@ -0,0 +1,48 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "indexmap"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "petgraph"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]]
name = "scotland-yard"
version = "0.1.0"
dependencies = [
"petgraph",
]

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "scotland-yard"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
petgraph = "0.6.2"

141
src/main.rs Normal file
View File

@ -0,0 +1,141 @@
use petgraph::stable_graph::{NodeIndex, StableGraph};
use petgraph::Undirected;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
let filename = String::from("src/nodes.txt");
let taxi_graph = build_graph_taxi(&filename);
let bus_graph = build_graph_bus(&filename);
let underground_graph = build_graph_underground(&filename);
let boat_graph = build_graph_boat(&filename);
loop {
let (current_nodes, transport_type) = parse_input();
let mut neighbors = Vec::<NodeIndex<usize>>::new();
for node in current_nodes {
match Some(&*transport_type.to_string()) {
Some("Taxi") => neighbors.extend(taxi_graph.neighbors(NodeIndex::new(node))),
Some("Bus") => neighbors.extend(bus_graph.neighbors(NodeIndex::new(node))),
Some("Underground") => {
neighbors.extend(underground_graph.neighbors(NodeIndex::new(node)))
}
Some("Boat") => neighbors.extend(boat_graph.neighbors(NodeIndex::new(node))),
_ => {}
}
}
let neighbors: Vec<usize> = neighbors.iter().map(|node| node.index()).collect();
for neighbor in neighbors {
print!("{} ", neighbor);
}
println!("");
}
}
fn parse_input() -> (Vec<usize>, String) {
println!("On which field is Mr. X currently?");
let mut target = String::new();
io::stdin()
.read_line(&mut target)
.expect("Failed to read from stdin");
let target: Vec<&str> = target.split_whitespace().collect();
let mut numbers = Vec::<usize>::new();
for number in target {
let number = number.parse().expect("Could not parse integer");
numbers.push(number);
}
let mut transport_type = String::new();
io::stdin()
.read_line(&mut transport_type)
.expect("Failed to read from stdin");
let transport_type = transport_type.trim();
(numbers, String::from(transport_type))
}
fn build_graph_taxi(filename: &String) -> StableGraph<(), (), Undirected, usize> {
let mut graph = StableGraph::<(), (), Undirected, usize>::with_capacity(199, 345);
if let Ok(lines) = read_lines(filename) {
for line in lines {
if let Ok(edge) = line {
if edge.contains("Taxi") {
let edge: Vec<&str> = edge.split_whitespace().collect();
graph.extend_with_edges(&[(
edge[0].parse::<usize>().unwrap(),
edge[1].parse::<usize>().unwrap(),
)]);
}
}
}
}
graph
}
fn build_graph_bus(filename: &String) -> StableGraph<(), (), Undirected, usize> {
let mut graph = StableGraph::<(), (), Undirected, usize>::with_capacity(62, 100);
if let Ok(lines) = read_lines(filename) {
for line in lines {
if let Ok(edge) = line {
if edge.contains("Bus") {
let edge: Vec<&str> = edge.split_whitespace().collect();
graph.extend_with_edges(&[(
edge[0].parse::<usize>().unwrap(),
edge[1].parse::<usize>().unwrap(),
)]);
}
}
}
}
graph
}
fn build_graph_underground(filename: &String) -> StableGraph<(), (), Undirected, usize> {
let mut graph = StableGraph::<(), (), Undirected, usize>::with_capacity(14, 20);
if let Ok(lines) = read_lines(filename) {
for line in lines {
if let Ok(edge) = line {
if edge.contains("Underground") {
let edge: Vec<&str> = edge.split_whitespace().collect();
graph.extend_with_edges(&[(
edge[0].parse::<usize>().unwrap(),
edge[1].parse::<usize>().unwrap(),
)]);
}
}
}
}
graph
}
fn build_graph_boat(filename: &String) -> StableGraph<(), (), Undirected, usize> {
let mut graph = StableGraph::<(), (), Undirected, usize>::with_capacity(4, 3);
if let Ok(lines) = read_lines(filename) {
for line in lines {
if let Ok(edge) = line {
if edge.contains("Boat") {
let edge: Vec<&str> = edge.split_whitespace().collect();
graph.extend_with_edges(&[(
edge[0].parse::<usize>().unwrap(),
edge[1].parse::<usize>().unwrap(),
)]);
}
}
}
}
graph
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

468
src/nodes.txt Normal file
View File

@ -0,0 +1,468 @@
1 8 Taxi
1 9 Taxi
1 46 Bus
1 46 Underground
1 58 Bus
2 10 Taxi
2 20 Taxi
3 4 Taxi
3 11 Taxi
3 12 Taxi
3 22 Bus
3 23 Bus
4 13 Taxi
5 15 Taxi
5 16 Taxi
6 7 Taxi
6 29 Taxi
7 17 Taxi
7 42 Bus
8 18 Taxi
8 19 Taxi
9 19 Taxi
9 20 Taxi
10 11 Taxi
10 21 Taxi
10 34 Taxi
11 22 Taxi
12 23 Taxi
13 14 Bus
13 23 Taxi
13 23 Bus
13 24 Taxi
13 46 Underground
13 52 Bus
13 67 Underground
13 89 Underground
14 15 Taxi
14 15 Bus
14 25 Taxi
15 16 Taxi
15 26 Taxi
15 28 Taxi
15 29 Bus
15 41 Bus
16 28 Taxi
16 29 Taxi
17 29 Taxi
17 30 Taxi
18 31 Taxi
18 43 Taxi
19 32 Taxi
20 33 Taxi
21 33 Taxi
22 23 Taxi
22 23 Bus
22 34 Taxi
22 34 Bus
22 35 Taxi
22 65 Bus
23 37 Taxi
23 67 Bus
24 37 Taxi
24 38 Taxi
25 38 Taxi
25 39 Taxi
26 27 Taxi
26 39 Taxi
27 28 Taxi
27 40 Taxi
28 41 Taxi
29 41 Taxi
29 41 Bus
29 42 Taxi
29 42 Bus
29 55 Bus
30 42 Taxi
31 43 Taxi
31 44 Taxi
32 33 Taxi
32 44 Taxi
32 45 Taxi
33 46 Taxi
34 46 Bus
34 47 Taxi
34 48 Taxi
34 63 Bus
35 36 Taxi
35 48 Taxi
35 65 Taxi
36 37 Taxi
36 49 Taxi
37 50 Taxi
38 50 Taxi
38 51 Taxi
39 51 Taxi
39 52 Taxi
40 41 Taxi
40 52 Taxi
40 53 Taxi
41 52 Bus
41 54 Taxi
41 87 Bus
42 56 Taxi
42 72 Taxi
42 72 Bus
43 57 Taxi
44 58 Taxi
45 46 Taxi
45 58 Taxi
45 59 Taxi
45 60 Taxi
46 47 Taxi
46 58 Bus
46 61 Taxi
46 74 Underground
46 78 Bus
46 79 Underground
47 62 Taxi
48 62 Taxi
48 63 Taxi
49 50 Taxi
49 66 Taxi
51 52 Taxi
51 67 Taxi
51 68 Taxi
52 67 Bus
52 69 Taxi
52 86 Bus
53 54 Taxi
53 69 Taxi
54 55 Taxi
54 70 Taxi
55 71 Taxi
55 89 Bus
56 91 Taxi
57 58 Taxi
57 73 Taxi
58 59 Taxi
58 74 Taxi
58 74 Bus
58 75 Taxi
58 77 Bus
59 75 Taxi
59 76 Taxi
60 61 Taxi
60 76 Taxi
61 62 Taxi
61 76 Taxi
61 78 Taxi
62 79 Taxi
63 64 Taxi
63 65 Bus
63 79 Taxi
63 79 Bus
63 80 Taxi
63 100 Bus
64 65 Taxi
64 81 Taxi
65 66 Taxi
65 67 Bus
65 82 Taxi
65 82 Bus
66 67 Taxi
66 82 Taxi
67 68 Taxi
67 79 Underground
67 82 Bus
67 84 Taxi
67 89 Underground
67 102 Bus
67 111 Underground
68 69 Taxi
68 85 Taxi
69 86 Taxi
70 71 Taxi
70 87 Taxi
71 72 Taxi
71 89 Taxi
72 90 Taxi
72 91 Taxi
72 105 Bus
72 107 Bus
73 74 Taxi
73 92 Taxi
74 75 Taxi
74 92 Taxi
74 94 Bus
75 94 Taxi
76 77 Taxi
77 78 Taxi
77 78 Bus
77 94 Bus
77 95 Taxi
77 96 Taxi
77 124 Bus
78 79 Taxi
78 79 Bus
78 97 Taxi
79 93 Underground
79 98 Taxi
79 111 Underground
80 99 Taxi
80 100 Taxi
81 82 Taxi
81 100 Taxi
82 100 Bus
82 101 Taxi
82 140 Bus
83 101 Taxi
83 102 Taxi
84 85 Taxi
85 103 Taxi
86 87 Bus
86 102 Bus
86 103 Taxi
86 104 Taxi
86 116 Bus
87 88 Taxi
87 105 Bus
88 89 Taxi
88 117 Taxi
89 105 Taxi
89 105 Bus
89 128 Underground
89 140 Underground
90 91 Taxi
90 105 Taxi
91 105 Taxi
91 107 Taxi
92 93 Taxi
93 94 Taxi
93 94 Bus
94 95 Taxi
95 122 Taxi
96 97 Taxi
96 109 Taxi
97 98 Taxi
97 109 Taxi
98 99 Taxi
98 110 Taxi
99 110 Taxi
99 112 Taxi
100 101 Taxi
100 111 Bus
100 112 Taxi
100 113 Taxi
101 114 Taxi
102 103 Taxi
102 115 Taxi
102 127 Bus
104 116 Taxi
105 106 Taxi
105 107 Bus
105 108 Taxi
105 108 Bus
106 107 Taxi
107 119 Taxi
107 161 Bus
108 116 Bus
108 117 Taxi
108 119 Taxi
108 135 Bus
109 110 Taxi
109 124 Taxi
110 111 Taxi
111 112 Taxi
111 124 Taxi
111 124 Bus
111 153 Underground
111 163 Underground
112 125 Taxi
113 114 Taxi
113 125 Taxi
114 115 Taxi
114 126 Taxi
114 131 Taxi
114 132 Taxi
115 126 Taxi
115 127 Taxi
116 117 Taxi
116 118 Taxi
116 127 Taxi
116 127 Bus
116 142 Bus
117 129 Taxi
118 129 Taxi
118 134 Taxi
118 142 Taxi
119 136 Taxi
120 121 Taxi
120 144 Taxi
121 122 Taxi
121 145 Taxi
122 123 Taxi
122 123 Bus
122 144 Bus
122 146 Taxi
123 124 Taxi
123 124 Bus
123 137 Taxi
123 144 Bus
123 148 Taxi
123 149 Taxi
123 165 Bus
124 130 Taxi
124 138 Taxi
124 153 Bus
125 131 Taxi
126 127 Taxi
126 140 Taxi
127 133 Taxi
127 133 Bus
127 134 Taxi
128 185 Underground
128 187 Bus
128 188 Taxi
128 199 Bus
129 135 Taxi
129 142 Taxi
129 143 Taxi
130 131 Taxi
130 139 Taxi
132 140 Taxi
133 140 Taxi
133 140 Bus
133 141 Taxi
133 157 Bus
134 141 Taxi
134 142 Taxi
135 128 Bus
135 136 Taxi
135 143 Taxi
135 161 Taxi
135 161 Bus
136 162 Taxi
137 147 Taxi
138 150 Taxi
138 152 Taxi
139 140 Taxi
139 153 Taxi
139 154 Taxi
140 128 Underground
140 153 Underground
140 154 Taxi
140 154 Bus
140 156 Taxi
140 156 Bus
141 142 Taxi
141 158 Taxi
142 128 Taxi
142 128 Bus
142 143 Taxi
142 157 Bus
142 158 Taxi
143 128 Taxi
143 160 Taxi
144 145 Taxi
144 163 Bus
144 177 Taxi
145 146 Taxi
146 147 Taxi
146 163 Taxi
147 164 Taxi
148 149 Taxi
148 164 Taxi
149 150 Taxi
149 165 Taxi
150 151 Taxi
151 152 Taxi
151 165 Taxi
151 166 Taxi
152 153 Taxi
153 154 Taxi
153 154 Bus
153 163 Underground
153 166 Taxi
153 167 Taxi
153 180 Bus
153 184 Bus
153 185 Underground
154 155 Taxi
154 156 Bus
155 156 Taxi
155 167 Taxi
155 168 Taxi
156 157 Taxi
156 157 Bus
156 169 Taxi
156 184 Bus
157 158 Taxi
157 170 Taxi
157 185 Bus
158 159 Taxi
159 170 Taxi
159 172 Taxi
159 186 Taxi
159 198 Taxi
160 128 Taxi
160 161 Taxi
160 173 Taxi
161 128 Bus
161 174 Taxi
161 199 Bus
162 175 Taxi
163 176 Bus
163 177 Taxi
163 191 Bus
164 178 Taxi
164 179 Taxi
165 179 Taxi
165 180 Taxi
165 180 Bus
165 191 Bus
166 181 Taxi
166 183 Taxi
167 168 Taxi
167 183 Taxi
168 184 Taxi
169 184 Taxi
170 185 Taxi
171 173 Taxi
171 175 Taxi
171 199 Taxi
172 128 Taxi
172 187 Taxi
173 174 Taxi
173 188 Taxi
174 175 Taxi
176 177 Taxi
176 189 Taxi
176 190 Bus
178 189 Taxi
178 191 Taxi
179 191 Taxi
180 181 Taxi
180 184 Bus
180 190 Bus
180 193 Taxi
181 182 Taxi
181 193 Taxi
182 183 Taxi
182 195 Taxi
183 196 Taxi
184 185 Taxi
184 185 Bus
184 196 Taxi
184 197 Taxi
185 186 Taxi
185 187 Bus
186 198 Taxi
187 188 Taxi
187 198 Taxi
187 199 Bus
188 199 Taxi
189 190 Taxi
190 191 Taxi
190 191 Bus
190 192 Taxi
191 192 Taxi
192 194 Taxi
193 194 Taxi
194 195 Taxi
195 197 Taxi
196 197 Taxi
198 199 Taxi
194 157 Boat
157 115 Boat
115 108 Boat