1use 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
37fn 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
64fn 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 explore(&mut computer, &mut stack, &mut path, &mut inventory);
96
97 let last = path.pop().unwrap();
99
100 for direction in path {
101 movement_silent(&mut computer, &direction);
102 }
103
104 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 let want = (combinations - 1) ^ gray_code(i);
120
121 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 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
236fn drain_output(computer: &mut Computer) {
239 while let State::Output(_) = computer.run() {}
240}
241
242fn gray_code(n: u32) -> u32 {
244 n ^ (n >> 1)
245}