Day 20, part 2
This commit is contained in:
parent
63fecbd235
commit
93f574463b
|
@ -1,7 +1,7 @@
|
|||
use aoc22::{day20, util};
|
||||
|
||||
pub fn main() {
|
||||
let nodes = day20::parse_file(&util::parse_input());
|
||||
let nodes = day20::parse_file(&util::parse_input(), 1);
|
||||
|
||||
day20::mix(&nodes);
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
use aoc22::{day20, util};
|
||||
|
||||
const KEY: isize = 811589153;
|
||||
const ROUNDS: usize = 10;
|
||||
|
||||
pub fn main() {
|
||||
let nodes = day20::parse_file(&util::parse_input(), KEY);
|
||||
// day20::print_nodes(&nodes);
|
||||
|
||||
for _ in 0..ROUNDS {
|
||||
day20::mix(&nodes);
|
||||
// day20::print_nodes(&nodes);
|
||||
}
|
||||
|
||||
println!("Sum of coordinates: {}", day20::calc_coordinates(&nodes));
|
||||
}
|
18
src/day20.rs
18
src/day20.rs
|
@ -35,14 +35,14 @@ pub fn get_prev(node: &Rc<RefCell<Node>>, n: usize) -> Rc<RefCell<Node>> {
|
|||
curr
|
||||
}
|
||||
|
||||
pub fn parse_file(input: &String) -> Vec<Rc<RefCell<Node>>> {
|
||||
pub fn parse_file(input: &String, key: isize) -> Vec<Rc<RefCell<Node>>> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
let mut last = None;
|
||||
for line in input.lines() {
|
||||
let val: isize = line.parse().unwrap();
|
||||
let node = Rc::new(RefCell::new(Node {
|
||||
val,
|
||||
val: val * key,
|
||||
next: None,
|
||||
prev: last.clone(),
|
||||
}));
|
||||
|
@ -64,9 +64,11 @@ pub fn parse_file(input: &String) -> Vec<Rc<RefCell<Node>>> {
|
|||
}
|
||||
|
||||
pub fn mix(nodes: &Vec<Rc<RefCell<Node>>>) {
|
||||
let div = nodes.len() - 1;
|
||||
|
||||
for node in nodes {
|
||||
let n = node.borrow().val;
|
||||
if n == 0 {
|
||||
if n == 0 || (n > 0 && (n as usize) % div == 0) || (n < 0 && (-n as usize) % div == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -77,9 +79,13 @@ pub fn mix(nodes: &Vec<Rc<RefCell<Node>>>) {
|
|||
next.borrow_mut().prev = Some(prev.clone());
|
||||
|
||||
let prev = if n > 0 {
|
||||
get_next(node, n as usize)
|
||||
let n = (n as usize) % div;
|
||||
assert!(n < div);
|
||||
get_next(node, n)
|
||||
} else {
|
||||
get_prev(node, (-n as usize) + 1)
|
||||
let n = ((-n as usize) % div) + 1;
|
||||
assert!(n < div);
|
||||
get_prev(node, n)
|
||||
};
|
||||
let next = get_next(&prev, 1);
|
||||
prev.deref().borrow_mut().next = Some(node.clone());
|
||||
|
@ -89,8 +95,6 @@ pub fn mix(nodes: &Vec<Rc<RefCell<Node>>>) {
|
|||
n_mut.prev = Some(prev.clone());
|
||||
n_mut.next = Some(next.clone());
|
||||
drop(n_mut);
|
||||
|
||||
// print_nodes(&nodes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue