Skip to content

Commit

Permalink
2019: Port Intcode to support large numbers
Browse files Browse the repository at this point in the history
64-bit integers are needed to support large numbers because 32-bit is
not sufficient as the Intcode computer needs evolve.
  • Loading branch information
ericvw committed Apr 10, 2024
1 parent 3c3b7ac commit eb8c8bf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions 2019/src/bin/puzzle_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<i32> = 0..=99;
const TARGET_OUTPUT: i64 = 19690720;
const INPUT_RANGE: ops::RangeInclusive<i64> = 0..=99;

let (noun, verb) = INPUT_RANGE
.flat_map(|noun| INPUT_RANGE.map(move |verb| (noun, verb)))
Expand Down
2 changes: 1 addition & 1 deletion 2019/src/bin/puzzle_05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion 2019/src/bin/puzzle_07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
28 changes: 14 additions & 14 deletions 2019/src/intcode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::VecDeque;
use std::io;

pub fn parse_program(input: impl io::Read) -> Vec<i32> {
pub fn parse_program(input: impl io::Read) -> Vec<i64> {
io::read_to_string(input)
.unwrap()
.trim()
Expand All @@ -11,8 +11,8 @@ pub fn parse_program(input: impl io::Read) -> Vec<i32> {
}

enum Parameter {
Position(i32),
Immediate(i32),
Position(i64),
Immediate(i64),
}

enum Instruction {
Expand All @@ -29,26 +29,26 @@ enum Instruction {

pub enum State {
Halt,
Output(i32),
Output(i64),
}

pub struct Computer {
pub memory: Vec<i32>,
pub input: VecDeque<i32>,
pub memory: Vec<i64>,
pub input: VecDeque<i64>,
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(),
Expand All @@ -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
Expand Down Expand Up @@ -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!(),
Expand Down

0 comments on commit eb8c8bf

Please sign in to comment.