did day5
This commit is contained in:
parent
fa6bd59445
commit
93eb6067f3
4
day5/Cargo.lock
generated
Normal file
4
day5/Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
|
6
day5/Cargo.toml
Normal file
6
day5/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
authors = ["attila <daf276@gmx.de>"]
|
||||
|
||||
[dependencies]
|
1
day5/input
Normal file
1
day5/input
Normal file
File diff suppressed because one or more lines are too long
65
day5/src/main.rs
Normal file
65
day5/src/main.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use std::fs;
|
||||
use std::char;
|
||||
|
||||
fn main() {
|
||||
let lines = fs::read_to_string("input").expect("Unable to read file");
|
||||
|
||||
//Part 1
|
||||
println!("{}", reduce(lines.clone()));
|
||||
|
||||
//Part 2
|
||||
|
||||
let mut all_lengths: Vec<i32> = Vec::new();
|
||||
|
||||
for i in 0..26 {
|
||||
let big_char = char::from_u32(65+i).unwrap();
|
||||
let little_char = char::from_u32(97+i).unwrap();
|
||||
|
||||
let string = lines.clone().replace(big_char, "").replace(little_char, "");
|
||||
|
||||
all_lengths.push(reduce(string));
|
||||
}
|
||||
|
||||
println!("{}", all_lengths.iter().min().unwrap());
|
||||
}
|
||||
|
||||
fn reduce(string: String) -> i32 {
|
||||
let mut current_string = string;
|
||||
let mut new_string: Vec<char>;
|
||||
let mut last_string = String::new();
|
||||
|
||||
while last_string != current_string {
|
||||
let all_chars: Vec<char> = current_string.chars().into_iter().collect();
|
||||
let mut count = 0;
|
||||
|
||||
new_string = Vec::new();
|
||||
|
||||
while count < all_chars.len() - 1 {
|
||||
let cur_char_uppercase = all_chars[count].to_uppercase().collect::<Vec<char>>()[0];
|
||||
let cur_char_lowercase = all_chars[count].to_lowercase().collect::<Vec<char>>()[0];
|
||||
|
||||
if all_chars[count] == cur_char_lowercase {
|
||||
if all_chars[count + 1] == cur_char_uppercase{
|
||||
count += 1;
|
||||
} else {
|
||||
new_string.push(all_chars[count].clone());
|
||||
}
|
||||
} else {
|
||||
if all_chars[count + 1] == cur_char_lowercase{
|
||||
count += 1;
|
||||
} else {
|
||||
new_string.push(all_chars[count].clone());
|
||||
}
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
|
||||
new_string.push(all_chars[all_chars.len() - 1].clone());
|
||||
|
||||
last_string= current_string.clone();
|
||||
current_string = new_string.clone().into_iter().collect();
|
||||
}
|
||||
|
||||
return (current_string.len()-1) as i32
|
||||
}
|
Loading…
Reference in New Issue
Block a user