Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Created December 5, 2019 04:50
Show Gist options
  • Save Lucretiel/0ad088f6459d86d6fee1246c7148844c to your computer and use it in GitHub Desktop.
Save Lucretiel/0ad088f6459d86d6fee1246c7148844c to your computer and use it in GitHub Desktop.
A sample implementation of Advent of Code 2019 Day 2 in Rust
fn solve(input: &str) -> impl Display {
let init: Vec<Cell<usize>> = input
.trim()
.split(',')
.map(|value| value.parse::<usize>().unwrap().into())
.collect();
let mut memory = Vec::new();
for noun in 0..100 {
for verb in 0..100 {
memory.clone_from(&init);
memory[1].set(noun);
memory[2].set(verb);
for op in memory.chunks(4) {
match op[0].get() {
99 => break,
code => {
let lhs = memory[op[1].get()].get();
let rhs = memory[op[2].get()].get();
let output = match code {
1 => lhs + rhs,
2 => lhs * rhs,
_ => panic!("Invalid opcode: {}", code),
};
memory[op[3].get()].set(output);
}
}
}
if memory[0].get() == 19690720 {
return (100 * noun) + verb;
}
}
}
panic!("Couldn't find a solution")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment