generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.rs
81 lines (64 loc) · 1.98 KB
/
25.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::collections::HashMap;
use itertools::Itertools;
advent_of_code::solution!(25);
type HeightMap = HashMap<usize, i32>;
fn parse_input(input: &str) -> (Vec<HeightMap>, Vec<HeightMap>) {
let mut locks = vec![];
let mut keys = vec![];
for schematic in input.split("\n\n").collect::<Vec<_>>().iter() {
let mut height_map: HeightMap = HashMap::new();
assert_eq!(
7,
schematic.lines().count(),
"Schematic should have 7 rows!"
);
let mut is_key = false;
for (i, line) in schematic.lines().enumerate() {
assert_eq!(5, line.len(), "Schematic should have 5 columns!");
if i == 0 {
is_key = line.chars().all(|c| c == '.');
}
for (j, c) in line.chars().enumerate() {
if c == '#' {
let height = height_map.get(&j).unwrap_or(&-1);
height_map.insert(j, height + 1);
}
}
}
if is_key {
keys.push(height_map);
} else {
locks.push(height_map);
}
}
(locks, keys)
}
fn is_fit(lock: &HeightMap, key: &HeightMap) -> bool {
(0..lock.len()).all(|i| lock.get(&i).unwrap() + key.get(&i).unwrap() <= 5)
}
pub fn part_one(input: &str) -> Option<u32> {
let (locks, keys) = parse_input(input);
let total_fit = locks
.iter()
.flat_map(|lock| keys.iter().map(move |key| (lock, key)))
.filter(|(lock, key)| is_fit(lock, key))
.count();
Some(total_fit as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(3));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}