1
0
Fork 0

Added Day 4 Part 2

This commit is contained in:
Ishan Jain 2020-12-04 11:09:32 +05:30
parent 7ca65d40dd
commit 992a5586ec
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
1 changed files with 46 additions and 4 deletions

View File

@ -38,9 +38,51 @@ fn main() {
}
fn validate(map: &HashMap<String, String>, key: &str) -> bool {
if let Some(v) = map.get(key) {
!v.is_empty()
match (key, map.get(key)) {
("byr", Some(v)) => {
let byr = v.parse::<i32>().unwrap();
byr >= 1920 && byr <= 2002
}
("iyr", Some(v)) => {
let iyr = v.parse::<i32>().unwrap();
iyr >= 2010 && iyr <= 2020
}
("eyr", Some(v)) => {
let eyr = v.parse::<i32>().unwrap();
eyr >= 2020 && eyr <= 2030
}
("hgt", Some(v)) => {
if v.contains("cm") {
let v = v.trim_end_matches(char::is_alphabetic);
let v = v.parse::<i32>().unwrap();
v >= 150 && v <= 193
} else if v.contains("in") {
let v = v.trim_end_matches(char::is_alphabetic);
let v = v.parse::<i32>().unwrap();
v >= 59 && v <= 76
} else {
false
}
}
("hcl", Some(v)) => {
v.starts_with('#')
&& v.chars()
.skip(1)
.filter(|x| match x {
'0'..='9' => true,
'a'..='f' => true,
_ => false,
})
.count()
== 6
}
("ecl", Some(v)) => match v.as_ref() {
"amb" | "blu" | "brn" | "gry" | "grn" | "hzl" | "oth" => true,
_ => false,
},
("pid", Some(v)) => v.len() == 9 && v.chars().filter(|&x| char::is_numeric(x)).count() == 9,
("cid", _) => true,
(_, None) => false,
(_, _) => unreachable!(),
}
}