-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheap-choices.rs
70 lines (53 loc) · 2.16 KB
/
cheap-choices.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
use std::io;
use std::collections::{ HashMap, BTreeMap, BTreeSet };
macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}
fn parse_input(input: &str) -> Option<(String, String, u32)> {
let mut parts = input.split_whitespace();
let category = parts.next()?.to_string();
let size = parts.next()?.to_string();
let price = parts.next()?.parse::<u32>().ok()?;
return Some((category, size, price));
}
fn parse_order(input: &str) -> Option<(String, String)> {
let mut parts = input.split_whitespace();
let category = parts.next()?.to_string();
let size = parts.next()?.to_string();
return Some((category, size));
}
fn main() {
let mut inventory: HashMap<String, HashMap<String, BTreeSet<u32>>> = HashMap::new();
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let c = parse_input!(input_line, i32);
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let p = parse_input!(input_line, i32);
for _ in 0..c as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let item = input_line.trim_matches('\n').to_string();
if let Some((category, size, price)) = parse_input(&item) {
let elems_map = inventory.entry(category).or_insert_with(HashMap::new);
let prices_set = elems_map.entry(size).or_insert_with(BTreeSet::new) ;
prices_set.insert(price);
}
}
for _ in 0..p as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let order = input_line.trim_matches('\n').to_string();
let mut reply = "NONE".to_string();
if let Some((category, size)) = parse_order(&order) {
if let Some(elems_map) = inventory.get_mut(&category) {
if let Some(prices_set) = elems_map.get_mut(&size) {
if let Some(first) = prices_set.pop_first() {
reply = first.to_string();
}
}
}
}
println!("{}", reply);
}
}