Remove to_vec from day2

More refactoring soon.
This commit is contained in:
Vladan Popovic 2020-01-15 23:57:01 +01:00
parent 4d60fb3e23
commit cc904a4ea1

View file

@ -1,6 +1,6 @@
use std; use std;
fn compute_intcode(p: &mut Vec<usize>) -> Vec<usize> { fn compute_intcode(p: &mut Vec<usize>) {
for idx in (0..(p.len())).step_by(4) { for idx in (0..(p.len())).step_by(4) {
match p[idx] { match p[idx] {
1 => { 1 => {
@ -16,10 +16,9 @@ fn compute_intcode(p: &mut Vec<usize>) -> Vec<usize> {
p[res] = p[left] * p[right]; p[res] = p[left] * p[right];
} }
99 => break, 99 => break,
_ => panic!("Something went wrong!"), _ => panic!("invalid opcode"),
} }
} }
p.to_vec()
} }
pub fn part_one() { pub fn part_one() {
@ -32,7 +31,9 @@ pub fn part_one() {
input[1] = 12; input[1] = 12;
input[2] = 2; input[2] = 2;
println!("Intcode [0] is: {}", compute_intcode(&mut input)[0]); compute_intcode(&mut input);
println!("Intcode [0] is: {}", input[0]);
} }
pub fn part_two() { pub fn part_two() {
@ -52,8 +53,8 @@ pub fn part_two() {
input[1] = i; input[1] = i;
for j in 0..99 { for j in 0..99 {
input[2] = j; input[2] = j;
let mut input_guess = input.clone(); let mut computed = input.clone();
let computed = compute_intcode(&mut input_guess); compute_intcode(&mut computed);
found = computed[0] == lookup_num; found = computed[0] == lookup_num;
@ -76,20 +77,19 @@ pub fn part_two() {
#[test] #[test]
fn test_part_one() { fn test_part_one() {
assert_eq!( let mut intcode = vec![1, 0, 0, 0, 99];
compute_intcode(&mut vec!(1, 0, 0, 0, 99)), compute_intcode(&mut intcode);
vec!(2, 0, 0, 0, 99) assert_eq!(intcode, vec!(2, 0, 0, 0, 99));
);
assert_eq!( let mut intcode = vec![2, 3, 0, 3, 99];
compute_intcode(&mut vec!(2, 3, 0, 3, 99)), compute_intcode(&mut intcode);
vec!(2, 3, 0, 6, 99) assert_eq!(intcode, vec!(2, 3, 0, 6, 99));
);
assert_eq!( let mut intcode = vec![2, 4, 4, 5, 99, 0];
compute_intcode(&mut vec!(2, 4, 4, 5, 99, 0)), compute_intcode(&mut intcode);
vec!(2, 4, 4, 5, 99, 9801) assert_eq!(intcode, vec!(2, 4, 4, 5, 99, 9801));
);
assert_eq!( let mut intcode = vec![1, 1, 1, 4, 99, 5, 6, 0, 99];
compute_intcode(&mut vec!(1, 1, 1, 4, 99, 5, 6, 0, 99)), compute_intcode(&mut intcode);
vec!(30, 1, 1, 4, 2, 5, 6, 0, 99) assert_eq!(intcode, vec!(30, 1, 1, 4, 2, 5, 6, 0, 99));
);
} }