-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.rs
166 lines (141 loc) · 4.18 KB
/
23.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#![feature(test)]
use std::{cmp::Reverse, collections::BinaryHeap};
use rustc_hash::FxHashSet;
type Input = [Vec<char>; 4];
fn setup(input: &str) -> Input {
let mut out = [vec![], vec![], vec![], vec![]];
let lines: Vec<(char, char, char, char)> = input
.lines()
.skip(2)
.take(2)
.map(|line| {
let get = |i| line.chars().nth(3 + 2 * i as usize).unwrap();
(get(0), get(1), get(2), get(3))
})
.collect();
for line in lines.iter().rev() {
out[0].push(line.0);
out[1].push(line.1);
out[2].push(line.2);
out[3].push(line.3);
}
out
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone)]
struct State {
energy: u32,
rooms: [Vec<char>; 4],
hallway: [char; 11],
}
impl State {
fn make_key(&self) -> ([Vec<char>; 4], [char; 11]) {
(self.rooms.clone(), self.hallway)
}
fn check_room(&self, idx: usize) -> bool {
let c = "ABCD".chars().nth(idx).unwrap();
self.rooms[idx].iter().all(|&x| x == c)
}
fn done(&self) -> bool {
self.hallway.iter().all(|&x| x == '.') && (0..4).all(|i| self.check_room(i))
}
fn check_hallway(&self, start: usize, end: usize) -> bool {
(start.min(end)..=end.max(start)).all(|i| self.hallway[i] == '.' || i == start)
}
fn add_cost(mut self, cost: u32) -> Self {
self.energy += cost;
self
}
fn push_room(mut self, idx: usize, c: char) -> Self {
self.rooms[idx].push(c);
self
}
fn pop_room(mut self, idx: usize) -> Self {
self.rooms[idx].pop();
self
}
fn set_hallway(mut self, idx: usize, c: char) -> Self {
self.hallway[idx] = c;
self
}
fn generate_moves(&self, n: usize) -> Vec<State> {
for (i, &c) in self.hallway.iter().enumerate() {
if c == '.' {
continue;
}
let dst = "ABCD".find(c).unwrap();
if self.rooms[dst].iter().any(|&x| x != c) {
continue;
}
if !self.check_hallway(i, 2 + 2 * dst) {
continue;
}
let dist = i.abs_diff(2 + 2 * dst) + n - self.rooms[dst].len();
return vec![self
.clone()
.add_cost(dist as u32 * 10u32.pow(dst as u32))
.push_room(dst, c)
.set_hallway(i, '.')];
}
let mut out = vec![];
for i in 0..4 {
if self.check_room(i) {
continue;
}
let c = *self.rooms[i].last().unwrap();
let src = 2 + 2 * i;
let dst = "ABCD".find(c).unwrap();
for j in [0, 1, 3, 5, 7, 9, 10] {
if !self.check_hallway(src, j) {
continue;
}
let dist = (1 + n - self.rooms[i].len()) + src.abs_diff(j);
out.push(
self.clone()
.add_cost(dist as u32 * 10u32.pow(dst as u32))
.pop_room(i)
.set_hallway(j, c),
)
}
}
out
}
}
fn solve(input: &Input, part2: bool) -> u32 {
let mut input = input.clone();
if part2 {
(0..4).for_each(|i| {
input[i].insert(1, "DBAC".chars().nth(i).unwrap());
input[i].insert(2, "DCBA".chars().nth(i).unwrap());
});
}
let n = input[0].len();
let mut queue = BinaryHeap::new();
queue.push(Reverse(State {
energy: 0,
rooms: input,
hallway: ['.'; 11],
}));
let mut visited = FxHashSet::default();
while !queue.is_empty() {
let state = queue.pop().unwrap().0;
let key = state.make_key();
if visited.contains(&key) {
continue;
}
visited.insert(key);
if state.done() {
return state.energy;
}
for next in state.generate_moves(n) {
queue.push(Reverse(next));
}
}
panic!();
}
fn part1(input: &Input) -> String {
solve(input, false).to_string()
}
fn part2(input: &Input) -> String {
solve(input, true).to_string()
}
aoc::main!(2021, 23, ex: 1);