Last active
December 24, 2015 11:21
-
-
Save salty-horse/dc76fab4f2ea42d9b19a to your computer and use it in GitHub Desktop.
Advent of Code - day 19 part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::fs::File; | |
| use std::io::BufReader; | |
| use std::io::BufRead; | |
| use std::collections::HashSet; | |
| fn main() { | |
| let input_fname = "day19_input.txt"; | |
| let f = File::open(input_fname).unwrap(); | |
| let mut replacements: Vec<(String, String)> = Vec::new(); | |
| let mut reader = BufReader::new(f); | |
| for line in (&mut reader).lines() { | |
| let line = line.unwrap(); | |
| if line.len() == 0 { | |
| break; | |
| } | |
| let v: Vec<&str> = line.splitn(2, " => ").collect(); | |
| replacements.push((v[0].to_string(), v[1].to_string())); | |
| } | |
| let molecule = reader.lines().next().unwrap().unwrap(); | |
| let mut output_molecules: HashSet<String> = HashSet::new(); | |
| for &(ref from, ref to) in replacements.iter() { | |
| for (ix, _) in molecule.match_indices(from) { | |
| let mut out_molecule = molecule[0 .. ix].to_string(); | |
| out_molecule.push_str(to); | |
| out_molecule.push_str(&molecule[ix + from.len() ..]); | |
| output_molecules.insert(out_molecule); | |
| } | |
| } | |
| println!("Unique molecules: {}", output_molecules.len()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment