Skip to content

Instantly share code, notes, and snippets.

@mbrenig
Created December 5, 2019 11:53
Show Gist options
  • Save mbrenig/45e739d374f5375a8128f97bfd93a7f0 to your computer and use it in GitHub Desktop.
Save mbrenig/45e739d374f5375a8128f97bfd93a7f0 to your computer and use it in GitHub Desktop.
fn apply_instruction(program: &mut Vec<u32>, ip: usize) {
let instruction = program[ip];
let verb = program[ip+1] as usize;
let noun = program[ip+2] as usize;
let target = program[ip+3] as usize;
program[target] = match instruction {
1 => program[verb] + program[noun],
2 => program[verb] * program[noun],
_ => unimplemented!("Unknown instruction: {}", instruction),
};
}
fn execute(mut program: Vec<u32>) -> Vec<u32> {
let mut instr_pointer = 0;
while program[instr_pointer] != 99 {
apply_instruction(&mut program, instr_pointer);
instr_pointer += 4;
}
program
}
fn main() {
assert_eq!(execute(vec![1,0,0,0,99]), vec![2,0,0,0,99]);
assert_eq!(execute(vec![2,3,0,3,99]), vec![2,3,0,6,99]);
assert_eq!(execute(vec![2,4,4,5,99,0]), vec![2,4,4,5,99,9801]);
assert_eq!(execute(vec![1,1,1,4,99,5,6,0,99]), vec![30,1,1,4,2,5,6,0,99]);
// Let's search everything!
for noun in 0..100 {
for verb in 0..100 {
let mut program = vec![1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,2,19,13,23,1,23,10,27,1,13,27,31,2,31,10,35,1,35,9,39,1,39,13,43,1,13,43,47,1,47,13,51,1,13,51,55,1,5,55,59,2,10,59,63,1,9,63,67,1,6,67,71,2,71,13,75,2,75,13,79,1,79,9,83,2,83,10,87,1,9,87,91,1,6,91,95,1,95,10,99,1,99,13,103,1,13,103,107,2,13,107,111,1,111,9,115,2,115,10,119,1,119,5,123,1,123,2,127,1,127,5,0,99,2,14,0,0];
program[1] = noun;
program[2] = verb;
program = execute(program);
if program[0] == 1969_07_20 {
println!("Found {} noun-verb: {}", program[0], noun*100 + verb);
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment