Finished December 14

This commit is contained in:
2021-12-15 01:04:27 +01:00
parent 10bd7c4997
commit 6b02bdfb80

View File

@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{HashMap};
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
@@ -15,7 +15,7 @@ fn main(){
line_vec.push(line.unwrap());
}
let mut input_seq: Vec<char> = line_vec[0].chars().collect();
let input_seq: Vec<char> = line_vec[0].chars().collect();
let mut rules: Vec<( (char, char) , char )> = Vec::new();
for i in 2..line_vec.len() {
@@ -28,39 +28,105 @@ fn main(){
rules.push( ( input_tuple, output ) );
}
for step in 0..STEPS {
let mut output_seq: Vec<char> = Vec::new();
for i in 0..input_seq.len() - 1 {
let first = input_seq[i];
let second = input_seq[i + 1];
output_seq.push(first);
for rule in &rules {
let r_in = rule.0;
let r_out = rule.1;
if first == r_in.0 && second == r_in.1 {
output_seq.push(r_out);
break;
}
let mut pair_map: HashMap<String, u128> = HashMap::new();
for i in 0..input_seq.len()-1 {
let mut str = String::from(input_seq[i]);
str.push(input_seq[i + 1]);
*pair_map.entry(str).or_insert(0) += 1 ;
}
for _step in 0..STEPS {
let mut output_map = HashMap::new();
let temp_pair_map = pair_map.clone();
for rule in &rules {
let mut key_string = String::from(rule.0.0);
key_string.push(rule.0.1);
if temp_pair_map.contains_key(&key_string) {
//pair_map.remove(&key_string);
let count = temp_pair_map.get(&key_string).unwrap();
let mut pair_1 = String::from(rule.0.0);
pair_1.push(rule.1);
let mut pair_2 = String::from(rule.1);
pair_2.push(rule.0.1);
*output_map.entry(pair_1).or_insert(0) += count ;
*output_map.entry(pair_2).or_insert(0) += count ;
}
}
output_seq.push(input_seq[input_seq.len()-1]);
println!("Step: {}", step);
input_seq = output_seq;
pair_map = output_map;
}
let mut counts = BTreeMap::new();
for word in input_seq.iter() {
*counts.entry(word).or_insert(0) += 1;
let mut letter_map = HashMap::new();
for pair in &pair_map {
let char_1 = pair.0.chars().collect::<Vec<char>>()[0];
let char_2 = pair.0.chars().collect::<Vec<char>>()[1];
let count = pair.1;
*letter_map.entry(char_1).or_insert(0) += count ;
*letter_map.entry(char_2).or_insert(0) += count ;
}
let max = counts.iter().max_by_key(|&(_, count)| count);
let min = counts.iter().min_by_key(|&(_, count)| count);
let mut out_map = HashMap::new();
for letter in &letter_map {
let current_char = letter.0.to_owned();
let count = letter.1.to_owned();
let mut new_count = 0;
if count % 2 == 1 {
new_count = ((count - 1 )/2) + 1;
} else if count == 1 {
new_count = 1;
} else {
new_count = count / 2;
}
*out_map.entry(current_char).or_insert(0) += new_count ;
}
let max = out_map.iter().max_by_key(|&(_, count)| count).unwrap().1.to_owned();
let min = out_map.iter().min_by_key(|&(_, count)| count).unwrap().1.to_owned();
println!("Diff: {}", max - min);
// for step in 0..STEPS {
// let mut output_seq: Vec<char> = Vec::new();
// for i in 0..input_seq.len() - 1 {
// let first = input_seq[i];
// let second = input_seq[i + 1];
// output_seq.push(first);
// for rule in &rules {
// let r_in = rule.0;
// let r_out = rule.1;
// if first == r_in.0 && second == r_in.1 {
// output_seq.push(r_out);
// break;
// }
// }
// }
// output_seq.push(input_seq[input_seq.len()-1]);
// println!("Step: {}", step);
// input_seq = output_seq;
// }
// let mut counts = BTreeMap::new();
// for word in input_seq.iter() {
// *counts.entry(word).or_insert(0) += 1;
// }
// let max = counts.iter().max_by_key(|&(_, count)| count);
// let min = counts.iter().min_by_key(|&(_, count)| count);
// println!("Max: {:?}, Min: {:?}, Diff: {}", min.unwrap(), max.unwrap(), max.unwrap().1 - min.unwrap().1) ;
println!("Max: {:?}, Min: {:?}, Diff: {}", min.unwrap(), max.unwrap(), max.unwrap().1 - min.unwrap().1) ;
}