From eb8c8bf9d388fe19534590df47986b325e1b2fe3 Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Tue, 9 Apr 2024 17:42:31 -0400 Subject: [PATCH] 2019: Port Intcode to support large numbers 64-bit integers are needed to support large numbers because 32-bit is not sufficient as the Intcode computer needs evolve. --- 2019/src/bin/puzzle_02.rs | 6 +++--- 2019/src/bin/puzzle_05.rs | 2 +- 2019/src/bin/puzzle_07.rs | 2 +- 2019/src/intcode.rs | 28 ++++++++++++++-------------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/2019/src/bin/puzzle_02.rs b/2019/src/bin/puzzle_02.rs index 61c405a..b8c4ce9 100644 --- a/2019/src/bin/puzzle_02.rs +++ b/2019/src/bin/puzzle_02.rs @@ -3,7 +3,7 @@ use std::ops; use aoc2019::intcode; -fn run_program(prog: &[i32], addr1_val: i32, addr2_val: i32) -> i32 { +fn run_program(prog: &[i64], addr1_val: i64, addr2_val: i64) -> i64 { let mut computer = intcode::Computer::new(prog, &[]); computer.memory[1] = addr1_val; @@ -19,8 +19,8 @@ fn main() { println!("Part 1: {}", run_program(&gravity_assist_program, 12, 2)); - const TARGET_OUTPUT: i32 = 19690720; - const INPUT_RANGE: ops::RangeInclusive = 0..=99; + const TARGET_OUTPUT: i64 = 19690720; + const INPUT_RANGE: ops::RangeInclusive = 0..=99; let (noun, verb) = INPUT_RANGE .flat_map(|noun| INPUT_RANGE.map(move |verb| (noun, verb))) diff --git a/2019/src/bin/puzzle_05.rs b/2019/src/bin/puzzle_05.rs index a7c7867..2dda3ff 100644 --- a/2019/src/bin/puzzle_05.rs +++ b/2019/src/bin/puzzle_05.rs @@ -2,7 +2,7 @@ use std::io; use aoc2019::intcode; -fn execute_diagnostic_program(prog: &[i32], input: &[i32]) -> i32 { +fn execute_diagnostic_program(prog: &[i64], input: &[i64]) -> i64 { let mut diagnostic_code = 0; let mut comp = intcode::Computer::new(prog, input); diff --git a/2019/src/bin/puzzle_07.rs b/2019/src/bin/puzzle_07.rs index c9d9f4e..db22b31 100644 --- a/2019/src/bin/puzzle_07.rs +++ b/2019/src/bin/puzzle_07.rs @@ -2,7 +2,7 @@ use std::io; use aoc2019::intcode; -fn run_amplifier_loop(program: &[i32], phase_settings: &[i32]) -> i32 { +fn run_amplifier_loop(program: &[i64], phase_settings: &[i64]) -> i64 { let mut amplifiers: Vec<_> = phase_settings .iter() .map(|&phase_setting| intcode::Computer::new(program, &[phase_setting])) diff --git a/2019/src/intcode.rs b/2019/src/intcode.rs index bc0aed6..847ac56 100644 --- a/2019/src/intcode.rs +++ b/2019/src/intcode.rs @@ -1,7 +1,7 @@ use std::collections::VecDeque; use std::io; -pub fn parse_program(input: impl io::Read) -> Vec { +pub fn parse_program(input: impl io::Read) -> Vec { io::read_to_string(input) .unwrap() .trim() @@ -11,8 +11,8 @@ pub fn parse_program(input: impl io::Read) -> Vec { } enum Parameter { - Position(i32), - Immediate(i32), + Position(i64), + Immediate(i64), } enum Instruction { @@ -29,26 +29,26 @@ enum Instruction { pub enum State { Halt, - Output(i32), + Output(i64), } pub struct Computer { - pub memory: Vec, - pub input: VecDeque, + pub memory: Vec, + pub input: VecDeque, ip: usize, - modes: i32, + modes: i64, } impl Computer { - fn opcode(inst: i32) -> i32 { + fn opcode(inst: i64) -> i64 { inst % 100 } - fn param_modes(inst: i32) -> i32 { + fn param_modes(inst: i64) -> i64 { inst / 100 } - pub fn new(program: &[i32], input: &[i32]) -> Self { + pub fn new(program: &[i64], input: &[i64]) -> Self { Self { memory: program.to_vec(), input: input.iter().copied().collect(), @@ -57,13 +57,13 @@ impl Computer { } } - fn read(&mut self) -> i32 { + fn read(&mut self) -> i64 { let val = self.memory[self.ip]; self.ip += 1; val } - fn next_param_mode(&mut self) -> i32 { + fn next_param_mode(&mut self) -> i64 { let val = self.modes % 10; self.modes /= 10; val @@ -112,14 +112,14 @@ impl Computer { } } - fn value(&self, param: Parameter) -> i32 { + fn value(&self, param: Parameter) -> i64 { match param { Parameter::Position(addr) => self.memory[usize::try_from(addr).unwrap()], Parameter::Immediate(val) => val, } } - fn write(&mut self, param: Parameter, val: i32) { + fn write(&mut self, param: Parameter, val: i64) { let dst = match param { Parameter::Position(addr) => usize::try_from(addr).unwrap(), Parameter::Immediate(_) => unreachable!(),