···
1
+
use std::collections::{HashMap, HashSet};
7
-
let input: String = std::fs::read_to_string("../input.txt").expect("invalid input!!");
8
-
let input: Vec<&str> = input.split("\n").collect();
9
-
let combinations: Vec<Combination> = &input[..input.len() - 2].iter().map(|v| {
10
-
let v: Vec<&str> = v.split(" ").collect();
12
-
[original, _, target] => Combination {
13
-
original: original.to_string(),
14
-
target: target.to_string(),
17
-
original: "".to_string(),
18
-
target: "".to_string(),
4
+
let input = include_str!("../../input.txt");
5
+
let input: Vec<&str> = input.trim().split("\n").collect();
7
+
let combinations: HashMap<String, Vec<String>> = {
8
+
let mut combinations = HashMap::new();
9
+
input[..input.len() - 2].iter().for_each(|v| {
10
+
let v: Vec<&str> = v.split(" ").collect();
12
+
[original, "=>", target] => {
13
+
let entry = combinations
14
+
.entry(original.to_string())
15
+
.or_insert(Vec::with_capacity(1));
16
+
entry.push(target.to_string());
18
+
_ => panic!("{:?}", v),
24
+
let medicine_mol: Vec<String> = {
25
+
let mut mol = Vec::new();
26
+
input.last().unwrap().chars().for_each(|letter| {
27
+
if letter.is_lowercase() {
28
+
mol.push(format!("{}{}", mol.last().unwrap(), letter));
30
+
mol.push(format!("{}", letter))
35
+
println!("{:?}", medicine_mol);
39
+
let mol_len = medicine_mol.len();
40
+
let mut possibilities: HashSet<String> = HashSet::new();
41
+
for (k, element) in medicine_mol.iter().enumerate() {
42
+
let of_element = combinations.get(element);
45
+
Some(of_element) => of_element.iter().for_each(|new_element| {
46
+
possibilities.insert(format!(
48
+
medicine_mol[0..k].join(""),
50
+
medicine_mol[k + 1..mol_len].join("")
56
+
println!("Part 1: {}", possibilities.len())