Browse Source

Update day8

main
Anthony J. Martinez 7 months ago
parent
commit
5a4a68065c
  1. 19
      day8/src/main.rs

19
day8/src/main.rs

@ -7,8 +7,9 @@ use std::time::Instant;
fn part1(data: &Vec<String>) -> isize {
let mut acc: isize = 0;
let mut pos: usize = 0;
let elements: usize = data.len();
let mut inst_set = data.clone();
engine(&mut pos, &mut acc, &mut inst_set);
engine(&mut pos, &mut acc, &mut inst_set, &elements);
acc
}
@ -22,7 +23,7 @@ fn part2(data: &Vec<String>) -> isize {
let val = &data[i];
if val.contains("nop") {
inst_set[i] = inst_set[i].replace("nop", "jmp");
engine(&mut pos, &mut acc, &mut inst_set);
engine(&mut pos, &mut acc, &mut inst_set, &elements);
if pos == elements {
p2_ans = acc;
println!("Replaced value at idx: {}", i);
@ -30,7 +31,7 @@ fn part2(data: &Vec<String>) -> isize {
}
} else if val.contains("jmp") {
inst_set[i] = inst_set[i].replace("jmp", "nop");
engine(&mut pos, &mut acc, &mut inst_set);
engine(&mut pos, &mut acc, &mut inst_set, &elements);
if pos == elements {
p2_ans = acc;
println!("Replaced value at idx: {}", i);
@ -45,10 +46,10 @@ fn part2(data: &Vec<String>) -> isize {
fn engine(pos: &mut usize,
acc: &mut isize,
inst_set: &mut Vec<String>) {
inst_set: &mut Vec<String>,
len: &usize) {
loop {
let len = inst_set.len();
if *pos < len {
if *pos < *len {
let inst = inst_set[*pos].clone();
let op: Vec<&str> = inst.split_ascii_whitespace().collect();
if op.is_empty() {
@ -58,13 +59,13 @@ fn engine(pos: &mut usize,
"nop" => {
inst_set[*pos] = String::new();
*pos += 1;
engine(pos, acc, inst_set);
engine(pos, acc, inst_set, len);
},
"acc" => {
inst_set[*pos] = String::new();
*pos += 1;
*acc += op[1].parse::<isize>().unwrap();
engine(pos, acc, inst_set);
engine(pos, acc, inst_set, len);
},
"jmp" => {
inst_set[*pos] = String::new();
@ -74,7 +75,7 @@ fn engine(pos: &mut usize,
} else {
*pos += jump as usize;
}
engine(pos, acc, inst_set);
engine(pos, acc, inst_set, len);
},
_ => ()
}

Loading…
Cancel
Save