Skip to content

Commit

Permalink
Tiny bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiac committed Dec 21, 2023
1 parent 134adef commit 55185d9
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions aoc-solver/src/y2023/day20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ fn parse(input: &str) -> Result<HashMap<String, Module>, AocError> {
.try_collect()?;

let mut inputs: HashMap<String, Vec<String>> = HashMap::new();

// Construct module inputs
for (name, module) in modules.iter() {
for output in &module.outputs {
inputs.entry(output.clone()).or_default().push(name.clone());
Expand Down Expand Up @@ -121,39 +119,21 @@ fn press_button(
};

if let Some(output) = output {
emit_output(
output,
&current,
module,
&mut output_buffer,
&mut lows,
&mut highs,
);
match output {
true => highs += module.outputs.len() as u32,
false => lows += module.outputs.len() as u32,
}

for target in &module.outputs {
output_buffer.push_back((target.clone(), output, current.to_owned()));
}
}
}
}

(lows, highs)
}

fn emit_output(
output: bool,
source: &str,
module: &mut Module,
output_buffer: &mut VecDeque<(String, bool, String)>,
lows: &mut u32,
highs: &mut u32,
) {
match output {
true => *highs += module.outputs.len() as u32,
false => *lows += module.outputs.len() as u32,
}

for target in &module.outputs {
output_buffer.push_back((target.clone(), output, source.to_owned()));
}
}

fn gcd(a: u64, b: u64) -> u64 {
if b == 0 {
a
Expand Down

0 comments on commit 55185d9

Please sign in to comment.