my solutions to advent of code
aoc advent-of-code

still not right day 19 2015

aylac.top 0194f0b0 6741f2bc

verified
Changed files
+55 -19
2015
19
rust
src
template
rust
src
+54 -18
2015/19/rust/src/main.rs
···
-
struct Combination {
-
original: String,
-
target: String,
-
}
+
use std::collections::{HashMap, HashSet};
fn main() {
-
let input: String = std::fs::read_to_string("../input.txt").expect("invalid input!!");
-
let input: Vec<&str> = input.split("\n").collect();
-
let combinations: Vec<Combination> = &input[..input.len() - 2].iter().map(|v| {
-
let v: Vec<&str> = v.split(" ").collect();
-
match v[..] {
-
[original, _, target] => Combination {
-
original: original.to_string(),
-
target: target.to_string(),
-
},
-
_ => Combination {
-
original: "".to_string(),
-
target: "".to_string(),
-
},
+
let input = include_str!("../../input.txt");
+
let input: Vec<&str> = input.trim().split("\n").collect();
+
+
let combinations: HashMap<String, Vec<String>> = {
+
let mut combinations = HashMap::new();
+
input[..input.len() - 2].iter().for_each(|v| {
+
let v: Vec<&str> = v.split(" ").collect();
+
match v[..] {
+
[original, "=>", target] => {
+
let entry = combinations
+
.entry(original.to_string())
+
.or_insert(Vec::with_capacity(1));
+
entry.push(target.to_string());
+
}
+
_ => panic!("{:?}", v),
+
}
+
});
+
combinations
+
};
+
+
let medicine_mol: Vec<String> = {
+
let mut mol = Vec::new();
+
input.last().unwrap().chars().for_each(|letter| {
+
if letter.is_lowercase() {
+
mol.push(format!("{}{}", mol.last().unwrap(), letter));
+
} else {
+
mol.push(format!("{}", letter))
+
}
+
});
+
mol
+
};
+
println!("{:?}", medicine_mol);
+
+
// part 1
+
{
+
let mol_len = medicine_mol.len();
+
let mut possibilities: HashSet<String> = HashSet::new();
+
for (k, element) in medicine_mol.iter().enumerate() {
+
let of_element = combinations.get(element);
+
+
match of_element {
+
Some(of_element) => of_element.iter().for_each(|new_element| {
+
possibilities.insert(format!(
+
"{}{}{}",
+
medicine_mol[0..k].join(""),
+
new_element,
+
medicine_mol[k + 1..mol_len].join("")
+
));
+
}),
+
_ => {}
+
}
}
-
}).
+
println!("Part 1: {}", possibilities.len())
+
}
}
+1 -1
template/rust/src/main.rs
···
fn main() {
-
let input = include_str!("../../input.txt");
+
let input = include_str!("../../input.txt").trim();
println!("{}", &input);
}