100-days-of-rust/Week-02/Day-13_Need-Help-With-Packing/day13/src/main.rs

42 lines
1009 B
Rust
Raw Normal View History

2024-08-07 12:11:20 +00:00
use std::io::{self, Write};
use day13::can_fit;
fn main() {
let mut buffer = String::new();
print!("Insert the weights separated by spaces: ");
io::stdout().flush().expect("Failed to flush stdout.");
io::stdin()
.read_line(&mut buffer)
.expect("Failed to read from stdin.");
let weights: Vec<usize> = buffer
.trim()
.split(' ')
.filter(|x| !x.is_empty())
.map(|x| {
x.parse()
.expect("Only whole numbers and spaces are allowed.")
})
.collect();
buffer = String::new();
print!("Insert how many bags are available: ");
io::stdout().flush().expect("Failed to flush stdout.");
io::stdin()
.read_line(&mut buffer)
.expect("Failed to read from stdin.");
let bags = buffer.trim().parse().expect("Integer expected.");
if can_fit(&weights, bags) {
println!("There are enough bags.");
} else {
println!("more bags are needed.");
}
}