wip: cloned graph

This commit is contained in:
Matej Janezic 2023-12-20 12:58:47 +01:00
parent 00000380c1
commit 000003902f
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
1 changed files with 6 additions and 5 deletions

View File

@ -1,7 +1,6 @@
use std::{ use std::{
collections::{HashMap, VecDeque}, collections::{HashMap, VecDeque},
mem::swap, mem::swap,
process::Output,
str::FromStr, str::FromStr,
}; };
@ -80,7 +79,7 @@ fn parse_input(input: &str) -> Vec<Node> {
pub fn part_one(input: &str) -> Option<usize> { pub fn part_one(input: &str) -> Option<usize> {
let mut nodes = parse_input(input); 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 highs = 0;
let mut lows = 0; let mut lows = 0;
@ -94,6 +93,7 @@ pub fn part_one(input: &str) -> Option<usize> {
let mut stack = VecDeque::from([broadcaster]); let mut stack = VecDeque::from([broadcaster]);
while !stack.is_empty() { while !stack.is_empty() {
let mut new_stack = VecDeque::new(); let mut new_stack = VecDeque::new();
let mut new_graph = graph.clone();
while let Some(index) = stack.pop_front() { while let Some(index) = stack.pop_front() {
let node = &nodes[index]; let node = &nodes[index];
@ -102,7 +102,7 @@ pub fn part_one(input: &str) -> Option<usize> {
match node.module { match node.module {
Module::Broadcaster => { Module::Broadcaster => {
for dest_index in node.outputs.iter() { 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); new_stack.push_back(*dest_index);
lows += 1; lows += 1;
} }
@ -121,7 +121,7 @@ pub fn part_one(input: &str) -> Option<usize> {
let signal = !high; let signal = !high;
for dest_index in nodes[index].outputs.iter() { 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); new_stack.push_back(*dest_index);
if signal { if signal {
highs += 1; highs += 1;
@ -142,7 +142,7 @@ pub fn part_one(input: &str) -> Option<usize> {
} }
for dest_index in nodes[index].outputs.iter() { 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); new_stack.push_back(*dest_index);
if !all { if !all {
highs += 1; highs += 1;
@ -160,6 +160,7 @@ pub fn part_one(input: &str) -> Option<usize> {
} }
stack = new_stack; stack = new_stack;
graph = new_graph;
} }
} }