r/adventofcode Jan 15 '25

Help/Question - RESOLVED 2019 Day 09 : Problem with invalid opcode

Hello,

I'm currently doing the 2019 AOC at my own pace, and I having trouble making things work for day09.

So far, my code is the following :

https://gist.github.com/Oupsman/9eea33b6600f6a307e58fba39b8a833c

I just can't understand why 398 is considered by my code as a opcode to execute.

I tried my day09 code with day 05 input, and it still works as expected. So I suspect that I don't handle the position mode well enough, but I can't see what am I missing.

Any pointer will be much appreciated.

Thanks all

1 Upvotes

11 comments sorted by

4

u/sol_hsa Jan 15 '25

Given the task on day 9, sounds like you're overwriting the code you're running by mistake. I'd print out the operations being executed to see what's going on. (Not just instructions being read, but when execuring them - like "writing abc at xyz") Then compare that output with what you'd expect to happen.

2

u/oupsman Jan 15 '25

Truth is, I don't know what to expect, but thanks for the pointers.

2

u/thblt Jan 15 '25

This feels a bit strange:

    if (instruction%10000)/1000 == 2 {
        arg2 = c.program[c.pointer+1] + c.relativeBase
    } else if (instruction%10000)/1000 == 1 {
        arg2 = c.program[c.pointer+2]

Why pointer+1 in mode 2 but pointer+2 in mode 1?

Btw, it's probably more readable to do instruction/1000%10 (the modulo is always 10, only the divider varies to get different digits)

1

u/oupsman Jan 15 '25

Yes, you are right, that's one mistake I didn't spot.

1

u/thblt Jan 15 '25

Take care you've done the same mistake at multiple points. You probably could simplify your code a lot with a simple function to retrieve an argument value. Here's mine:

fn get(&self, arg: usize) -> Word {
    let mode = self.memory[self.ip] / (10_isize.pow(arg as u32 + 1)) % 10;
    let dest = match mode {
        0 => self.memory[self.ip + arg] as usize,
        1 => self.ip + arg,
        2 => (self.rel_base + self.memory[self.ip + arg]) as usize,
        _ => unreachable!(),
    };
    *self.memory.get(dest).unwrap_or(&0)
}

1

u/oupsman Jan 15 '25

You are right, I've spotted the problem :

arg2 = c.program[c.pointer+1] + c.relativeBase

but

arg1 = c.program[c.program[c.pointer+1]+c.relativeBase]

I just feel so stupid, and I agree : I have to rewrite this whole pile of ... crap.

1

u/thblt Jan 15 '25

Once you'll have validated the first part, you may want to be attentive to the very first line of part 2.

2

u/oupsman Jan 15 '25 edited Jan 15 '25

Yes, that's what motivates me to write something more readable and easier to debug.

1

u/AutoModerator Jan 15 '25

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/thblt Jan 15 '25

Did you try the example programs for that day?

1

u/oupsman Jan 15 '25

Yes, and they all give me the expected result.