aoc/2016/src/bin/2016_06b.rs

47 lines
1.8 KiB
Rust

// Of course, that would be the message - if you hadn't agreed to use a modified repetition code
// instead.
//
// In this modified code, the sender instead transmits what looks like random data, but for each
// character, the character they actually want to send is slightly less likely than the others.
// Even after signal-jamming noise, you can look at the letter distributions in each column and
// choose the least common letter to reconstruct the original message.
//
// In the above example, the least common character in the first column is a; in the second, d,
// and so on. Repeating this process for the remaining characters produces the original message,
// advent.
//
// Given the recording in your puzzle input and this new decoding methodology, what is the original
// message that Santa is trying to send?
//
use std::collections::HashMap;
use core::read_data;
fn main() {
let binding = read_data("2016_06_data.txt");
let lines = binding.lines();
let mut final_string = String::new();
let mut results = vec![HashMap::<char, u32>::new(); 8];
for line in lines {
for (index, char) in line.chars().enumerate() {
println!("Index {index} -> {char}");
results[index].entry(char).and_modify(|x| *x += 1).or_insert(1);
}
}
for results_column in &results {
let mut current_min = u32::MAX ;
let mut current_char = ' ';
for (index, (key, value)) in results_column.iter().enumerate() {
if current_min > *value {
current_min = *value;
current_char = *key;
}
}
println!("Character {current_char} with {current_min}");
final_string.push(current_char);
}
println!("Password is {final_string}");
}
// xrwcsnps