Day 20, part 2
This commit is contained in:
		@ -1,7 +1,7 @@
 | 
				
			|||||||
use aoc22::{day20, util};
 | 
					use aoc22::{day20, util};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn main() {
 | 
					pub fn main() {
 | 
				
			||||||
    let nodes = day20::parse_file(&util::parse_input());
 | 
					    let nodes = day20::parse_file(&util::parse_input(), 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    day20::mix(&nodes);
 | 
					    day20::mix(&nodes);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										16
									
								
								src/bin/d20p2.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/bin/d20p2.rs
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
					    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 result = Vec::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut last = None;
 | 
					    let mut last = None;
 | 
				
			||||||
    for line in input.lines() {
 | 
					    for line in input.lines() {
 | 
				
			||||||
        let val: isize = line.parse().unwrap();
 | 
					        let val: isize = line.parse().unwrap();
 | 
				
			||||||
        let node = Rc::new(RefCell::new(Node {
 | 
					        let node = Rc::new(RefCell::new(Node {
 | 
				
			||||||
            val,
 | 
					            val: val * key,
 | 
				
			||||||
            next: None,
 | 
					            next: None,
 | 
				
			||||||
            prev: last.clone(),
 | 
					            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>>>) {
 | 
					pub fn mix(nodes: &Vec<Rc<RefCell<Node>>>) {
 | 
				
			||||||
 | 
					    let div = nodes.len() - 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for node in nodes {
 | 
					    for node in nodes {
 | 
				
			||||||
        let n = node.borrow().val;
 | 
					        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;
 | 
					            continue;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -77,9 +79,13 @@ pub fn mix(nodes: &Vec<Rc<RefCell<Node>>>) {
 | 
				
			|||||||
        next.borrow_mut().prev = Some(prev.clone());
 | 
					        next.borrow_mut().prev = Some(prev.clone());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let prev = if n > 0 {
 | 
					        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 {
 | 
					        } 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);
 | 
					        let next = get_next(&prev, 1);
 | 
				
			||||||
        prev.deref().borrow_mut().next = Some(node.clone());
 | 
					        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.prev = Some(prev.clone());
 | 
				
			||||||
        n_mut.next = Some(next.clone());
 | 
					        n_mut.next = Some(next.clone());
 | 
				
			||||||
        drop(n_mut);
 | 
					        drop(n_mut);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        // print_nodes(&nodes);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user