Add D8P1 in Rust

This commit is contained in:
kageru 2019-12-08 14:18:41 +01:00
parent 66218401af
commit ade972a995
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
4 changed files with 60 additions and 0 deletions

9
2019/08/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day08"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
2019/08/input Normal file

File diff suppressed because one or more lines are too long

28
2019/08/src/counter.rs Normal file
View File

@ -0,0 +1,28 @@
use std::collections::HashMap;
use std::ops::Index;
use std::hash::Hash;
#[derive(Debug)]
pub struct Counter<T: Hash + Eq + Clone> {
values: HashMap<T, usize>
}
impl <T: Hash + Eq + Clone> Counter<T> {
pub fn new() -> Self {
Self { values: HashMap::new() }
}
pub fn add(&mut self, collection: &[T]) {
for x in collection {
self.values.insert(x.clone(), self.values.get(&x).unwrap_or(&0) + 1);
}
}
}
impl <T: Hash + Eq + Clone> Index<T> for Counter<T> {
type Output = usize;
fn index(&self, idx: T) -> &usize {
self.values.get(&idx).unwrap_or(&0)
}
}

22
2019/08/src/main.rs Normal file
View File

@ -0,0 +1,22 @@
mod counter;
use counter::Counter;
use std::io::BufRead;
const WIDTH: usize = 25;
const HEIGHT: usize = 6;
fn main() {
let input: Vec<_> = std::io::stdin().lock().lines().next().unwrap().unwrap().chars().collect();
let mut counters: Vec<_> = vec![];
let mut chunks = input.chunks(WIDTH).peekable();
while chunks.peek().is_some() {
let mut counter = Counter::new();
for _ in 0..HEIGHT {
counter.add(chunks.next().unwrap());
}
counters.push(counter);
}
let fewest_zeros = counters.into_iter().min_by_key(|c| c['0']).unwrap();
println!("{:?}", fewest_zeros['1'] * fewest_zeros['2']);
}