+54
-18
2015/19/rust/src/main.rs
+54
-18
2015/19/rust/src/main.rs
···
1
-
struct Combination {
2
-
original: String,
3
-
target: String,
4
-
}
1
+
use std::collections::{HashMap, HashSet};
5
2
6
3
fn main() {
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();
11
-
match v[..] {
12
-
[original, _, target] => Combination {
13
-
original: original.to_string(),
14
-
target: target.to_string(),
15
-
},
16
-
_ => Combination {
17
-
original: "".to_string(),
18
-
target: "".to_string(),
19
-
},
4
+
let input = include_str!("../../input.txt");
5
+
let input: Vec<&str> = input.trim().split("\n").collect();
6
+
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();
11
+
match v[..] {
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());
17
+
}
18
+
_ => panic!("{:?}", v),
19
+
}
20
+
});
21
+
combinations
22
+
};
23
+
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));
29
+
} else {
30
+
mol.push(format!("{}", letter))
31
+
}
32
+
});
33
+
mol
34
+
};
35
+
println!("{:?}", medicine_mol);
36
+
37
+
// part 1
38
+
{
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);
43
+
44
+
match of_element {
45
+
Some(of_element) => of_element.iter().for_each(|new_element| {
46
+
possibilities.insert(format!(
47
+
"{}{}{}",
48
+
medicine_mol[0..k].join(""),
49
+
new_element,
50
+
medicine_mol[k + 1..mol_len].join("")
51
+
));
52
+
}),
53
+
_ => {}
54
+
}
20
55
}
21
-
}).
56
+
println!("Part 1: {}", possibilities.len())
57
+
}
22
58
}