master
mitchellhansen 4 years ago
parent c13f13816e
commit 24b0056f35

@ -23,10 +23,10 @@ hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in
*/
use crate::Problem;
use std::num::ParseIntError;
use std::str::FromStr;
use std::num::{ParseIntError};
use crate::Problem;
struct Passport {
byr: String,
@ -40,14 +40,13 @@ struct Passport {
}
fn parse_from_string(s: &str) -> Option<Passport> {
let f_i: fn(&str, &str) -> Option<Result<i64, ParseIntError>> = |key, glob| {
let mut i = glob.split(" ");
for pair in i {
let mut i = pair.split(":");
if i.next().unwrap() == key {
let q = i.next().unwrap();
return Some(q.parse::<i64>())
return Some(q.parse::<i64>());
}
}
None
@ -58,94 +57,123 @@ fn parse_from_string(s: &str) -> Option<Passport> {
for pair in i {
let mut i = pair.split(":");
if i.next().unwrap() == key {
let q = i.next().unwrap();
return Some(q.to_string())
return Some(q.to_string());
}
}
None
};
// let byr = match f_i("byr", s) {
// None => { return None }
// Some(v) => { match v {
// Ok(v) => { v }
// Err(e) => { return None }
// }}
// };
// let iyr = match f_i("iyr", s) {
// None => { return None }
// Some(v) => { match v {
// Ok(v) => { v }
// Err(e) => { return None }
// }}
// };
// let eyr = match f_i("eyr", s) {
// None => { return None }
// Some(v) => { match v {
// Ok(v) => { v }
// Err(e) => { return None }
// }}
// };
// let hgt = match f_s("hgt", s) {
// None => { return None }
// Some(v) => { v }
// };
// let hcl = match f_s("hcl", s) {
// None => { return None }
// Some(v) => { v }
// };
// let ecl = match f_s("ecl", s) {
// None => { return None }
// Some(v) => { v }
// };
// let pid = match f_i("pid", s) {
// None => { return None }
// Some(v) => { match v {
// Ok(v) => { v }
// Err(e) => { return None }
// }}
// };
// let cid = match f_i("cid", s) {
// None => { None }
// Some(v) => { match v {
// Ok(v) => { Some(v) }
// Err(e) => { None }
// }}
// };
let byr = match f_s("byr", s) {
None => { return None }
Some(v) => { v }
};
let iyr = match f_s("iyr", s) {
None => { return None }
Some(v) => { v }
let byr = match f_i("byr", s) {
None => { return None; }
Some(v) => {
match v {
Ok(v) => {
if v >= 1920 && v <= 2002 {
v.to_string()
} else {
return None;
}
}
Err(e) => { return None; }
}
}
};
let eyr = match f_s("eyr", s) {
None => { return None }
Some(v) => { v }
let iyr = match f_i("iyr", s) {
None => { return None; }
Some(v) => {
match v {
Ok(v) => {
if v >= 2010 && v <= 2020 {
v.to_string()
} else {
return None;
}
}
Err(e) => { return None; }
}
}
};
let eyr = match f_s("eyr", s) {
None => { return None }
Some(v) => { v }
let eyr = match f_i("eyr", s) {
None => { return None; }
Some(v) => {
match v {
Ok(v) => {
if v >= 2020 && v <= 2030 {
v.to_string()
} else {
return None;
}
}
Err(e) => { return None; }
}
}
};
let hgt = match f_s("hgt", s) {
None => { return None }
Some(v) => { v }
None => { return None; }
Some(v) => {
if v.find("cm").is_some() {
let height = v.split("cm").next().unwrap().parse::<i32>().unwrap();
if height >= 150 && height <= 193 {
v
} else {
//println!("height {}", height);
return None;
}
} else if v.find("in").is_some() {
let height = v.split("in").next().unwrap().parse::<i32>().unwrap();
if height >= 59 && height <= 76 {
v
} else {
// println!("height {}", height);
return None;
}
} else { return None; }
}
};
let hcl = match f_s("hcl", s) {
None => { return None }
Some(v) => { v }
None => { return None; }
Some(v) => {
if v.starts_with("#") &&
v[1..].len() == 6 &&
v[1..].chars().all(|s| {
if s >= '0' && s <= '9' ||
s >= 'a' && s <= 'f' {
true
} else {
false
}
}) {
v
} else {
return None;
}
}
};
let ecl = match f_s("ecl", s) {
None => { return None }
Some(v) => { v }
None => { return None; }
Some(v) => {
if v == "amb" ||
v == "blu" ||
v == "brn" ||
v == "gry" ||
v == "grn" ||
v == "hzl" ||
v == "oth" {
v
} else {
return None;
}
}
};
let pid = match f_s("pid", s) {
None => { return None }
Some(v) => { v }
None => { return None; }
Some(v) => {
if v.chars().all(char::is_numeric) && v.len() == 9
{ v } else { return None; }
}
};
let cid = match f_s("cid", s) {
None => { None }
Some(v) => { Some(v) }
@ -178,8 +206,12 @@ impl Problem for Problem4 {
let s = s.trim().replace("\n", " ");
if !s.is_empty() {
match parse_from_string(s.as_str()) {
None => {println!("{}", s); None}
Some(v) => {Some(v)}
None => {
None
}
Some(v) => {
println!("{}", s);
Some(v) }
}
} else {
None

Loading…
Cancel
Save