problem 4 part 1

master
mitchellhansen 4 years ago
parent d53d13b872
commit c13f13816e

@ -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();
}

@ -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) {
}
}

@ -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<String>,
}
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>())
}
}
None
};
let f_s : fn(&str, &str) -> Option<String> = |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<Passport>,
}
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) {}
}

@ -0,0 +1 @@
pub mod lib;
Loading…
Cancel
Save