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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use super::int_code::Program;
use std::collections::{HashSet, VecDeque};
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
enum Direction {
North,
East,
South,
West,
}
impl Direction {
const fn reverse(self) -> Self {
match self {
Self::North => Self::South,
Self::East => Self::West,
Self::South => Self::North,
Self::West => Self::East,
}
}
const fn as_str(self) -> &'static str {
match self {
Self::North => "north",
Self::East => "east",
Self::South => "south",
Self::West => "west",
}
}
fn from_str(string: &str) -> Option<Self> {
match string {
"north" => Some(Self::North),
"east" => Some(Self::East),
"south" => Some(Self::South),
"west" => Some(Self::West),
_ => None,
}
}
}
enum Command<'a> {
Move(Direction),
Take(&'a str),
Drop(&'a str),
}
struct Room {
id: String,
directions: Vec<Direction>,
items: Vec<String>,
solution: Option<SolutionType>,
}
type SolutionType = i64;
fn execute_command(program: &mut Program, command: Command) -> Result<Room, String> {
match command {
Command::Move(direction) => {
program.input_string(&format!("{}\n", direction.as_str()));
}
Command::Take(item) => {
program.input_string(&format!("take {}\n", item));
}
Command::Drop(item) => {
program.input_string(&format!("drop {}\n", item));
}
}
parse_output(program)
}
fn parse_output(program: &mut Program) -> Result<Room, String> {
let output = program.run_for_output()?;
let output: Vec<u8> = output.iter().map(|&b| b as u8).collect();
let output = std::str::from_utf8(&output).map_err(|_| "Invalid input: Not utf-8")?;
let mut directions = Vec::new();
let mut items = Vec::new();
let mut room_id = "";
let mut solution = None;
for line in output.lines() {
if line.starts_with("== ") {
room_id = line;
} else if let Some(item) = line.strip_prefix("- ") {
match Direction::from_str(item) {
Some(direction) => {
directions.push(direction);
}
None => {
items.push(item.to_string());
}
}
} else if line.starts_with("\"Oh, hello! You should be able to get in by typing") {
let error_message = "Unable to parse typing instruction";
solution = Some(
line.split_whitespace()
.nth(11)
.ok_or(error_message)?
.parse::<SolutionType>()
.map_err(|_| error_message)?,
);
}
}
Ok(Room {
id: room_id.to_string(),
directions,
items,
solution,
})
}
pub fn part1(input_string: &str) -> Result<SolutionType, String> {
let mut program = Program::parse(input_string)?;
let initial_room = parse_output(&mut program)?;
let mut blacklisted_items = HashSet::new();
blacklisted_items.insert("infinite loop".to_string());
blacklisted_items.insert("photons".to_string());
blacklisted_items.insert("giant electromagnet".to_string());
blacklisted_items.insert("escape pod".to_string());
blacklisted_items.insert("molten lava".to_string());
let mut carried_items = Vec::new();
let mut visited_rooms = HashSet::new();
let mut to_visit = VecDeque::new();
to_visit.push_back((initial_room, Vec::new()));
let mut directions_to_security_checkpoint = Vec::new();
let mut direction_to_pressure_sensitive_floor = Direction::North;
while let Some((from_room, directions_to_reach_here)) = to_visit.pop_front() {
for &direction in directions_to_reach_here.iter() {
execute_command(&mut program, Command::Move(direction))?;
}
for &direction in from_room.directions.iter() {
let new_room = execute_command(&mut program, Command::Move(direction))?;
if new_room.id == from_room.id {
direction_to_pressure_sensitive_floor = direction;
} else {
let mut new_directions = directions_to_reach_here.clone();
new_directions.push(direction);
if new_room.id == "== Security Checkpoint ==" {
directions_to_security_checkpoint = new_directions.clone();
}
let new_room_id = new_room.id.clone();
if visited_rooms.insert(new_room_id) {
for item in new_room
.items
.iter()
.filter(|&item| !blacklisted_items.contains(item))
{
execute_command(&mut program, Command::Take(item))?;
carried_items.push(item.clone());
}
to_visit.push_back((new_room, new_directions));
}
execute_command(&mut program, Command::Move(direction.reverse()))?;
}
}
for &direction in directions_to_reach_here.iter().rev() {
execute_command(&mut program, Command::Move(direction.reverse()))?;
}
}
for &direction in directions_to_security_checkpoint.iter() {
execute_command(&mut program, Command::Move(direction))?;
}
for item in carried_items.iter() {
execute_command(&mut program, Command::Drop(item))?;
}
let mut latest_gray_code = 0;
for i in 1..=(1 << carried_items.len()) {
let gray_code = i ^ (i >> 1);
for (j, item) in carried_items.iter().enumerate() {
let bit_mask = 1 << j;
if gray_code & bit_mask != 0 && latest_gray_code & bit_mask == 0 {
execute_command(&mut program, Command::Take(item))?;
} else if latest_gray_code & bit_mask != 0 && gray_code & bit_mask == 0 {
execute_command(&mut program, Command::Drop(item))?;
}
}
latest_gray_code = gray_code;
let new_room = execute_command(
&mut program,
Command::Move(direction_to_pressure_sensitive_floor),
)?;
if let Some(solution) = new_room.solution {
return Ok(solution);
}
}
Err("No solution found".to_string())
}
pub fn part2(_input_string: &str) -> Result<String, String> {
Ok(String::from(""))
}
#[test]
pub fn tests_part1() {
assert_eq!(part1(include_str!("day25_input.txt")), Ok(319_815_680));
assert_eq!(part1(include_str!("day25_input_2.txt")), Ok(2_424_308_736));
}