Skip to content

Commit

Permalink
2019: Port Day 2 Intcode program to use signed integers
Browse files Browse the repository at this point in the history
I misread the instructions that clearly state an Intcode program is a
list integers.
  • Loading branch information
ericvw committed Mar 4, 2024
1 parent 97767da commit 7e24aba
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions 2019/src/bin/puzzle_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum Opcode {
Halt = 99,
}

impl From<u32> for Opcode {
fn from(x: u32) -> Self {
impl From<i32> for Opcode {
fn from(x: i32) -> Self {
match x {
1 => Opcode::Add,
2 => Opcode::Multiply,
Expand All @@ -21,15 +21,15 @@ impl From<u32> for Opcode {

const INSTRUCTION_LEN: usize = 4;

fn extract_parameters(instruction: &[u32]) -> (usize, usize, usize) {
fn extract_parameters(instruction: &[i32]) -> (usize, usize, usize) {
(
usize::try_from(instruction[1]).unwrap(),
usize::try_from(instruction[2]).unwrap(),
usize::try_from(instruction[3]).unwrap(),
)
}

fn run_program(prog: &[u32], addr1_val: u32, addr2_val: u32) -> Vec<u32> {
fn run_program(prog: &[i32], addr1_val: i32, addr2_val: i32) -> Vec<i32> {
let mut prog = prog.to_vec();

prog[1] = addr1_val;
Expand All @@ -56,7 +56,7 @@ fn run_program(prog: &[u32], addr1_val: u32, addr2_val: u32) -> Vec<u32> {
}

fn main() {
let intcode_prog: Vec<u32> = io::read_to_string(io::stdin())
let intcode_prog: Vec<i32> = io::read_to_string(io::stdin())
.unwrap()
.trim()
.split(',')
Expand All @@ -65,8 +65,8 @@ fn main() {

println!("Part 1: {}", run_program(&intcode_prog, 12, 2)[0]);

const TARGET_OUTPUT: u32 = 19690720;
const INPUT_RANGE: ops::RangeInclusive<u32> = 0..=99;
const TARGET_OUTPUT: i32 = 19690720;
const INPUT_RANGE: ops::RangeInclusive<i32> = 0..=99;

let (noun, verb) = INPUT_RANGE
.flat_map(|noun| INPUT_RANGE.map(move |verb| (noun, verb)))
Expand Down

0 comments on commit 7e24aba

Please sign in to comment.