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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! An intcode intepreter written in Rust!
//!
//! Intcode is a machine language specified in several challenges from
//! [Advent of Code 2019](https://adventofcode.com/2019). This library provides a full
//! implementation of the language that is compatible with every Intcode-based challenge.

use std::convert::TryInto;
use std::fmt;
use std::thread::{self, JoinHandle};

/// An instance of an Intcode virtual machine.
#[derive(Debug)]
pub struct Machine {
    mem: Vec<isize>,
    ip: isize,
    base: isize,
}

impl Machine {
    /// Constructs a new machine with the initial state:
    ///
    /// - empty `mem`;
    /// - `ip` pointing to zero;
    /// - `base` set at zero.
    pub fn new() -> Machine {
        Machine {
            mem: Vec::new(),
            ip: 0,
            base: 0,
        }
    }

    pub fn with_program(program: &[isize]) -> Machine {
        let mut machine = Machine::new();
        machine.copy(0, program);
        machine
    }

    /// An immutable view into the virtual machine's memory.
    pub fn mem(&self) -> &[isize] {
        &self.mem
    }

    /// A mutable view into the virtual machine's memory.
    pub fn mem_mut(&mut self) -> &mut [isize] {
        &mut self.mem
    }

    /// Ensures that memory locations `0..size` are valid, extending memory with zeroes if needed.
    pub fn reserve(&mut self, size: usize) {
        if self.mem.len() < size {
            self.mem.resize(size, 0);
        }
    }

    /// Copies the given slice into memory at the given location.
    pub fn copy(&mut self, start: usize, values: &[isize]) {
        self.reserve(start + values.len());
        self.mem[start..start + values.len()].copy_from_slice(values);
    }

    fn read(&mut self, i: isize) -> Result<isize, Exit> {
        let i: usize = i.try_into().map_err(|_| Exit::NegativePointer)?;

        self.reserve(i + 1);
        Ok(self.mem[i])
    }

    fn write(&mut self, i: isize, val: isize) -> Result<(), Exit> {
        let i: usize = i.try_into().map_err(|_| Exit::NegativePointer)?;

        self.reserve(i + 1);
        self.mem[i] = val;
        Ok(())
    }

    fn param(&mut self, offset: isize) -> Result<isize, Exit> {
        let opcode = self.read(self.ip)?;
        let arg = self.read(self.ip + offset)?;
        let mode = (opcode / 10isize.pow(offset as u32 + 1)) % 10;

        match mode {
            // absolute pointer
            0 => self.read(arg),

            // immediate
            1 => Ok(arg),

            // relative pointer
            2 => self.read(self.base + arg),

            unknown => Err(Exit::IllegalMode(unknown)),
        }
    }

    fn out(&mut self, offset: isize, val: isize) -> Result<(), Exit> {
        let opcode = self.read(self.ip)?;
        let arg = self.read(self.ip + offset)?;
        let mode = (opcode / 10isize.pow(offset as u32 + 1)) % 10;

        match mode {
            // absolute pointer
            0 => self.write(arg, val),

            // relative pointer
            2 => self.write(self.base + arg, val),

            unknown => Err(Exit::IllegalMode(unknown)),
        }
    }

    /// Executes a single instruction.
    pub fn step(&mut self, input: Option<isize>) -> Result<(), Exit> {
        let opcode = self.read(self.ip)?;
        let instruction = opcode % 100;

        match instruction {
            // add
            1 => {
                let a = self.param(1)?;
                let b = self.param(2)?;
                self.out(3, a + b)?;
                self.ip += 4;
                Ok(())
            }

            // mul
            2 => {
                let a = self.param(1)?;
                let b = self.param(2)?;
                self.out(3, a * b)?;
                self.ip += 4;
                Ok(())
            }

            // in
            3 => {
                let a = input.ok_or(Exit::Input)?;
                self.out(1, a)?;
                self.ip += 2;
                Ok(())
            }

            // out
            4 => {
                let a = self.param(1)?;
                self.ip += 2;
                Err(Exit::Output(a))
            }

            // jt
            5 => {
                let a = self.param(1)?;
                let b = self.param(2)?;
                if a != 0 {
                    self.ip = b;
                } else {
                    self.ip += 3;
                }
                Ok(())
            }

            // jf
            6 => {
                let a = self.param(1)?;
                let b = self.param(2)?;
                if a == 0 {
                    self.ip = b;
                } else {
                    self.ip += 3;
                }
                Ok(())
            }

            // lt
            7 => {
                let a = self.param(1)?;
                let b = self.param(2)?;
                self.out(3, if a < b { 1 } else { 0 })?;
                self.ip += 4;
                Ok(())
            }

            // eq
            8 => {
                let a = self.param(1)?;
                let b = self.param(2)?;
                self.out(3, if a == b { 1 } else { 0 })?;
                self.ip += 4;
                Ok(())
            }

            // arb
            9 => {
                let a = self.param(1)?;
                self.base += a;
                self.ip += 2;
                Ok(())
            }

            // halt
            99 => Err(Exit::Halted),

            unknown => Err(Exit::IllegalInstruction(unknown)),
        }
    }

    /// Runs the program until the first error is encountered.
    pub fn resume(&mut self, mut input: Option<isize>) -> Exit {
        loop {
            match self.step(input.take()) {
                Ok(_) => {}
                Err(e) => return e,
            }
        }
    }

    /// Runs the program using the given I/O interfaces.
    pub fn run<I, O>(&mut self, input: I, output: O) -> Exit
    where
        I: IntoIterator<Item = isize>,
        O: FnMut(isize),
    {
        let mut input = input.into_iter().peekable();
        let mut output = output;
        let mut next_input = None;
        loop {
            match self.resume(next_input.take()) {
                Exit::Input if input.peek().is_some() => {
                    next_input = input.next();
                }
                Exit::Output(a) => {
                    output(a);
                }
                other => return other,
            }
        }
    }

    pub fn spawn<I, O>(mut self, input: I, output: O) -> JoinHandle<(Machine, Exit)>
    where
        I: IntoIterator<Item = isize> + Send + 'static,
        O: FnMut(isize) + Send + 'static,
    {
        thread::spawn(move || {
            let result = self.run(input, output);
            (self, result)
        })
    }
}

/// Errors that can occur during execution.
#[derive(Debug, PartialEq)]
pub enum Exit {
    /// Attempted to use a negative value as a pointer.
    NegativePointer,

    /// Encountered an unknown parameter mode.
    IllegalMode(isize),

    /// Encountered an unknown instruction.
    IllegalInstruction(isize),

    /// The program encountered an `in` instruction and needs to take input.
    Input,

    /// The program encountered an `out` instruction and needs to return output.
    Output(isize),

    /// Encountered a halt instruction (99).
    Halted,
}

impl fmt::Display for Exit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Exit::NegativePointer => write!(f, "attempted to use a negative value as a pointer"),
            Exit::IllegalMode(mode) => write!(f, "illegal mode: {}", mode),
            Exit::IllegalInstruction(inst) => write!(f, "illegal instruction: {}", inst),
            Exit::Input => write!(f, "need input"),
            Exit::Output(a) => write!(f, "got output: {}", a),
            Exit::Halted => write!(f, "halted"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn run(program: &[isize], input: &[isize]) -> (Machine, Exit, Vec<isize>) {
        let mut machine = Machine::with_program(program);
        let mut output = Vec::new();

        let exit = machine.run(input.iter().copied(), |a| output.push(a));
        (machine, exit, output)
    }

    #[test]
    fn day2a() {
        let (machine, exit, _output) = run(&[1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50], &[]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(machine.mem()[0], 3500);
    }

    #[test]
    fn day5a_io() {
        let (_machine, exit, output) = run(&[3, 0, 4, 0, 99], &[95243]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, &[95243]);
    }

    #[test]
    fn day5a_mode() {
        let (_machine, exit, _output) = run(&[1002, 4, 3, 4, 33], &[]);
        assert_eq!(exit, Exit::Halted);
    }

    const DAY5B_CMP: &[isize] = &[
        3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0,
        1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20,
        1105, 1, 46, 98, 99,
    ];

    #[test]
    fn day5b_cmp_lt() {
        let (_machine, exit, output) = run(DAY5B_CMP, &[5]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, &[999]);
    }

    #[test]
    fn day5b_cmp_gt() {
        let (_machine, exit, output) = run(DAY5B_CMP, &[9]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, &[1001]);
    }

    #[test]
    fn day5b_cmp_eq() {
        let (_machine, exit, output) = run(DAY5B_CMP, &[8]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, &[1000]);
    }

    fn day7a(program: &[isize]) -> isize {
        let mut result = isize::min_value();
        for a in 0..5 {
            let a_out = run(program, &[a, 0]).2[0];

            for b in 0..5 {
                if b == a {
                    continue;
                }
                let b_out = run(program, &[b, a_out]).2[0];

                for c in 0..5 {
                    if c == a || c == b {
                        continue;
                    }
                    let c_out = run(program, &[c, b_out]).2[0];

                    for d in 0..5 {
                        if d == a || d == b || d == c {
                            continue;
                        }
                        let d_out = run(program, &[d, c_out]).2[0];

                        for e in 0..5 {
                            if e == a || e == b || e == c || e == d {
                                continue;
                            }
                            let e_out = run(program, &[e, d_out]).2[0];

                            result = result.max(e_out);
                        }
                    }
                }
            }
        }

        result
    }

    #[test]
    fn day7a_1() {
        const DAY7A_1: &[isize] = &[
            3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0,
        ];

        assert_eq!(day7a(DAY7A_1), 43210);
    }

    #[test]
    fn day7a_2() {
        const DAY7A_2: &[isize] = &[
            3, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1, 24, 23, 23, 4, 23, 99,
            0, 0,
        ];

        assert_eq!(day7a(DAY7A_2), 54321);
    }

    #[test]
    fn day7a_3() {
        const DAY7A_3: &[isize] = &[
            3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33, 1002, 33, 7, 33, 1, 33,
            31, 31, 1, 32, 31, 31, 4, 31, 99, 0, 0, 0,
        ];

        assert_eq!(day7a(DAY7A_3), 65210);
    }

    fn day7b(program: &[isize]) -> isize {
        use std::sync::mpsc;

        let mut result = isize::min_value();

        for a in 5..10 {
            for b in 5..10 {
                if b == a {
                    continue;
                }
                for c in 5..10 {
                    if c == a || c == b {
                        continue;
                    }
                    for d in 5..10 {
                        if d == a || d == b || d == c {
                            continue;
                        }
                        for e in 5..10 {
                            if e == a || e == b || e == c || e == d {
                                continue;
                            }

                            let (main_tx, a_rx) = mpsc::channel();
                            let (a_tx, b_rx) = mpsc::channel();
                            let (b_tx, c_rx) = mpsc::channel();
                            let (c_tx, d_rx) = mpsc::channel();
                            let (d_tx, e_rx) = mpsc::channel();
                            let (e_tx, main_rx) = mpsc::channel();

                            main_tx.send(a).unwrap();
                            a_tx.send(b).unwrap();
                            b_tx.send(c).unwrap();
                            c_tx.send(d).unwrap();
                            d_tx.send(e).unwrap();
                            main_tx.send(0).unwrap();

                            Machine::with_program(program)
                                .spawn(a_rx, move |x| a_tx.send(x).unwrap());
                            Machine::with_program(program)
                                .spawn(b_rx, move |x| b_tx.send(x).unwrap());
                            Machine::with_program(program)
                                .spawn(c_rx, move |x| c_tx.send(x).unwrap());
                            Machine::with_program(program)
                                .spawn(d_rx, move |x| d_tx.send(x).unwrap());
                            Machine::with_program(program)
                                .spawn(e_rx, move |x| e_tx.send(x).unwrap());

                            let mut last = 0;
                            for x in main_rx {
                                last = x;
                                let _ = main_tx.send(x);
                            }
                            result = result.max(last);
                        }
                    }
                }
            }
        }

        result
    }

    #[test]
    fn day7b_1() {
        const DAY7B_1: &[isize] = &[
            3, 26, 1001, 26, -4, 26, 3, 27, 1002, 27, 2, 27, 1, 27, 26, 27, 4, 27, 1001, 28, -1, 28,
            1005, 28, 6, 99, 0, 0, 5,
        ];

        assert_eq!(day7b(DAY7B_1), 139629729);
    }

    #[test]
    fn day7b_2() {
        const DAY7B_2: &[isize] = &[
            3, 52, 1001, 52, -5, 52, 3, 53, 1, 52, 56, 54, 1007, 54, 5, 55, 1005, 55, 26, 1001, 54, -5,
            54, 1105, 1, 12, 1, 53, 54, 53, 1008, 54, 0, 55, 1001, 55, 1, 55, 2, 53, 55, 53, 4, 53,
            1001, 56, -1, 56, 1005, 56, 6, 99, 0, 0, 0, 0, 10,
        ];

        assert_eq!(day7b(DAY7B_2), 18216);
    }

    #[test]
    fn day9_quine() {
        const PROGRAM: &[isize] = &[109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99];

        let (_machine, exit, output) = run(PROGRAM, &[]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, PROGRAM);
    }

    #[test]
    fn day9_overflow_check() {
        let (_machine, exit, output) = run(&[1102,34915192,34915192,7,4,7,99,0], &[]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, &[1219070632396864]);
    }

    #[test]
    fn day9_read_check() {
        let (_machine, exit, output) = run(&[104,1125899906842624,99], &[]);
        assert_eq!(exit, Exit::Halted);
        assert_eq!(output, &[1125899906842624]);
    }
}