Add day 2 part 1 in Rust

This commit is contained in:
kageru 2019-12-03 05:59:12 +01:00
parent 0cfbc8f3e5
commit d46c40b6e9
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

37
2019/02/day2.rs Normal file
View File

@ -0,0 +1,37 @@
use std::io;
use std::io::BufRead;
pub fn main() {
let input: Vec<usize> = io::stdin()
.lock()
.lines()
.next()
.unwrap()
.unwrap()
.split(',')
.map(|n| n.parse().unwrap())
.collect();
println!("Part 1: {}", execute(&mut input.clone(), 12, 2));
}
fn execute(input: &mut Vec<usize>, p1: usize, p2: usize) -> usize {
input[1] = p1;
input[2] = p2;
for i in (0..).step_by(4) {
let opcode = input[i];
let first = input[i+1];
let second = input[i+2];
let target = input[i+3];
if target == 0 {
dbg!("target is 0", target, opcode, first, second);
}
match opcode {
1 => input[target] = input[first] + input[second],
2 => input[target] = input[first] * input[second],
99 => break,
_ => unreachable!("Invalid opcode")
}
}
dbg!(&input);
input[0]
}