Skip to main content

aoc/year2019/
day25.rs

1//! # Cryostasis
2//!
3//! Plays the game automatically, solving the weight puzzle using
4//! [Gray codes](https://en.wikipedia.org/wiki/Gray_code) to check every combination of items
5//! by swapping only one item at a time.
6//!
7//! Makes some assumptions:
8//! * The ship's layout contains no loops, so a depth-first search will explore every room then
9//!   return to the starting point.
10//! * The 5 dangerous items are common across all inputs.
11//! * No items are called "north", "south", "west" or "east".
12//! * The final room is called "Pressure-Sensitive Floor".
13//!
14//! If these assumptions hold then this solution will solve any arbitrary combination of ship
15//! layout and items.
16//!
17//! Just for fun this solution can be played interactively on the command line if
18//! "--features frivolity" is enabled.
19use std::fmt::Write as _;
20
21use crate::util::bitset::*;
22use crate::util::intcode::*;
23use crate::util::parse::*;
24
25pub fn parse(input: &str) -> Vec<i64> {
26    input.iter_signed().collect()
27}
28
29pub fn part1(input: &[i64]) -> String {
30    if cfg!(feature = "frivolity") { play_manually(input) } else { play_automatically(input) }
31}
32
33pub fn part2(_input: &[i64]) -> &'static str {
34    "n/a"
35}
36
37// Let a human play the game interactively.
38fn play_manually(input: &[i64]) -> String {
39    use std::io::stdin;
40
41    let mut computer = Computer::new(input);
42    let mut output = String::new();
43    let mut input = String::new();
44
45    loop {
46        match computer.run() {
47            State::Output(value) => output.push(char::from(value as u8)),
48            State::Input => {
49                pretty_print(&output);
50                output.clear();
51                let _unused = stdin().read_line(&mut input);
52                computer.input_ascii(&input);
53                input.clear();
54            }
55            State::Halted => {
56                pretty_print(&output);
57                output.retain(|c| c.is_ascii_digit());
58                break output;
59            }
60        }
61    }
62}
63
64// Use ANSI codes to colorize the output to highlight the text.
65fn pretty_print(output: &str) {
66    use crate::util::ansi::*;
67
68    let mut buffer = String::new();
69    let mut item = GREEN;
70
71    for line in output.lines() {
72        if line.starts_with('=') {
73            write!(buffer, "{BOLD}{WHITE}{line}{RESET}").unwrap();
74        } else if line.starts_with('-') {
75            write!(buffer, "{item}{line}{RESET}").unwrap();
76        } else if line.starts_with("Items here:") {
77            item = YELLOW;
78            buffer.push_str(line);
79        } else {
80            buffer.push_str(line);
81        }
82        buffer.push('\n');
83    }
84
85    println!("{buffer}");
86}
87
88fn play_automatically(input: &[i64]) -> String {
89    let mut computer = Computer::new(input);
90    let mut stack = Vec::new();
91    let mut path = Vec::new();
92    let mut inventory = Vec::new();
93
94    // DFS through the ship, picking up all 8 safe items, then return to the starting point.
95    explore(&mut computer, &mut stack, &mut path, &mut inventory);
96
97    // Retrace our path back to the Security Checkpoint.
98    let last = path.pop().unwrap();
99
100    for direction in path {
101        movement_silent(&mut computer, &direction);
102    }
103
104    // Use Gray codes to take or drop one item at a time, until we are exactly the right weight.
105    // As an optimization we keep track of combinations of items that are too heavy or too light.
106    // If we are adding an item to a collection that is already too heavy or vice-versa,
107    // then we can skip the pressure plate check.
108    let combinations = 1 << inventory.len();
109    let mut have = combinations - 1;
110    let mut output = String::new();
111    let mut too_light = Vec::with_capacity(combinations as usize);
112    let mut too_heavy = Vec::with_capacity(combinations as usize);
113
114    for i in 1..combinations {
115        // Since we start with all items in our possession, the meaning of bits in the Gray code is
116        // reversed. 0 is take an item and 1 is drop an item. Iterating over too_heavy and
117        // too_light is still cheaper than the cost to emulate another take or drop, so it is worth
118        // seeing if we can skip altering inventory to a given configuration.
119        let want = (combinations - 1) ^ gray_code(i);
120
121        // Skip supersets of known heavy configurations and subsets of known light ones.
122        if too_heavy.iter().any(|&heavy| want & heavy == heavy)
123            || too_light.iter().any(|&light| want & light == want)
124        {
125            continue;
126        }
127
128        sync_items(&mut computer, have, want, &inventory);
129        have = want;
130
131        if matches!(movement_noisy(&mut computer, &last, &mut output), State::Halted) {
132            // Keep only the password digits from Santa's response.
133            output.retain(|b| b.is_ascii_digit());
134            break;
135        } else if output.contains("heavier") {
136            too_light.push(want);
137        } else {
138            too_heavy.push(want);
139        }
140
141        output.clear();
142    }
143
144    output
145}
146
147fn explore(
148    computer: &mut Computer,
149    stack: &mut Vec<String>,
150    path: &mut Vec<String>,
151    inventory: &mut Vec<String>,
152) {
153    let direction = stack.last().map_or("none", String::as_str);
154    let reverse = opposite(direction);
155
156    let mut output = String::new();
157    movement_noisy(computer, direction, &mut output);
158
159    for line in output.lines() {
160        if line.starts_with("== Pressure-Sensitive Floor ==") {
161            path.clone_from(stack);
162            return;
163        } else if let Some(suffix) = line.strip_prefix("- ") {
164            if opposite(suffix) == "none" {
165                if !dangerous(suffix) {
166                    take_item(computer, suffix);
167                    inventory.push(suffix.to_owned());
168                }
169            } else if suffix != reverse {
170                stack.push(suffix.to_owned());
171                explore(computer, stack, path, inventory);
172                stack.pop();
173            }
174        }
175    }
176
177    movement_silent(computer, reverse);
178}
179
180fn opposite(direction: &str) -> &'static str {
181    match direction {
182        "north" => "south",
183        "south" => "north",
184        "east" => "west",
185        "west" => "east",
186        _ => "none",
187    }
188}
189
190fn dangerous(item: &str) -> bool {
191    matches!(
192        item,
193        "escape pod" | "giant electromagnet" | "infinite loop" | "molten lava" | "photons"
194    )
195}
196
197fn movement_noisy(computer: &mut Computer, direction: &str, output: &mut String) -> State {
198    if direction != "none" {
199        computer.input_ascii(&format!("{direction}\n"));
200    }
201    loop {
202        match computer.run() {
203            State::Output(value) => output.push(char::from(value as u8)),
204            other => break other,
205        }
206    }
207}
208
209fn movement_silent(computer: &mut Computer, direction: &str) {
210    if direction != "none" {
211        computer.input_ascii(&format!("{direction}\n"));
212        drain_output(computer);
213    }
214}
215
216fn take_item(computer: &mut Computer, item: &str) {
217    computer.input_ascii(&format!("take {item}\n"));
218    drain_output(computer);
219}
220
221fn drop_item(computer: &mut Computer, item: &str) {
222    computer.input_ascii(&format!("drop {item}\n"));
223    drain_output(computer);
224}
225
226fn sync_items(computer: &mut Computer, have: u32, want: u32, inventory: &[String]) {
227    for i in (want ^ have).biterator() {
228        if have & (1 << i) != 0 {
229            drop_item(computer, &inventory[i]);
230        } else {
231            take_item(computer, &inventory[i]);
232        }
233    }
234}
235
236// A quirk of the intcode program is that commands can't be stacked. We must first read all the
237// output from the previous command before the next command can be submitted.
238fn drain_output(computer: &mut Computer) {
239    while let State::Output(_) = computer.run() {}
240}
241
242/// Convert a normal binary number to its Gray Code equivalent.
243fn gray_code(n: u32) -> u32 {
244    n ^ (n >> 1)
245}