summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inputs/day1945
-rw-r--r--src/bin/day19.rs58
2 files changed, 103 insertions, 0 deletions
diff --git a/inputs/day19 b/inputs/day19
new file mode 100644
index 0000000..b0515c0
--- /dev/null
+++ b/inputs/day19
@@ -0,0 +1,45 @@
+Al => ThF
+Al => ThRnFAr
+B => BCa
+B => TiB
+B => TiRnFAr
+Ca => CaCa
+Ca => PB
+Ca => PRnFAr
+Ca => SiRnFYFAr
+Ca => SiRnMgAr
+Ca => SiTh
+F => CaF
+F => PMg
+F => SiAl
+H => CRnAlAr
+H => CRnFYFYFAr
+H => CRnFYMgAr
+H => CRnMgYFAr
+H => HCa
+H => NRnFYFAr
+H => NRnMgAr
+H => NTh
+H => OB
+H => ORnFAr
+Mg => BF
+Mg => TiMg
+N => CRnFAr
+N => HSi
+O => CRnFYFAr
+O => CRnMgAr
+O => HP
+O => NRnFAr
+O => OTi
+P => CaP
+P => PTi
+P => SiRnFAr
+Si => CaSi
+Th => ThCa
+Ti => BP
+Ti => TiTi
+e => HF
+e => NAl
+e => OMg
+
+CRnSiRnCaPTiMgYCaPTiRnFArSiThFArCaSiThSiThPBCaCaSiRnSiRnTiTiMgArPBCaPMgYPTiRnFArFArCaSiRnBPMgArPRnCaPTiRnFArCaSiThCaCaFArPBCaCaPTiTiRnFArCaSiRnSiAlYSiThRnFArArCaSiRnBFArCaCaSiRnSiThCaCaCaFYCaPTiBCaSiThCaSiThPMgArSiRnCaPBFYCaCaFArCaCaCaCaSiThCaSiRnPRnFArPBSiThPRnFArSiRnMgArCaFYFArCaSiRnSiAlArTiTiTiTiTiTiTiRnPMgArPTiTiTiBSiRnSiAlArTiTiRnPMgArCaFYBPBPTiRnSiRnMgArSiThCaFArCaSiThFArPRnFArCaSiRnTiBSiThSiRnSiAlYCaFArPRnFArSiThCaFArCaCaSiThCaCaCaSiRnPRnCaFArFYPMgArCaPBCaPBSiRnFYPBCaFArCaSiAl
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);
+ }
+}