132 lines
3.9 KiB
Rust
132 lines
3.9 KiB
Rust
use std::collections::{HashMap};
|
|
use std::fs::File;
|
|
use std::io::{BufReader};
|
|
use std::io::prelude::*;
|
|
|
|
const STEPS: u32 = 40;
|
|
|
|
fn main(){
|
|
let file = File::open("./input.txt").expect("Read failed");
|
|
let reader = BufReader::new(file);
|
|
|
|
let mut line_vec = Vec::new();
|
|
|
|
for line in reader.lines() {
|
|
line_vec.push(line.unwrap());
|
|
}
|
|
|
|
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() {
|
|
let split_vec: Vec<&str> = line_vec[i].split(" -> ").collect();
|
|
let input_vec: Vec<char> = split_vec[0].to_owned().chars().collect();
|
|
let input_tuple = (input_vec[0], input_vec[1]);
|
|
|
|
let output = split_vec[1].chars().collect::<Vec<char>>()[0];
|
|
|
|
rules.push( ( input_tuple, output ) );
|
|
}
|
|
|
|
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 ;
|
|
}
|
|
}
|
|
pair_map = output_map;
|
|
}
|
|
|
|
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 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) ;
|
|
|
|
|
|
|
|
} |