Skip to content

Commit f6612d2

Browse files
committed
Reuse intcode instances
1 parent 2e7b4df commit f6612d2

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
212212
| 4 | [Secure Container](https://adventofcode.com/2019/day/4) | [Source](src/year2019/day04.rs) | 7 |
213213
| 5 | [Sunny with a Chance of Asteroids](https://adventofcode.com/2019/day/5) | [Source](src/year2019/day05.rs) | 3 |
214214
| 6 | [Universal Orbit Map](https://adventofcode.com/2019/day/6) | [Source](src/year2019/day06.rs) | 28 |
215-
| 7 | [Amplification Circuit](https://adventofcode.com/2019/day/7) | [Source](src/year2019/day07.rs) | 487 |
215+
| 7 | [Amplification Circuit](https://adventofcode.com/2019/day/7) | [Source](src/year2019/day07.rs) | 82 |
216216
| 8 | [Space Image Format](https://adventofcode.com/2019/day/8) | [Source](src/year2019/day08.rs) | 5 |
217217
| 9 | [Sensor Boost](https://adventofcode.com/2019/day/9) | [Source](src/year2019/day09.rs) | 1021 |
218218
| 10 | [Monitoring Station](https://adventofcode.com/2019/day/10) | [Source](src/year2019/day10.rs) | 1001 |

Diff for: src/year2019/day07.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
use super::intcode::*;
88
use crate::util::parse::*;
99
use crate::util::slice::*;
10+
use std::array::from_fn;
1011

1112
pub fn parse(input: &str) -> Vec<i64> {
1213
input.iter_signed::<i64>().collect()
1314
}
1415

1516
pub fn part1(input: &[i64]) -> i64 {
1617
let mut result = 0;
18+
let mut computer = Computer::new(input);
1719

1820
let sequence = |slice: &[i64]| {
1921
let mut signal = 0;
2022

2123
// Send exactly 2 inputs and receive exactly 1 output per amplifier.
2224
for &phase in slice {
23-
let mut computer = Computer::new(input);
25+
computer.reset();
2426
computer.input(phase);
2527
computer.input(signal);
2628

@@ -39,9 +41,11 @@ pub fn part1(input: &[i64]) -> i64 {
3941

4042
pub fn part2(input: &[i64]) -> i64 {
4143
let mut result = 0;
44+
let mut computers: [Computer; 5] = from_fn(|_| Computer::new(input));
4245

4346
let feedback = |slice: &[i64]| {
44-
let mut computers: Vec<_> = (0..5).map(|_| Computer::new(input)).collect();
47+
// Reset state.
48+
computers.iter_mut().for_each(Computer::reset);
4549

4650
// Send each initial phase setting exactly once.
4751
for (computer, &phase) in computers.iter_mut().zip(slice.iter()) {

Diff for: src/year2019/intcode.rs

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ impl Computer {
3333
self.input.extend(ascii.bytes().map(|b| b as usize));
3434
}
3535

36+
/// Resets state *except* for memory which may have been modified.
37+
pub fn reset(&mut self) {
38+
self.pc = 0;
39+
self.base = 0;
40+
self.input.clear();
41+
}
42+
3643
/// Runs until either the program needs input, outputs a value or encounters the halt opcode.
3744
/// In the first two cases, the computer can be resumed by calling `run` again.
3845
pub fn run(&mut self) -> State {

Diff for: tests/year2019/day07_test.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
use aoc::year2019::day07::*;
2-
3-
const FIRST_EXAMPLE: &str = "\
4-
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0";
5-
6-
const SECOND_EXAMPLE: &str = "\
7-
3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,
8-
27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5";
9-
101
#[test]
112
fn part1_test() {
12-
let input = parse(FIRST_EXAMPLE);
13-
assert_eq!(part1(&input), 43210);
3+
// No example data
144
}
155

166
#[test]
177
fn part2_test() {
18-
let input = parse(SECOND_EXAMPLE);
19-
assert_eq!(part2(&input), 139629729);
8+
// No example data
209
}

0 commit comments

Comments
 (0)