-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwhat-is-your-garden-worth.rs
More file actions
43 lines (34 loc) · 1.24 KB
/
what-is-your-garden-worth.rs
File metadata and controls
43 lines (34 loc) · 1.24 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
use std::io;
use std::collections::HashMap;
macro_rules! parse_input {
($t:ident) => {{
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
input_line.trim_matches('\n').parse::<$t>().unwrap()
}}
}
fn parse_offer(offer: String, into: &mut HashMap<String, u32>) {
let parts: Vec<&str> = offer.split(" = ").collect();
if 2 != parts.len() {
panic!("Invalid offering format");
}
let price = parts[0].trim_start_matches("$").parse::<u32>().unwrap_or(0);
let vegetables: Vec<String> = parts[1].chars().filter(|&c| !c.is_whitespace())
.map(|c| c.to_string())
.collect();
for vegetable in vegetables {
into.insert(vegetable.clone(), price);
}
}
fn main() {
let mut offers: HashMap<String, u32> = HashMap::new();
let mut res = 0;
for _ in 0..parse_input!(i32) as usize {
parse_offer(parse_input!(String), &mut offers);
}
for _ in 0..parse_input!(i32) as usize {
res += parse_input!(String).chars().filter_map(|c| offers.get(&c.to_string()))
.sum::<u32>();
}
println!("${}", res.to_string().as_bytes().rchunks(3).rev().map(std::str::from_utf8).collect::<Result<Vec<&str>, _>>().unwrap().join(","));
}