Skip to content

Commit

Permalink
2024_25
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaikia committed Dec 25, 2024
1 parent 6c5ce03 commit 75f6a93
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/bin/2024_25/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use aoc::{common, grid::Grid};

fn solve(input: &str) -> usize {
let mut ans = 0;
let mut rows = 0;
let mut locks = Vec::new();
let mut keys = Vec::new();
for pat in input.split("\n\n") {
let grid = Grid::from_str(pat, |c| c);
rows = grid.rows;
let vec: Vec<usize> = (0..grid.cols)
.map(|c| grid.find_in_col(c, '#').len() - 1)
.collect();
if grid.get(&(0, 0)) == '#' {
// lock
locks.push(vec);
} else {
// key
keys.push(vec);
}
}

for l in locks.iter() {
for k in keys.iter() {
ans += if l.iter().zip(k.iter()).all(|(a, b)| *a + *b < rows - 1) {
1
} else {
0
};
}
}
ans
}

fn main() {
let input = common::get_input();
println!("{input:?}");
common::timed(&input, solve, true);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_samples() {
let sample_input = "#####\n.####\n.####\n.####\n.#.#.\n.#...\n.....\n\n#####\n##.##\n.#.##\n...##\n...#.\n...#.\n.....\n\n.....\n#....\n#....\n#...#\n#.#.#\n#.###\n#####\n\n.....\n.....\n#.#..\n###..\n###.#\n###.#\n#####\n\n.....\n.....\n.....\n#....\n#.#..\n#.#.#\n#####";
assert_eq!(solve(sample_input), 3);
}
}

0 comments on commit 75f6a93

Please sign in to comment.