-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.rs
149 lines (130 loc) · 3.96 KB
/
20.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
#[derive(Debug)]
struct Tile {
content: Vec<Vec<bool>>,
borders: [u16; 4],
adjacent_borders: HashSet<u16>,
}
impl Tile {
fn new(content: Vec<Vec<bool>>) -> Self {
let upper_borders = border_to_ids(content.first().expect("No upper border!"));
let lower_borders = border_to_ids(content.last().expect("No lower border!"));
let left_borders = {
let id = content
.iter()
.map(|line| *line.first().expect("No left border!"))
.enumerate()
.fold(0, |acc, (index, val)| acc | ((val as u16) << index));
(id, id.reverse_bits() >> 6)
};
let right_borders = {
let id = content
.iter()
.map(|line| *line.last().expect("No right border!"))
.enumerate()
.fold(0, |acc, (index, val)| acc | ((val as u16) << index));
(id, id.reverse_bits() >> 6)
};
let borders = [
upper_borders.0,
lower_borders.0,
left_borders.0,
right_borders.0,
];
let mut adjacent_borders = HashSet::with_capacity(8);
adjacent_borders.insert(upper_borders.0);
adjacent_borders.insert(lower_borders.0);
adjacent_borders.insert(left_borders.0);
adjacent_borders.insert(right_borders.0);
adjacent_borders.insert(upper_borders.1);
adjacent_borders.insert(lower_borders.1);
adjacent_borders.insert(left_borders.1);
adjacent_borders.insert(right_borders.1);
Tile {
content,
borders,
adjacent_borders,
}
}
pub fn get_borders(&self) -> Vec<u16> {
self.borders.to_vec()
}
pub fn is_adjacent(&self, other: &Self) -> bool {
other
.get_borders()
.into_iter()
.any(|other_border| self.adjacent_borders.contains(&other_border))
}
}
fn border_to_ids(border: &[bool]) -> (u16, u16) {
let id = border
.iter()
.enumerate()
.fold(0, |acc, (index, val)| acc | ((*val as u16) << index));
(id, id.reverse_bits() >> 6)
}
impl FromStr for Tile {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Tile::new(
s.lines()
.map(|line| line.chars().map(|character| character == '#').collect())
.collect(),
))
}
}
type Tiles = HashMap<u16, Tile>;
fn parse_tiles(input: &str) -> Tiles {
input
.split("\n\n")
.map(|tile_block| {
let tile_elements: Vec<&str> = tile_block.splitn(2, '\n').collect();
let tile_id = tile_elements[0]
.chars()
.skip(5)
.take(4)
.collect::<String>()
.parse()
.expect("Couldn't parse ID!");
(
tile_id,
tile_elements[1].parse().expect("Couldn't parse Tile"),
)
})
.collect()
}
fn get_corner_tiles(tiles: &Tiles) -> Vec<u16> {
tiles
.iter()
.filter(|(tile_id, tile)| {
tiles
.iter()
.filter(|(other_tile_id, other_tile)| {
tile_id != other_tile_id && tile.is_adjacent(other_tile)
})
.take(3)
.count()
== 2
})
.map(|(tile_id, _tile)| *tile_id)
.take(4)
.collect()
}
fn solve_part_one(tiles: &Tiles) {
let corner_product: u64 = get_corner_tiles(tiles)
.into_iter()
.map(|tile_id| tile_id as u64)
.product();
println!(
"The product of the IDs of the four corner tiles is {}.",
corner_product
);
}
fn solve_part_two() {}
fn main() {
let input = include_str!("20_data.raw");
let tiles = parse_tiles(input);
solve_part_one(&tiles);
solve_part_two();
}