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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::{fmt, collections::{BTreeMap, BTreeSet}};
#[derive(Debug, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)]
pub struct StreetName(pub String);
impl fmt::Display for StreetName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct InputStreetValue {
pub street_name: StreetName,
pub position: GridPosition,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GridPosition {
pub column: String,
pub row: usize,
}
impl fmt::Display for GridPosition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.column, self.row)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeduplicatedRoads {
pub roads: BTreeMap<StreetName, BTreeSet<GridPosition>>,
}
impl DeduplicatedRoads {
pub fn from_streets(streets: &[InputStreetValue]) -> Self {
let mut deduplicated_names = BTreeMap::new();
for input_street in streets {
deduplicated_names
.entry(input_street.street_name.clone())
.or_insert_with(|| BTreeSet::new())
.insert(input_street.position.clone());
}
Self { roads: deduplicated_names }
}
pub fn process(&self) -> (ProcessedRoadNames, UnprocessedRoadNames) {
let mut processed = BTreeMap::new();
let mut unprocessed = BTreeMap::new();
for (road_name, positions) in &self.roads {
let positions_vec = positions.into_iter().cloned().collect::<Vec<GridPosition>>();
match positions_vec.len() {
0 => { },
1 => { processed.insert(road_name.clone(), FinalizedGridPositon::SingleRect(positions_vec[0].clone())); }
2 => { processed.insert(road_name.clone(), FinalizedGridPositon::TwoRect(positions_vec[0].clone(), positions_vec[1].clone())); }
_ => { unprocessed.insert(road_name.clone(), positions_vec); }
}
}
(ProcessedRoadNames {
processed: processed.into_iter().map(|(k, v)| ProcessedRoad { name: k, position: v }).collect(),
},
UnprocessedRoadNames {
unprocessed: unprocessed.into_iter().map(|(k, v)| UnprocessedRoad { name: k, positions: v }).collect(),
})
}
}
#[test]
fn test_deduplicate_streets() {
let input = [
InputStreetValue {
street_name: StreetName(String::from("Valley View Road")),
position: GridPosition {
column: String::from("A"),
row: 4,
}
},
InputStreetValue {
street_name: StreetName(String::from("Valley View Road")),
position: GridPosition {
column: String::from("A"),
row: 5,
}
},
InputStreetValue {
street_name: StreetName(String::from("Valley View Road")),
position: GridPosition {
column: String::from("B"),
row: 6,
}
},
];
let mut output_expected = BTreeMap::new();
let mut valley_view_road_expected = BTreeSet::new();
valley_view_road_expected.insert(GridPosition { column: String::from("A"), row: 4 });
valley_view_road_expected.insert(GridPosition { column: String::from("A"), row: 5 });
valley_view_road_expected.insert(GridPosition { column: String::from("B"), row: 6 });
output_expected.insert(StreetName(String::from("Valley View Road")), valley_view_road_expected);
assert_eq!(DeduplicatedRoads::from_streets(&input), DeduplicatedRoads { roads: output_expected });
}
#[test]
fn test_format_street() {
let street_grid_1 = GridPosition { column: String::from("A"), row: 9 };
let street_grid_2 = GridPosition { column: String::from("I"), row: 5 };
let road_pos_1 = FinalizedGridPositon::TwoRect(street_grid_1.clone(), street_grid_2);
assert_eq!(format!("{}", street_grid_1), String::from("A9"));
assert_eq!(format!("{}", road_pos_1), String::from("A9-I5"));
}
pub enum FinalizedGridPositon {
SingleRect(GridPosition),
TwoRect(GridPosition, GridPosition),
}
impl fmt::Display for FinalizedGridPositon {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::FinalizedGridPositon::*;
match self {
SingleRect(single) => write!(f, "{}", single),
TwoRect(a, b) => write!(f, "{}-{}", a, b),
}
}
}
pub struct ProcessedRoad {
pub name: StreetName,
pub position: FinalizedGridPositon,
}
impl fmt::Display for ProcessedRoad {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}\t{}", self.name, self.position)
}
}
pub struct UnprocessedRoad {
pub name: StreetName,
pub positions: Vec<GridPosition>,
}
impl fmt::Display for UnprocessedRoad {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let unprocessed_string = self.positions.iter().map(|pos| format!("{}", pos)).collect::<Vec<String>>().join("\t");
write!(f, "{}\t{}", self.name, unprocessed_string)
}
}
pub struct ProcessedRoadNames {
pub processed: Vec<ProcessedRoad>,
}
impl ProcessedRoadNames {
pub fn to_csv(&self, delimiter: &str) -> String {
self.processed.iter().map(|processed_road|
format!("{}{}{}", processed_road.name, delimiter, processed_road.position))
.collect::<Vec<String>>()
.join("\r\n")
}
}
pub struct UnprocessedRoadNames {
pub unprocessed: Vec<UnprocessedRoad>,
}
impl UnprocessedRoadNames {
pub fn to_csv(&self, delimiter: &str) -> String {
self.unprocessed.iter().map(|unprocessed_road| {
let unprocessed_string = unprocessed_road.positions
.iter()
.map(|pos| format!("{}", pos))
.collect::<Vec<String>>()
.join(delimiter);
format!("{}{}{}", unprocessed_road.name, delimiter, unprocessed_string)
})
.collect::<Vec<String>>()
.join("\r\n")
}
}