Created
December 31, 2024 04:41
-
-
Save icub3d/e0e15045c25bbdc941439cdb0e9dcc38 to your computer and use it in GitHub Desktop.
Advent of Code 2024 - Day 25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn height(input: &str) -> Vec<isize> { | |
input | |
.lines() | |
.flat_map(|line| line.chars().enumerate()) | |
.filter(|(_, c)| *c == '#') | |
.fold(vec![-1; 5], |mut heights, (i, _)| { | |
heights[i] += 1; | |
heights | |
}) | |
} | |
fn fit(key: &[isize], lock: &[isize]) -> bool { | |
key.iter().zip(lock.iter()).all(|(&k, l)| k < 6 - l) | |
} | |
fn main() { | |
let input = include_str!("input.txt"); | |
let now = std::time::Instant::now(); | |
let (keys, locks) = | |
input | |
.split("\n\n") | |
.fold((Vec::new(), Vec::new()), |(mut keys, mut locks), group| { | |
match group.chars().next() { | |
Some('#') => locks.push(height(group)), | |
_ => keys.push(height(group)), | |
} | |
(keys, locks) | |
}); | |
let p1 = locks | |
.iter() | |
.map(|lock| keys.iter().filter(|key| fit(key, lock)).count()) | |
.sum::<usize>(); | |
println!("p1: {} ({:?})", p1, now.elapsed()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment