From 24b0056f356931a6414069bbb487b3a5f7919cc1 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Fri, 4 Dec 2020 17:02:26 -0800 Subject: [PATCH] part2 --- src/problem4/lib.rs | 190 ++++++++++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 79 deletions(-) diff --git a/src/problem4/lib.rs b/src/problem4/lib.rs index 16ec94d..f5d73b7 100644 --- a/src/problem4/lib.rs +++ b/src/problem4/lib.rs @@ -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,112 +40,140 @@ struct Passport { } fn parse_from_string(s: &str) -> Option { - - let f_i : fn(&str, &str) -> Option> = |key, glob| { + let f_i: fn(&str, &str) -> Option> = |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::()) + return Some(q.parse::()); } } None }; - let f_s : fn(&str, &str) -> Option = |key, glob| { + let f_s: fn(&str, &str) -> Option = |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.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::().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::().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