1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::error::Error;
use std::io::{self, Read};
use lazy_static::lazy_static;
use regex::Regex;
fn main() -> Result<(), Box<Error>> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let output = solve(&input)?;
println!("{}", output);
Ok(())
}
fn solve(input: &str) -> Result<String, Box<Error>> {
let polymer = input.trim();
// Part One
// let reaction = Reaction {
// polymer: polymer.into()
// };
// Ok(reaction.last().map(|x| x.len().to_string()).unwrap())
// Part Two
let output = (b'a'..=b'z')
.map(|x| x as char)
.map(|x| {
polymer
.replace(x, "")
.replace(&x.to_uppercase().to_string(), "")
})
.map(|polymer| Reaction { polymer })
.flat_map(|reaction| reaction.last())
.map(|polymer| polymer.len())
.min();
Ok(output.unwrap().to_string())
}
#[test]
fn test_solve() {
let input = "dabAcCaCBAcCcaDA";
let output = solve(input).unwrap();
// Part One
// assert_eq!(output, "10".to_string());
// Part Two
assert_eq!(output, "4".to_string());
}
struct Reaction {
polymer: String,
}
impl Iterator for Reaction {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
lazy_static! {
static ref RE: Regex = {
let re: String = (b'a'..=b'z')
.map(|x| x as char)
.flat_map(|x| {
vec![
format!("{}{}", x, x.to_uppercase()),
format!("{}{}", x.to_uppercase(), x),
]
})
.collect::<Vec<_>>()
.join("|");
Regex::new(&re).unwrap()
};
}
let current = self.polymer.clone();
self.polymer = RE.replace(&self.polymer, "").into();
if self.polymer == current {
None
} else {
Some(self.polymer.to_string())
}
}
}
#[test]
fn test_reaction() {
let polymer = "dabAcCaCBAcCcaDA".into();
let mut reaction = Reaction { polymer };
let polymer = reaction.next().unwrap();
assert_eq!(polymer, "dabAaCBAcCcaDA".to_string());
let polymer = reaction.next().unwrap();
assert_eq!(polymer, "dabCBAcCcaDA".to_string());
let polymer = reaction.next().unwrap();
assert_eq!(polymer, "dabCBAcaDA".to_string());
assert_eq!(reaction.next(), Option::None);
}