-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.rs
More file actions
74 lines (63 loc) · 1.88 KB
/
Copy pathmain.rs
File metadata and controls
74 lines (63 loc) · 1.88 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
pub mod common;
use common::Timer;
fn solution(path: &str) -> String {
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let mut map = HashMap::new();
struct Status {
min: i64,
max: i64,
total: i64,
count: i64,
}
for line_result in reader.lines() {
let line = line_result.unwrap();
let mut parts = line.split(';');
let city_name = parts.next().unwrap().to_string();
let measurement = parts.next().unwrap().parse::<i64>().unwrap();
if let Some(Status {
min,
max,
total,
count,
}) = map.get_mut(&city_name)
{
*min = (*min).min(measurement);
*max = (*max).max(measurement);
*total += measurement;
*count += 1;
} else {
map.insert(
city_name,
Status {
min: measurement,
max: measurement,
total: measurement,
count: 1,
},
);
}
}
let mut bucket = String::new();
let mut list = map.into_iter().collect::<Vec<_>>();
list.sort_by(|a, b| a.0.cmp(&b.0));
for (city_name, status) in list {
let avg = status.total / status.count;
let line = format!(
"{}={};{};{}({}/{})\n",
city_name, status.min, status.max, avg, status.total, status.count,
);
bucket.push_str(&line);
}
bucket
}
fn main() {
let expect_output = std::fs::read_to_string(common::OUTPUT_PATH).unwrap();
let timer = Timer::new();
let got = solution(common::MEASUREMENTS_PATH);
println!("Elapsed: {}ms", timer.elapsed_as_millis());
assert_eq!(expect_output, got);
}