aboutsummaryrefslogtreecommitdiff
path: root/day05.rs
diff options
context:
space:
mode:
Diffstat (limited to 'day05.rs')
-rw-r--r--day05.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/day05.rs b/day05.rs
new file mode 100644
index 0000000..ea6e919
--- /dev/null
+++ b/day05.rs
@@ -0,0 +1,31 @@
+use std::fs::File;
+use std::io::prelude::*;
+
+fn react(s: &mut String) -> usize {
+ while let Some(pos) = (0..s.len() - 1).find(|i| {
+ (s.as_bytes()[*i] as i32 - s.as_bytes()[*i + 1] as i32).abs() == 32
+ }) {
+ s.replace_range(pos..pos + 2, "");
+ }
+ return s.len();
+}
+
+fn main() {
+ let mut f = File::open("day05").unwrap();
+
+ let mut s = String::new();
+ f.read_to_string(&mut s).unwrap();
+ s.pop();
+
+ println!("{}", react(&mut s.clone()));
+
+ println!("{}",
+ (65u8..65u8 + 27)
+ .map(|c| {
+ let mut t = s.clone();
+ t.retain(|b| b != (c as char) && b != (c + 32u8) as char);
+ react(&mut t)
+ })
+ .min()
+ .unwrap());
+}