diff --git a/src/bin/20.rs b/src/bin/20.rs index 5bdb6af..4d3a5d8 100644 --- a/src/bin/20.rs +++ b/src/bin/20.rs @@ -1,7 +1,6 @@ use std::{ collections::{HashMap, VecDeque}, mem::swap, - process::Output, str::FromStr, }; @@ -80,7 +79,7 @@ fn parse_input(input: &str) -> Vec { pub fn part_one(input: &str) -> Option { let mut nodes = parse_input(input); - let mut graph = vec![vec![None; input.len()]; input.len()]; + let mut graph = vec![vec![None; nodes.len()]; nodes.len()]; let mut highs = 0; let mut lows = 0; @@ -94,6 +93,7 @@ pub fn part_one(input: &str) -> Option { let mut stack = VecDeque::from([broadcaster]); while !stack.is_empty() { let mut new_stack = VecDeque::new(); + let mut new_graph = graph.clone(); while let Some(index) = stack.pop_front() { let node = &nodes[index]; @@ -102,7 +102,7 @@ pub fn part_one(input: &str) -> Option { match node.module { Module::Broadcaster => { for dest_index in node.outputs.iter() { - graph[index][*dest_index] = Some(false); + new_graph[index][*dest_index] = Some(false); new_stack.push_back(*dest_index); lows += 1; } @@ -121,7 +121,7 @@ pub fn part_one(input: &str) -> Option { let signal = !high; for dest_index in nodes[index].outputs.iter() { - graph[index][*dest_index] = Some(signal); + new_graph[index][*dest_index] = Some(signal); new_stack.push_back(*dest_index); if signal { highs += 1; @@ -142,7 +142,7 @@ pub fn part_one(input: &str) -> Option { } for dest_index in nodes[index].outputs.iter() { - graph[index][*dest_index] = Some(!all); + new_graph[index][*dest_index] = Some(!all); new_stack.push_back(*dest_index); if !all { highs += 1; @@ -160,6 +160,7 @@ pub fn part_one(input: &str) -> Option { } stack = new_stack; + graph = new_graph; } }