use std::env; use std::fs; use std::iter::zip; fn main() { println!("Hello, AoC day 24!"); let args: Vec = env::args().collect(); if args.len() != 2 { println!("wrong number of arguments!"); std::process::exit(1); } let file_path = &args[1]; let file = fs::read_to_string(file_path) .expect("should be able to read the file"); let keys = file.split("\n\n") .filter(|section| section.starts_with(".....")) .map(string_transpose) .map(|vec_of_vec_of_char| vec_of_vec_of_char .into_iter() .map(|vec_of_char| vec_of_char .into_iter() .filter(|c| *c == '#') .count() - 1 ) .collect::>() ) .collect::>(); let locks = file.split("\n\n") .filter(|section| section.starts_with("#####")) .map(string_transpose) .map(|vec_of_vec_of_char| vec_of_vec_of_char .into_iter() .map(|vec_of_char| vec_of_char .into_iter() .filter(|c| *c == '#') .count() - 1 ) .collect::>() ) .collect::>(); println!("{keys:?}, {locks:?}"); let mut sum = 0; for lock in &locks { for key in &keys { if fits(lock, key) { sum += 1; } } } println!("the number of unique lock/key pairs is {sum}"); } fn string_transpose(several_lines: &str) -> Vec> { let mut output = Vec::with_capacity(several_lines.lines().next().unwrap().len()); for line in several_lines.lines() { for (idx, chr) in line.chars().enumerate() { if idx == output.len() { output.push(vec![]); } output[idx].push(chr); } } return output } fn fits(lock: &Vec, key: &Vec) -> bool { //println!("testing {lock:?} against {key:?}"); for (tumbler, tooth) in zip(lock, key) { //println!("tumbler: {tumbler}; tooth: {tooth}"); if *tumbler + *tooth > 5 { return false; } } //println!("we good!"); return true; }