-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.rs
67 lines (56 loc) · 1.5 KB
/
day25.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! [Day 25: Code Chronicle](https://adventofcode.com/2024/day/25)
struct Puzzle<'a> {
data: &'a str,
}
impl<'a> Puzzle<'a> {
/// Initialize from the puzzle input.
const fn new(data: &'a str) -> Self {
Self { data }
}
/// Solve part one.
fn part1(&self) -> u64 {
let mut locks = Vec::new();
let mut keys = Vec::new();
for schematics in self.data.split("\n\n") {
let heights: Vec<_> = (0..5)
.map(|x| {
schematics
.lines()
.filter(|row| row.chars().nth(x).unwrap() == '#')
.count()
- 1
})
.collect();
if schematics.starts_with("#####") {
locks.push(heights);
} else {
keys.push(heights);
}
}
let mut answer = 0;
for key in &keys {
for lock in &locks {
if key.iter().zip(lock.iter()).all(|(a, b)| a + b <= 5) {
answer += 1;
}
}
}
answer
}
}
fn main() {
let args = aoc::parse_args();
let puzzle = Puzzle::new(&args.input);
println!("{}", puzzle.part1());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn part1() {
let data = aoc::load_input_data("test.txt");
let puzzle = Puzzle::new(&data);
assert_eq!(puzzle.part1(), 3);
}
}