-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.rs
45 lines (40 loc) · 1.48 KB
/
main.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
#![feature(str_split_once)]
use std::collections::HashMap;
const SIZE: usize = 10;
#[rustfmt::skip]
pub fn main() {
let tiles = include_str!("../input.txt")
.split("\n\n")
.map(|data| {
let (id, cells) = data.split_once('\n').unwrap();
let id = id[5..].trim_end_matches(":").parse().unwrap();
let cells: Vec<u8> = cells.bytes().filter(|&b| b != b'\n').collect();
(id, [
cells[0..SIZE].into_iter()
.fold(0u16, |e, &c| e << 1 | (c == b'#') as u16),
(0..SIZE).map(|i| cells[SIZE - 1 + i * SIZE])
.fold(0u16, |e, c| e << 1 | (c == b'#') as u16),
cells[SIZE * (SIZE - 1)..SIZE * SIZE].into_iter()
.fold(0u16, |e, &c| e << 1 | (c == b'#') as u16),
(0..SIZE).map(|i| cells[i * SIZE])
.fold(0u16, |e, c| e << 1 | (c == b'#') as u16),
])
})
.collect::<Vec<(usize, [u16; 4])>>();
let edges: HashMap<u16, u8> = tiles
.iter()
.flat_map(|t| t.1.iter().map(|&e| [e, e.reverse_bits() >> 6]))
.fold(HashMap::new(), |mut map, [a, b]| {
*map.entry(a).or_default() += 1;
*map.entry(b).or_default() += 1;
map
});
println!(
"{}",
tiles
.iter()
.filter(|t| t.1.iter().filter(|e| edges[e] > 1).take(3).count() < 3)
.map(|t| t.0)
.product::<usize>()
);
}