From c13f13816e67d7f7438b468218898e0526ed39f6 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 3 Dec 2020 23:22:46 -0800 Subject: [PATCH] problem 4 part 1 --- src/main.rs | 11 ++- src/problem3/lib.rs | 9 +- src/problem4/lib.rs | 197 ++++++++++++++++++++++++++++++++++++++++++++ src/problem4/mod.rs | 1 + 4 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 src/problem4/lib.rs create mode 100644 src/problem4/mod.rs diff --git a/src/main.rs b/src/main.rs index e738cf1..8638ecd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,12 @@ extern crate tempfile; use crate::problem1::lib::Problem1; use crate::problem2::lib::Problem2; use crate::problem3::lib::Problem3; +use crate::problem4::lib::Problem4; mod problem1; mod problem2; mod problem3; +mod problem4; mod util; @@ -29,8 +31,11 @@ fn main() { //problem2.run_part1(); //problem2.run_part2(); - let problem3 = Problem3::new(&util::get_problem(3)); - problem3.run_part1(); - problem3.run_part2(); + // let problem3 = Problem3::new(&util::get_problem(3)); + // problem3.run_part1(); + // problem3.run_part2(); + let problem4 = Problem4::new(&util::get_problem(4)); + problem4.run_part1(); + problem4.run_part2(); } diff --git a/src/problem3/lib.rs b/src/problem3/lib.rs index 4f343bc..a5d6799 100644 --- a/src/problem3/lib.rs +++ b/src/problem3/lib.rs @@ -60,7 +60,11 @@ impl Problem for Problem3 { } fn run_part1(&self) { - + let v : i64 = self.traverse(1, 1); + println!("{}", v); + } + + fn run_part2(&self) { let v : i64 = self.traverse(1, 1) * self.traverse(3, 1) * self.traverse(5, 1) * @@ -68,8 +72,5 @@ impl Problem for Problem3 { self.traverse(1, 2); println!("{}", v); } - - fn run_part2(&self) { - } } diff --git a/src/problem4/lib.rs b/src/problem4/lib.rs new file mode 100644 index 0000000..16ec94d --- /dev/null +++ b/src/problem4/lib.rs @@ -0,0 +1,197 @@ +/* + byr (Birth Year) + iyr (Issue Year) + eyr (Expiration Year) + hgt (Height) + hcl (Hair Color) + ecl (Eye Color) + pid (Passport ID) + cid (Country ID) (ignore these) + +ecl:gry pid:860033327 eyr:2020 hcl:#fffffd +byr:1937 iyr:2017 cid:147 hgt:183cm + +iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 +hcl:#cfa07d byr:1929 + +hcl:#ae17e1 iyr:2013 +eyr:2024 +ecl:brn pid:760753108 byr:1931 +hgt:179cm + +hcl:#cfa07d eyr:2025 pid:166559648 +iyr:2011 ecl:brn hgt:59in +*/ + +use crate::Problem; +use std::str::FromStr; +use std::num::{ParseIntError}; + + +struct Passport { + byr: String, + iyr: String, + eyr: String, + hgt: String, + hcl: String, + ecl: String, + pid: String, + cid: Option, +} + +fn parse_from_string(s: &str) -> Option { + + 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::()) + } + } + None + }; + + 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()) + } + } + 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 eyr = match f_s("eyr", s) { + None => { return None } + Some(v) => { v } + }; + let eyr = match f_s("eyr", s) { + None => { return None } + Some(v) => { v } + }; + 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_s("pid", s) { + None => { return None } + Some(v) => { v } + }; + let cid = match f_s("cid", s) { + None => { None } + Some(v) => { Some(v) } + }; + + Some(Passport { + byr, + iyr, + eyr, + hgt, + hcl, + ecl, + pid, + cid, + }) +} + +pub struct Problem4 { + valid_passports: Vec, +} + +impl Problem4 {} + +impl Problem for Problem4 { + fn new(input: &String) -> Self { + Problem4 { + valid_passports: input + .split("\n\n") + .filter_map(|s| { + 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)} + } + } else { + None + } + }).collect(), + } + } + + fn run_part1(&self) { + println!("{}", self.valid_passports.len()) + } + + fn run_part2(&self) {} +} + diff --git a/src/problem4/mod.rs b/src/problem4/mod.rs new file mode 100644 index 0000000..965f28e --- /dev/null +++ b/src/problem4/mod.rs @@ -0,0 +1 @@ +pub mod lib;