diff --git a/src/main.rs b/src/main.rs index 3332bc7..7abe676 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,13 @@ extern crate reqwest; extern crate tempfile; use crate::problem1::part1::Problem1; +use crate::problem2::part2::Problem2; mod problem1; +mod problem2; mod util; + pub trait Problem { // Parses input and generates state fn new(input: &String) -> Self; @@ -16,7 +19,11 @@ pub trait Problem { } fn main() { - let problem1 = Problem1::new(&util::get_problem(1)); - problem1.run_part1(); - problem1.run_part2(); + // let problem1 = Problem1::new(&util::get_problem(1)); + // problem1.run_part1(); + // problem1.run_part2(); + + let problem2 = Problem2::new(&util::get_problem(2)); + problem2.run_part1(); + problem2.run_part2(); } \ No newline at end of file diff --git a/src/problem1/.part1.rs.swp b/src/problem1/.part1.rs.swp deleted file mode 100644 index b81565a..0000000 Binary files a/src/problem1/.part1.rs.swp and /dev/null differ diff --git a/src/problem2/mod.rs b/src/problem2/mod.rs new file mode 100644 index 0000000..c281610 --- /dev/null +++ b/src/problem2/mod.rs @@ -0,0 +1 @@ +pub mod part2; \ No newline at end of file diff --git a/src/problem2/part2.rs b/src/problem2/part2.rs new file mode 100644 index 0000000..2bcc2ac --- /dev/null +++ b/src/problem2/part2.rs @@ -0,0 +1,82 @@ +/* +Each line gives the password policy and then the password. +The password policy indicates the lowest and highest number +of times a given letter must appear for the password to be +valid. For example, 1-3 a means that the password must +contain a at least 1 time and at most 3 times. + +Each policy actually describes two positions in the password, +where 1 means the first character, 2 means the second character, +and so on. (Be careful; Toboggan Corporate Policies have no +concept of "index zero"!) Exactly one of these positions must +contain the given letter. Other occurrences of the letter are +irrelevant for the purposes of policy enforcement. +*/ + +use crate::Problem; + +pub struct Problem2 { + password_list: Vec<(String, String, String)>, +} + +impl Problem2 {} + +impl Problem for Problem2 { + fn new(input: &String) -> Self { + Problem2 { + password_list: input + .split("\n") + .filter_map(|s| { + let s = s.trim(); + if s.len() < 2 { + return None + } + let mut iter = s.split(" "); + let range = iter.next().unwrap(); + let val = iter.next().unwrap().replace(':',""); + let password = iter.next().unwrap(); + + Some((range.to_string(), val.to_string(), password.to_string())) + }).collect(), + } + } + + fn run_part1(&self) { + let mut valid_passwords = 0; + for set in &self.password_list { + let mut range_iter = set.0.split("-"); + let min = range_iter.next().unwrap().parse::().unwrap(); + let max = range_iter.next().unwrap().parse::().unwrap(); + + let count = set.2.matches(&set.1).count() as i32; + if count >= min && count <= max { + valid_passwords += 1; + //println!("{} {} {}", set.0, set.1, set.2) + } + } + println!("{}", valid_passwords) + } + + fn run_part2(&self) { + let mut valid_passwords = 0; + for set in &self.password_list { + let mut range_iter = set.0.split("-"); + let first = range_iter.next().unwrap().parse::().unwrap(); + let second = range_iter.next().unwrap().parse::().unwrap(); + + let a = set.2.get((first - 1)..first).unwrap(); + let b = set.2.get((second - 1)..second).unwrap(); + + if a == set.1 || b == set.1 { + if a == set.1 && b == set.1 { + // nope + } else { + valid_passwords += 1; + //println!("{} {} {}", set.0, set.1, set.2) + } + } + } + println!("{}", valid_passwords) + } +} +