summaryrefslogtreecommitdiff
path: root/2016/aoc6/src/main.rs
blob: 45ae3b1df9b39f4f1a24a2297b25d9a535d3cb90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
use std::collections::HashMap;

fn main() {
    let lines = read_file();
    let answer_width = lines[0].len();
    for i in 0..answer_width {
        let line = lines.iter().map(|line| line.chars().nth(i).unwrap()).collect::<Vec<_>>();

        let mut char_counts = HashMap::new();
        for character in line {
            *char_counts.entry(character).or_insert(0) += 1;
        }
        let (character, _) = char_counts.iter().min_by_key(|&(_, &count)| count).unwrap();
        println!("{}", character);
    }
}

fn read_file() -> Vec<String> {
    let file = BufReader::new(File::open("test_input.txt").unwrap());
    file.lines()
        .map(|line| line.unwrap().trim().to_string())
        .filter(|line| line.len() > 0)
        .collect()
}