summaryrefslogtreecommitdiff
path: root/src/bin/day19.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/day19.rs')
-rw-r--r--src/bin/day19.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/bin/day19.rs b/src/bin/day19.rs
new file mode 100644
index 0000000..7c1e6b4
--- /dev/null
+++ b/src/bin/day19.rs
@@ -0,0 +1,58 @@
+use std::collections::{HashMap, HashSet};
+
+fn main() {
+ let input = advent::read_lines(19);
+ println!("19a: {}", distinct_molecules(&input));
+ println!("19b: {}", 0);
+}
+
+fn distinct_molecules(input: &[String]) -> usize {
+ let mut input = input.to_owned();
+ let molecule = input.pop().unwrap();
+ input.pop(); // drop empty line
+
+ let mut replacements = HashMap::new();
+ for line in input {
+ let replacement : Vec<_> = line.split(" => ").collect();
+ let (left, right) = (replacement[0].to_string(), replacement[1].to_string());
+ replacements.entry(left).or_insert(Vec::new()).push(right);
+ }
+
+ let mut result = HashSet::new();
+
+ for (left, rights) in replacements {
+ for right in rights {
+ for i in 0 .. molecule.len() {
+ let check_pos = std::cmp::min(i + left.len(), molecule.len());
+ if molecule[i..check_pos].contains(&left) {
+ let new_molecule = molecule[0..i].to_string()
+ + &right
+ + &molecule[(i + left.len())..];
+ result.insert(new_molecule);
+ }
+ }
+ }
+ }
+
+ result.len()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test() {
+ let mut input = Vec::from([
+ "H => HO".to_string(),
+ "H => OH".to_string(),
+ "O => HH".to_string(),
+ "".to_string(),
+ "HOH".to_string(),
+ ]);
+ assert_eq!(distinct_molecules(&input), 4);
+ input.pop();
+ input.push("HOHOHO".to_string());
+ assert_eq!(distinct_molecules(&input), 7);
+ }
+}