|
| 1 | +//! [Day 25: Cryostasis](https://adventofcode.com/2019/day/25) |
| 2 | +
|
| 3 | +use std::collections::{HashMap, HashSet}; |
| 4 | + |
| 5 | +use intcode::{Computer, State}; |
| 6 | + |
| 7 | +type Room = String; |
| 8 | +type Direction = String; |
| 9 | +type Map = HashMap<Room, HashMap<Direction, Room>>; |
| 10 | + |
| 11 | +fn run(computer: &mut Computer, command: &str) -> String { |
| 12 | + computer.input_flush(); |
| 13 | + |
| 14 | + if !command.is_empty() { |
| 15 | + command.bytes().for_each(|byte| computer.push_byte(byte)); |
| 16 | + computer.push_byte(b'\n'); |
| 17 | + } |
| 18 | + |
| 19 | + let mut output = String::new(); |
| 20 | + for _ in 1..10000 { |
| 21 | + match computer.run() { |
| 22 | + State::Output(ch) => { |
| 23 | + output.push(char::from_u32(u32::try_from(ch).unwrap()).unwrap()); |
| 24 | + } |
| 25 | + State::Halted | State::Input => return output, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + String::new() |
| 30 | +} |
| 31 | + |
| 32 | +fn parse(output: &str) -> (&str, Vec<&str>, Vec<&str>) { |
| 33 | + let mut dirs = Vec::new(); |
| 34 | + let mut items = Vec::new(); |
| 35 | + let mut room = ""; |
| 36 | + |
| 37 | + for line in output.lines() { |
| 38 | + if let Some(s) = line.strip_prefix("== ") { |
| 39 | + if let Some(s) = s.strip_suffix(" ==") { |
| 40 | + if room.is_empty() { |
| 41 | + room = s; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if let Some(s) = line.strip_prefix("- ") { |
| 47 | + match s { |
| 48 | + "north" | "east" | "south" | "west" => dirs.push(s), |
| 49 | + _ => items.push(s), |
| 50 | + }; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + (room, dirs, items) |
| 55 | +} |
| 56 | + |
| 57 | +fn reverse(direction: &str) -> &'static str { |
| 58 | + match direction { |
| 59 | + "north" => "south", |
| 60 | + "south" => "north", |
| 61 | + "east" => "west", |
| 62 | + "west" => "east", |
| 63 | + _ => "none", |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn explore(computer: &mut Computer, map: &mut Map, output: &str) { |
| 68 | + // |
| 69 | + |
| 70 | + let (room, dirs, items) = parse(output); |
| 71 | + |
| 72 | + for dir in &dirs { |
| 73 | + if map.contains_key(room) && map[room].contains_key(*dir) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + // go next room |
| 78 | + let output = run(computer, dir); |
| 79 | + |
| 80 | + let (newroom, _, _) = parse(&output); |
| 81 | + let known = map.contains_key(newroom); |
| 82 | + |
| 83 | + map.entry(room.to_string()) |
| 84 | + .or_default() |
| 85 | + .insert((*dir).to_string(), newroom.to_string()); |
| 86 | + map.entry(newroom.to_string()) |
| 87 | + .or_default() |
| 88 | + .insert(reverse(dir).to_string(), room.to_string()); |
| 89 | + |
| 90 | + if output.contains("A loud") { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if !known { |
| 95 | + explore(computer, map, &output); |
| 96 | + } |
| 97 | + |
| 98 | + run(computer, reverse(dir)); |
| 99 | + } |
| 100 | + |
| 101 | + for item in items { |
| 102 | + let mut temp_computer = computer.clone(); |
| 103 | + |
| 104 | + let command = format!("take {item}"); |
| 105 | + |
| 106 | + let output = run(&mut temp_computer, &command); |
| 107 | + if output.is_empty() { |
| 108 | + // stuck |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + let output = run(&mut temp_computer, "inv"); |
| 113 | + if output.contains("Items in your inventory") { |
| 114 | + run(computer, &command); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +fn find_path<'a>(map: &'a Map, start: &str, target: &str) -> Vec<&'a str> { |
| 120 | + let mut seen = HashSet::new(); |
| 121 | + |
| 122 | + let mut stack = Vec::new(); |
| 123 | + |
| 124 | + stack.push((start, Vec::new())); |
| 125 | + |
| 126 | + while let Some((current, path)) = stack.pop() { |
| 127 | + if current == target { |
| 128 | + return path; |
| 129 | + } |
| 130 | + |
| 131 | + for (dir, cell) in &map[current] { |
| 132 | + if seen.insert(cell) { |
| 133 | + let mut new_path = path.clone(); |
| 134 | + new_path.push(dir); |
| 135 | + stack.push((cell, new_path)); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + Vec::new() |
| 141 | +} |
| 142 | + |
| 143 | +fn find_weight(computer: &mut Computer, inventory: &[&str], checkpoint_dir: &str) -> u64 { |
| 144 | + let re = regex::Regex::new( |
| 145 | + r"You should be able to get in by typing (\d+) on the keypad at the main airlock.", |
| 146 | + ) |
| 147 | + .unwrap(); |
| 148 | + |
| 149 | + let mut prev_code = 0i32; |
| 150 | + |
| 151 | + for code in 0..(1 << inventory.len()) { |
| 152 | + let gray_code = code ^ (code >> 1); |
| 153 | + let diff = gray_code - prev_code; |
| 154 | + |
| 155 | + if diff != 0 { |
| 156 | + let action = if diff > 0 { "drop" } else { "take" }; |
| 157 | + |
| 158 | + let mut diff = diff.abs(); |
| 159 | + let mut item = 0; |
| 160 | + while diff & 1 == 0 { |
| 161 | + diff >>= 1; |
| 162 | + item += 1; |
| 163 | + } |
| 164 | + |
| 165 | + let command = format!("{action} {}", inventory[item]); |
| 166 | + run(computer, &command); |
| 167 | + } |
| 168 | + |
| 169 | + let output = run(computer, checkpoint_dir); |
| 170 | + |
| 171 | + if let Some(caps) = re.captures(&output) { |
| 172 | + return caps[1].parse().unwrap(); |
| 173 | + } |
| 174 | + |
| 175 | + prev_code = gray_code; |
| 176 | + } |
| 177 | + |
| 178 | + 0 |
| 179 | +} |
| 180 | + |
| 181 | +fn solve(program: &str) -> u64 { |
| 182 | + let mut computer = Computer::load(program); |
| 183 | + |
| 184 | + let mut map = HashMap::new(); |
| 185 | + |
| 186 | + let output = run(&mut computer, ""); |
| 187 | + let (start_room, _, _) = parse(&output); |
| 188 | + |
| 189 | + // visit all rooms and collect items |
| 190 | + explore(&mut computer, &mut map, &output); |
| 191 | + |
| 192 | + // find path to the Pressure-Sensitive Floor |
| 193 | + let path = find_path(&map, start_room, "Pressure-Sensitive Floor"); |
| 194 | + |
| 195 | + // the direction to go to Security Checkpoint from Pressure-Sensitive Floor |
| 196 | + let checkpoint_dir = map["Security Checkpoint"] |
| 197 | + .iter() |
| 198 | + .filter(|(_, room)| room == &"Pressure-Sensitive Floor") |
| 199 | + .map(|(dir, _)| dir) |
| 200 | + .next() |
| 201 | + .unwrap(); |
| 202 | + |
| 203 | + // go to the Pressure-Sensitive Floor |
| 204 | + for step in path { |
| 205 | + run(&mut computer, step); |
| 206 | + } |
| 207 | + |
| 208 | + // the inventory |
| 209 | + let inventory = run(&mut computer, "inv"); |
| 210 | + let inventory = inventory |
| 211 | + .lines() |
| 212 | + .filter_map(|line| line.strip_prefix("- ")) |
| 213 | + .collect::<Vec<&str>>(); |
| 214 | + |
| 215 | + // get the unlock code |
| 216 | + find_weight(&mut computer, &inventory, checkpoint_dir) |
| 217 | +} |
| 218 | + |
| 219 | +fn main() { |
| 220 | + let args = aoc::parse_args(); |
| 221 | + |
| 222 | + println!("{}", solve(&args.input)); |
| 223 | +} |
0 commit comments