diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-12-02 07:27:13 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-12-02 07:27:13 +0200 |
commit | 4002dbfa903aae2ece66174bc28e7410768c084e (patch) | |
tree | 7549ded196af0d61a8d98901cbbec6089e0ad859 /src | |
parent | 57d07a9f54ef4dcccf7ceca00c8fa9d73b430ba2 (diff) |
Day 2: Scanning strings
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/day_2.rs | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs index add43bf..7700871 100644 --- a/src/bin/day_2.rs +++ b/src/bin/day_2.rs @@ -4,15 +4,50 @@ use advent_of_code_2018::*; use std::error::Error; use std::path::PathBuf; +extern crate im_rc; +use im_rc::HashMap; + // cargo watch -cs "cargo run --release --bin day_2" fn main() -> Result<(), Box<Error>> { let input = read_file(&PathBuf::from("inputs/2.txt"))?; println!("Input: {:?}", input); - - - - + + let (twice, thrice) = input.iter().fold((0,0), |(twice, thrice), next| { + let occurances = next.chars().fold(HashMap::new(), |occurances, c| { + let counter = occurances.get(&c).cloned().unwrap_or(0); + occurances.update(c, counter + 1) + }); + let has_twice = occurances.values().any(|count| *count == 2); + let has_thrice = occurances.values().any(|count| *count == 3); + + ( + twice + if has_twice { 1 } else { 0 }, + thrice + if has_thrice { 1 } else { 0 }, + ) + }); + + println!("Twice: {}", twice); + println!("Thrice: {}", thrice); + + let checksum = twice * thrice; + println!("Checksum: {}", checksum); + + for i in &input { + for j in &input { + let diff = i.chars().zip(j.chars()).fold(0, |diff, (x, y)| { + if x != y { + diff + 1 + } else { + diff + } + }); + if diff == 1 { + println!("Diff of 1: {} + {}", i, j); + } + } + } + Ok(()) } |