master
MitchellHansen 4 years ago
parent 9eaf7517af
commit 9a4a516782

@ -2,10 +2,13 @@ extern crate reqwest;
extern crate tempfile; extern crate tempfile;
use crate::problem1::part1::Problem1; use crate::problem1::part1::Problem1;
use crate::problem2::part2::Problem2;
mod problem1; mod problem1;
mod problem2;
mod util; mod util;
pub trait Problem { pub trait Problem {
// Parses input and generates state // Parses input and generates state
fn new(input: &String) -> Self; fn new(input: &String) -> Self;
@ -16,7 +19,11 @@ pub trait Problem {
} }
fn main() { fn main() {
let problem1 = Problem1::new(&util::get_problem(1)); // let problem1 = Problem1::new(&util::get_problem(1));
problem1.run_part1(); // problem1.run_part1();
problem1.run_part2(); // problem1.run_part2();
let problem2 = Problem2::new(&util::get_problem(2));
problem2.run_part1();
problem2.run_part2();
} }

Binary file not shown.

@ -0,0 +1 @@
pub mod part2;

@ -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::<i32>().unwrap();
let max = range_iter.next().unwrap().parse::<i32>().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::<usize>().unwrap();
let second = range_iter.next().unwrap().parse::<usize>().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)
}
}
Loading…
Cancel
Save