Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Here's my Rust code. I was lucky that I caught that bit at the very end of the instructions about setting the initial values. Otherwise I probably would have spiraled trying to figure out why my answer to Part 1 was wrong.
Now I just need to refactor this out to make it easy to extend for future days!
/// Day 2: 1202 Program Alarm/// /// Create an "Intcode" computer that can calculate a gravity slingshotusestd::fs;/// Parses the input. Expects a single line of integers separated by/// commasfnparse_input()->Vec<usize>{lettext:String=fs::read_to_string("data/day2.txt").unwrap();letcleaned=text.trim();cleaned.split(",").map(|c|c.parse().unwrap()).collect()}/// An Intcode Interpreter is a simple virtual machine that uses opcodes/// to modify its internal memorystructIntcodeInterpreter{ints:Vec<usize>,ip:usize,}implIntcodeInterpreter{pubfnnew(ints:Vec<usize>)->Self{Self{ints,ip:0}}/// Sets a memory address to a valuepubfnset(&mutself,position:usize,value:usize){self.ints[position]=value;}/// Reads from a memory addresspubfnget(&self,position:usize)->usize{self.ints[position]}/// Runs the program in memory until the stopcode (99) is reached/// (To be refactored and generalized)pubfnrun(&mutself){whileself.ints[self.ip]!=99{letopcode=self.ints[self.ip];letin1=self.ints[self.ip+1];letin2=self.ints[self.ip+2];letout=self.ints[self.ip+3];ifopcode==1{self.ints[out]=self.ints[in1]+self.ints[in2];}elseifopcode==2{self.ints[out]=self.ints[in1]*self.ints[in2];}else{panic!("Unrecognized opcode {}!",opcode);}self.ip=(self.ip+4)%self.ints.len();}}}/// Given a desired output, hunts through the possible values of position/// 1 and 2 (termed "noun" and "verb") by brute force until the output/// is found.fnfind_output(output:usize,ints:Vec<usize>)->(usize,usize){fornounin0..=99{forverbin0..=99{letmutinterpreter=IntcodeInterpreter::new(ints.clone());interpreter.set(1,noun);interpreter.set(2,verb);interpreter.run();ifinterpreter.get(0)==output{return(noun,verb);}}}panic!("Couldn't find output!");}pubfnrun(){letints=parse_input();println!("Part 1:");letmutinterpreter=IntcodeInterpreter::new(ints.clone());interpreter.set(1,12);interpreter.set(2,2);interpreter.run();println!("After running, position 0 is: {}",interpreter.get(0));println!("Part 2:");let(noun,verb)=find_output(19690720,ints.clone());println!("Noun: {}, Verb: {}",noun,verb);println!("Secret code is: {}{}",noun,verb);}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's my Rust code. I was lucky that I caught that bit at the very end of the instructions about setting the initial values. Otherwise I probably would have spiraled trying to figure out why my answer to Part 1 was wrong.
Now I just need to refactor this out to make it easy to extend for future days!