Rust! I am doing WAY too much object copying. I think I could get the same results by passing vector of references around. But, wanted to not think too much :)
use std::collections::HashSet; use std::mem::swap; #[derive(Debug, PartialOrd, PartialEq, Clone)] enum ActionType { Acc, Jmp, Nop, } #[derive(Debug, PartialOrd, PartialEq, Clone)] struct Action { num: i32, action: ActionType, } impl Action { fn run(&self, pos: &i32, accumulator: &i32) -> (i32, i32) { match self.action { ActionType::Acc => (pos + 1, accumulator + self.num), ActionType::Jmp => (pos + self.num, accumulator.clone()), ActionType::Nop => (pos + 1, accumulator.clone()), } } } fn run_actions(input: &[Action]) -> (i32, bool, Vec<usize>) { let mut actions_taken = HashSet::new(); let mut action_order = vec![]; let mut curr_action: usize = 0; let mut acc: i32 = 0; loop { let action = match input.get(curr_action) { Some(a) => a, None => return (acc, true, action_order), }; let (new_action, new_acc) = action.run(&(curr_action as i32), &acc); action_order.push(new_action as usize); if actions_taken.contains(&new_action) { return (acc, false, action_order); } curr_action = new_action as usize; acc = new_acc; actions_taken.insert(new_action); } } #[aoc(day8, part1)] fn last_value_before_rerun(input: &Vec<Action>) -> i32 { run_actions(input.as_slice()).0 } #[aoc(day8, part2)] fn fix_program(input: &Vec<Action>) -> i32 { let (_, _, mut action_order) = run_actions(input.as_slice()); action_order.reverse(); for action in action_order .iter() .filter(|a| input.get(**a).unwrap().action != ActionType::Acc) { let to_swap: &Action = input.get(*action).unwrap(); let swapped = match to_swap.action { ActionType::Nop => Action { num: to_swap.num, action: ActionType::Jmp, }, ActionType::Jmp => Action { num: to_swap.num, action: ActionType::Nop, }, _ => unreachable!(), }; let (acc, finished, _) = run_actions( [&input[0..*action], &[swapped], &input[*action + 1..]] .concat() .as_slice(), ); if finished { return acc; } } 0 }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust! I am doing WAY too much object copying. I think I could get the same results by passing vector of references around. But, wanted to not think too much :)