Skip to content

Instantly share code, notes, and snippets.

@cubetastic33
Created August 20, 2018 16:22
Show Gist options
  • Save cubetastic33/f6528510977298395cb6836bd9fc1235 to your computer and use it in GitHub Desktop.
Save cubetastic33/f6528510977298395cb6836bd9fc1235 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer - Challenge #361 [Easy] Tally Program
//A program for the r/dailyprogrammer challenge #361 [Easy] Tally Program, done using Rust.
use std::io;
#[derive(Debug)]
struct ScoreCard {
a: i32,
b: i32,
c: i32,
d: i32,
e: i32,
}
fn main() {
let input = get_input();
let mut scores = ScoreCard {a: 0, b: 0, c: 0, d: 0, e: 0};
for letter in input.chars() {
match letter {
'a' => scores.a += 1,
'b' => scores.b += 1,
'c' => scores.c += 1,
'd' => scores.d += 1,
'e' => scores.e += 1,
'A' => scores.a -= 1,
'B' => scores.b -= 1,
'C' => scores.c -= 1,
'D' => scores.d -= 1,
'E' => scores.e -= 1,
_ => println!("Error: Use only letters a, b, c, d, or e."),
}
}
show_result(&scores);
}
fn get_input() -> String {
//Get user input
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
input.trim().to_string()
}
fn show_result(scores: &ScoreCard) {
//Show the result
let mut ordered_scores = vec!(scores.a, scores.b, scores.c, scores.d, scores.e);
ordered_scores.sort();
println!("{:?}", ordered_scores);
//TODO: order the players as well.
for (person, score) in ordered_scores.iter().rev().enumerate() {
match person {
4 => println!("e: {}", score),
_ => print!("{}: {}, ", ['a', 'b', 'c', 'd', 'e'][person], score),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment