Solution for Moons and Umbrellas

main
Inga 🏳‍🌈 4 years ago
parent 07c71874d2
commit fdde07dee2
  1. 9
      0327-Qualification/task2-moons-umbrellas/Cargo.toml
  2. 31
      0327-Qualification/task2-moons-umbrellas/src/main.rs

@ -0,0 +1,9 @@
[package]
name = "task2-moons-umbrellas"
version = "0.1.0"
authors = ["inga-lovinde <52715130+inga-lovinde@users.noreply.github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,31 @@
use std::{io::{self, BufRead}};
const CHAR_C: char = 'C';
const CHAR_J: char = 'J';
fn solve(cj_price: u32, jc_price: u32, template: &str) -> u32 {
let chars: Vec<_> = template.chars().filter(|&ch| ch == CHAR_C || ch == CHAR_J).collect();
let mut result = 0;
for i in 1..chars.len() {
result += match (chars[i-1], chars[i]) {
(CHAR_C, CHAR_J) => cj_price,
(CHAR_J, CHAR_C) => jc_price,
_ => 0,
};
}
result
}
fn main() {
let stdin = io::stdin();
let mut lines = stdin.lock().lines();
let test_number: u32 = lines.next().unwrap().unwrap().parse().unwrap();
for t in 1..=test_number {
let line = lines.next().unwrap().unwrap();
let parts: Vec<_> = line.split(char::is_whitespace).collect();
println!("Case #{}: {}", t, solve(parts[0].parse().unwrap(), parts[1].parse().unwrap(), parts[2]));
}
}
Loading…
Cancel
Save