From 4e5fb867d39ee7e44574c738ecc385612fa390a2 Mon Sep 17 00:00:00 2001 From: Dylan Thies Date: Thu, 21 Dec 2023 08:31:18 -0500 Subject: [PATCH] some slight refactor on day20 part 1 --- 2023/day-20/src/part1.rs | 58 ++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/2023/day-20/src/part1.rs b/2023/day-20/src/part1.rs index f37327c..42542d3 100644 --- a/2023/day-20/src/part1.rs +++ b/2023/day-20/src/part1.rs @@ -21,6 +21,7 @@ enum ModuleType<'a> { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] struct Module<'a> { + pub label: &'a str, pub mod_type: ModuleType<'a>, pub connections: Vec<&'a str>, } @@ -44,6 +45,34 @@ impl<'a> Module<'a> { } } } + + fn state_hash(&self) -> String { + match &self.mod_type { + ModuleType::Broadcast => (self.label).to_string(), + ModuleType::FlipFlop(is_on) => { + if *is_on { + self.label.to_uppercase() + } else { + self.label.to_lowercase() + } + } + ModuleType::Conjunction(last_froms_was_high) => { + "%".to_string() + + self.label + + &last_froms_was_high + .iter() + .map(|(key, value)| { + if *value { + key.to_uppercase() + } else { + key.to_lowercase() + } + }) + .collect::() + + "%" + } + } + } } fn push_button<'a>( @@ -89,32 +118,8 @@ fn push_button<'a>( fn setup_to_key(setup: &BTreeMap<&str, Module>) -> String { setup - .iter() - .map(|(label, module)| match &module.mod_type { - ModuleType::Broadcast => (*label).to_string(), - ModuleType::FlipFlop(is_on) => { - if *is_on { - label.to_uppercase() - } else { - label.to_lowercase() - } - } - ModuleType::Conjunction(map) => { - "%".to_string() - + *label - + &map - .iter() - .map(|(key, value)| { - if *value { - key.to_uppercase() - } else { - key.to_lowercase() - } - }) - .collect::() - + "%" - } - }) + .values() + .map(|module| module.state_hash()) .collect::() } @@ -199,6 +204,7 @@ fn parse_line(input: &str) -> IResult<&str, (&str, Module)> { ( label, Module { + label, mod_type, connections, },