diff options
Diffstat (limited to '2024/day25.rs')
| -rw-r--r-- | 2024/day25.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/2024/day25.rs b/2024/day25.rs new file mode 100644 index 0000000..507c53d --- /dev/null +++ b/2024/day25.rs @@ -0,0 +1,63 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_mut)] +use std::env; +use std::fs::File; +use std::io::Read; + +fn main() { + let args: Vec<String> = env::args().collect(); + let filename = if args.len() == 1 { + "in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl" + } else { + args[1].clone() + }; + let mut f = File::open(filename).expect("cannot open file"); + let mut content = String::new(); + f.read_to_string(&mut content).expect("cannot read file"); + let paras = content.trim_end().split("\n\n"); + + let mut locks: Vec<Vec<i64>> = Vec::new(); + let mut keys: Vec<Vec<i64>> = Vec::new(); + + for para in paras { + let mut para = para.split("\n"); + let line = para.next().unwrap(); + + if line == "#####" { + let mut key = vec![0; 5]; + for line in para { + for (i, c) in line.chars().enumerate() { + if c == '#' { + key[i] += 1; + } + } + } + keys.push(key) + } else { + let mut lock = vec![-1; 5]; + for line in para { + for (i, c) in line.chars().enumerate() { + if c == '#' { + lock[i] += 1; + } + } + } + locks.push(lock) + } + } + //println!("{:?}", locks); + //println!("{:?}", keys); + + let mut res1 = 0; + for l in locks.clone() { + for k in keys.clone() { + if l.iter().zip(k.iter()).map(|(a, b)| a + b).all(|x| x <= 5) { + res1 += 1; + } + } + } + + println!("res1: {}", res1); + assert_eq!(res1, 2993); +} |
