From cfd9b4f2ad1a09bedf7f764f84448a61faab54a3 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 20:26:02 +0200 Subject: Refile for merging repos --- 2018/src/bin/day_2.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2018/src/bin/day_2.rs (limited to '2018/src/bin/day_2.rs') diff --git a/2018/src/bin/day_2.rs b/2018/src/bin/day_2.rs new file mode 100644 index 0000000..7700871 --- /dev/null +++ b/2018/src/bin/day_2.rs @@ -0,0 +1,53 @@ +extern crate advent_of_code_2018; +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> { + 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(()) +} -- cgit v1.2.3