From 9a4a51678222927b68e599d325248dc166b409f8 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 2 Dec 2020 12:28:22 -0800 Subject: [PATCH] day 2 --- src/main.rs | 13 ++++-- src/problem1/.part1.rs.swp | Bin 12288 -> 0 bytes src/problem2/mod.rs | 1 + src/problem2/part2.rs | 82 +++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-) delete mode 100644 src/problem1/.part1.rs.swp create mode 100644 src/problem2/mod.rs create mode 100644 src/problem2/part2.rs 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 b81565a4ccb8234d326f59c792494ab88836d781..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&x;&I6vr#c!5?u|^rVO6?Fg}ZGut!Mdr2nQy+i^YHi(NkDPvD}b*9|@WmWYI z%Wm{0#GC(s;=xk_f)^3Ic=IX>Lhvez2aS00Ao#8B>Fvq7Mpy8#Pz9f@?y7pPUVZCT z!LU86|f3e1*`&Afn%$HE>5tw5&g;e3a-rS69?+~tF5d8RspMkRlq7> z6|f3e1*`&A0jq#jz$#!BIED%Y5o2FI%-Fq05Ip|>-~9gn;|az-1lPbO@IeoF;92kt zI0b%soUtFl58zvH2iyj?z#Bk-=fQVNjC~5;0efH<2oQn*ybfLfSHKx?8vOhiV_$)f z!7VTbD_{xSJI&Z#@HzMd+yS@2dte5pU=u8XQ{W`H`zT}IfUm)4-~;eJD8Xgmg8}#* zTKoon1xA-&fGt)5tAJI&Dqt0`3Rne>tpYb0q11oPQz1pBlWf`Pyqt&IToZq@47A8I zic6l5P_$0FH#^veUWUYdW+IULH$>8Nq)tW4p<7?=|0lBOgw zQap*hG*D`k2&FxtxpZ9GZ6`|*SX%04nttsg&PbNo$b2&DJLim(eWNzIc7a#_`+^WR z1&ZP|pL(?B^<6Jc^4tW8R)N-4^8c504gPjr?Fwr#u_LY*=1Ias?YLdmv@X_vwP|{< zF)MlD7s29Rx-54A;$Vgdvt2;5&Y3=@fX3r)f;p7~_)};;V8naQKAf`=j&VUQ#{2qULhz`dUEDi>I9h5ZRNmFJ28 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) + } +} +