-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.rs
53 lines (44 loc) · 1.25 KB
/
day5.rs
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
use std::fs;
use std::cmp;
use std::usize;
fn main() {
let contents = fs::read_to_string("day5.input")
.expect("Something went wrong reading the file");
let mut polymer = Vec::new();
for c in contents.trim().chars() {
polymer.push(c);
}
println!("{:?}", reduce(&mut polymer));
let mut min = usize::MAX;
for b in String::from("abcdefghijklmnopqrstuvwxyz").chars() {
let mut polymer = Vec::new();
let b = b.to_ascii_uppercase();
for c in contents.trim().chars() {
if c.to_ascii_uppercase() != b {
polymer.push(c);
}
}
min = cmp::min(min, reduce(&mut polymer));
}
println!("{:?}", min);
}
fn reduce(polymer: &mut Vec<char>) -> usize {
let mut i = 0;
while i < (polymer.len() - 1) {
if compare(polymer[i], polymer[i+1]) {
polymer.remove(i);
polymer.remove(i);
i = cmp::max(i as i32 - 2,0) as usize;
} else {
i += 1;
}
}
polymer.len()
}
fn compare(a: char, b: char) -> bool {
if (a.is_uppercase() && b.is_uppercase()) ||
(a.is_lowercase() && b.is_lowercase()) {
return false;
}
a.to_ascii_uppercase() == b.to_ascii_uppercase()
}