Skip to content

Commit 14e6a84

Browse files
committed
25th day
1 parent 25c8ab6 commit 14e6a84

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

data/examples/25.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#####

src/bin/25.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
advent_of_code::solution!(25);
2+
3+
fn parse_data(input: &str) -> Vec<Vec<u8>> {
4+
input
5+
.split("\n\n")
6+
.map(|s| s.bytes().filter(|c| !c.is_ascii_whitespace()).collect())
7+
.collect()
8+
}
9+
10+
pub fn part_one(input: &str) -> Option<u32> {
11+
let schematics = parse_data(input);
12+
13+
let mut locks_and_keys = [
14+
Vec::with_capacity(schematics.len()),
15+
Vec::with_capacity(schematics.len()),
16+
];
17+
18+
for schematic in schematics {
19+
let locks_and_keys_index = if schematic[0] == b'#' { 0 } else { 1 };
20+
21+
let mut pins = [0; 5];
22+
schematic
23+
.into_iter()
24+
.enumerate()
25+
.for_each(|(i, c)| pins[i % 5] += if c == b'#' { 1 } else { 0 });
26+
27+
locks_and_keys[locks_and_keys_index].push(pins);
28+
}
29+
30+
let [locks, keys] = locks_and_keys;
31+
32+
let result = locks
33+
.into_iter()
34+
.flat_map(|lock| std::iter::repeat(lock).zip(keys.iter()))
35+
.filter(|(lock, key)| lock.iter().zip(key.iter()).all(|(l, k)| l + k <= 7))
36+
.count() as u32;
37+
38+
Some(result)
39+
}
40+
41+
pub fn part_two(_input: &str) -> Option<String> {
42+
// "Thank you Eric for another wonderful year of AoC!"
43+
Some(String::from("⭐️⭐️"))
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
fn test_part_one() {
52+
let input = advent_of_code::template::read_file("examples", DAY);
53+
let result = part_one(&input);
54+
assert_eq!(result, Some(3));
55+
}
56+
57+
#[test]
58+
fn test_part_two() {
59+
let input = advent_of_code::template::read_file("examples", DAY);
60+
let result = part_two(&input);
61+
assert_eq!(result, Some(String::from("⭐️⭐️")));
62+
}
63+
}

0 commit comments

Comments
 (0)