-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpanel-count.rs
74 lines (57 loc) · 1.82 KB
/
panel-count.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
use std::{io, collections::HashMap};
macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}
fn read_int() -> i32 {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
parse_input!(input_line, i32)
}
fn read_str() -> String {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
input_line.trim_matches('\n').to_string()
}
fn read_person() -> Vec<String> {
read_str().split(' ').map(|x| {x.to_string()}).collect()
}
struct Formula {
properties: HashMap<u32, String>
}
impl Formula {
fn new(formula_str: &str, dic: &HashMap<String,u32> ) -> Formula {
let properties = formula_str
.split(" AND ")
.filter_map(|condition| {
condition.split_once('=').map(|(prop, val)| {
(dic.get(&prop.trim().to_string()).unwrap_or(&99).clone(), val.trim().to_string())
})
})
.collect();
Formula { properties }
}
fn matches(&self, person: &Vec<String>) -> bool {
for (&idx, val) in &self.properties {
if person.get(idx as usize).map_or(true, |p| p != val) {
return false;
}
}
true
}
}
fn main() {
let mut properties: HashMap<String,u32> = HashMap::new();
let mut persons: Vec<Vec<String>> = Vec::new();
for i in 0..read_int() as usize {
properties.insert(read_str(), (i + 1) as u32);
}
for _ in 0..read_int() as usize {
persons.push(read_person());
}
for _ in 0..read_int() as usize {
let mut cmpt = 0u32;
let formula = Formula::new(&read_str(), &properties);
persons.iter().for_each(|x| cmpt += formula.matches(x) as u32);
println!("{}", cmpt);
}
}