Skip to content

Instantly share code, notes, and snippets.

@sethdusek
Created June 22, 2016 07:40
Show Gist options
  • Save sethdusek/8881777956a58cde652c34afda077ed1 to your computer and use it in GitHub Desktop.
Save sethdusek/8881777956a58cde652c34afda077ed1 to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::prelude::*;
use std::str::FromStr;
use std::io::BufReader;
fn get_hm() -> HashMap<char, i16> {
let file = BufReader::new(File::open("/tmp/scores.txt").unwrap());
let mut hm = HashMap::new();
for line in file.lines() {
if let Ok(line) = line {
let splitted: Vec<&str> = line.split_whitespace().collect();
let alphabet = splitted[0].chars().next().unwrap();
let number = i16::from_str(splitted[1]).unwrap();
hm.insert(alphabet, number);
}
}
hm
}
fn main() {
let mut hm = get_hm();
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
for ch in line.chars() {
match hm.get_mut(&ch) {
Some(count) => *count-=1,
None => continue
}
}
let mut count: BTreeMap<i16, Vec<char>> = BTreeMap::new();
for (k,v) in hm {
if v < 0 {
println!("Invalid Input. More {}'s have been taken from the bag than possible.", k);
std::process::exit(1);
}
count.insert(v, vec![k]);
}
for (k, v) in count.iter().rev() {
print!("{}: ", k);
for (idx, ch) in v.iter().enumerate() {
if idx < v.len()-1 {
print!("{},", ch);
}
else { println!("{}", ch); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment