-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
57 lines (45 loc) · 1.29 KB
/
main.rs
File metadata and controls
57 lines (45 loc) · 1.29 KB
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
use std::fs;
use std::cmp;
use std::collections::HashMap;
fn solve1(input: &str) -> usize {
let mut reacts = Vec::new();
for n in input.chars() {
if reacts.is_empty() {
reacts.push(n);
continue;
}
if let Some(bn) = reacts.pop() {
if bn.eq_ignore_ascii_case(&n) && bn != n {
continue;
}else{
reacts.push(bn);
reacts.push(n);
}
}
}
reacts.len() - 1
}
fn solve2(input: &str) -> u32 {
let mut repeated = HashMap::new();
let mut minimal: u32 = std::u32::MAX;
for c in input.chars() {
if None != repeated.get(&c.to_ascii_lowercase()){
continue;
}
let batch = input.chars().filter(|n| {
!c.eq_ignore_ascii_case(&n)
});
let solved = solve1(&batch.collect::<String>());
minimal = cmp::min(minimal, solved as u32);
repeated.insert(c.to_ascii_lowercase(), true);
}
minimal
}
fn main() {
let input = fs::read_to_string("input.txt").expect("Unable to read file");
//let input = "dabAcCaCBAcCcaDA";
println!("size: {}", input.len());
println!("solution 1: {}", solve1(&input));
println!("solution 2: {}", solve2(&input));
// solve2(&input);
}