aoc_2024/day25/src/main.rs

84 lines
2.2 KiB
Rust
Raw Permalink Normal View History

use std::env;
use std::fs;
use std::iter::zip;
fn main() {
println!("Hello, AoC day 24!");
let args: Vec<String> = 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::<Vec<_>>()
)
.collect::<Vec<_>>();
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::<Vec<_>>()
)
.collect::<Vec<_>>();
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<Vec<char>> {
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<usize>, key: &Vec<usize>) -> 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;
}