-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstellations.rs
141 lines (117 loc) · 2.81 KB
/
constellations.rs
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
use std::io;
use std::io::Write;
use std::process;
use std::vec;
const MAX_DISTANCE: i32 = 3;
#[derive(Copy, Clone)]
struct Coords {
x: i32,
y: i32,
z: i32,
w: i32,
}
impl Coords {
fn distance_to(&self, other: &Self) -> i32 {
let xd = (self.x - other.x).abs();
let yd = (self.y - other.y).abs();
let zd = (self.z - other.z).abs();
let wd = (self.w - other.w).abs();
return xd + yd + zd + wd;
}
fn from_string(string: &str) -> Self {
let mut values: [i32; 4] = [0; 4];
let mut iter = string.trim().split(',');
for v in 0..4 {
let next = iter.next();
if next.is_none() {
eprintln!("Expected {} values in line, but only got {}", 4, v);
process::exit(1);
}
let parse = next.unwrap().parse();
if parse.is_err() {
eprintln!("Failed to parse integer value: {}", parse.unwrap_err());
process::exit(1);
}
values[v] = parse.unwrap();
}
return Coords{ x: values[0], y: values[1], z: values[2], w: values[3] };
}
}
struct Constellation {
data: Vec<Coords>,
}
impl Constellation {
fn add(&mut self, new: Coords) {
self.data.push(new);
}
fn can_add(&self, candidate: &Coords) -> bool {
match self.data.iter().find(|elem| elem.distance_to(candidate) <= MAX_DISTANCE) {
Some(_) => return true,
None => return false,
}
}
fn can_join(&self, other: &Constellation) -> bool {
for our in self.data.iter() {
for theirs in other.data.iter() {
if our.distance_to(&theirs) <= MAX_DISTANCE {
return true;
}
}
}
return false;
}
fn join(&mut self, other: &Constellation) {
self.data.reserve(other.data.len());
self.data.extend(other.data.iter());
}
fn len(&self) -> usize {
return self.data.len();
}
fn new() -> Self {
let vector: Vec<Coords> = Vec::new();
return Constellation{ data: vector };
}
fn new_from_coords(origin: Coords) -> Self {
let mut vector: Vec<Coords> = Vec::new();
vector.push(origin);
return Constellation{ data: vector };
}
}
fn read_stdin() -> Vec<Constellation> {
let mut result: Vec<Constellation> = Vec::new();
let mut line = String::new();
loop {
line.clear();
match io::stdin().read_line(&mut line) {
Ok(bytes) => {
if bytes == 0 {
break;
}
result.push(Constellation::new_from_coords(Coords::from_string(&line)));
}
Err(err) => {
eprintln!("An error occurred while reading from stdin: {}", err);
process::exit(1);
}
}
}
return result;
}
fn main() {
let mut cons = read_stdin();
'join: loop {
let len = cons.len();
for i in 0..(len-1) {
for j in (i+1)..len {
if cons[i].can_join(&cons[j]) {
eprintln!("Joined constellation [{}]({}) and [{}]({})", i, cons[i].len(), j, cons[j].len());
let removed = cons.remove(j);
cons[i].join(&removed);
continue 'join;
}
}
}
break;
}
println!("Total number of constellations: {}", cons.len());
}