Skip to content

Commit

Permalink
adding day25 2024 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Fadi88 committed Dec 25, 2024
1 parent 8fe4660 commit 17cfc02
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions 2024/day25/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::iter::zip;
use std::time::Instant;

fn bench<F, R>(f: F) -> R
Expand All @@ -10,15 +11,50 @@ where
result // Return the result of the function
}

fn part_1() {
let input = include_str!("input.txt");
fn get_heights(d: &[&str]) -> Vec<usize> {
let mut ret = Vec::new();
let width = d[0].len();

for i in 0..width {
let count = d
.iter()
.filter(|line| line.chars().nth(i) == Some('#'))
.count();
ret.push(count);
}
ret
}

fn does_fit(p: (&[usize], &[usize])) -> bool {
let (k, l) = p;
zip(k, l).all(|(ki, li)| ki + li <= 7)
}
fn part_1() {
let input_file = include_str!("input.txt"); // Include the file at compile time
let mut key_heights = Vec::new();
let mut lock_heights = Vec::new();

for block in input_file.split("\n\n") {
let lines: Vec<&str> = block.lines().collect();
if lines[0].contains('.') {
lock_heights.push(get_heights(&lines));
} else {
key_heights.push(get_heights(&lines));
}
}

let mut s = 0;
for key in &key_heights {
for lock in &lock_heights {
if does_fit((key, lock)) {
s += 1;
}
}
}

fn part_2() {
let input = include_str!("input.txt");
println!("{}", s);
}

fn main() {
bench(part_1);
bench(part_2);
}

0 comments on commit 17cfc02

Please sign in to comment.