Wrote program for Day 13
This commit is contained in:
parent
c107af7217
commit
24a619b9e8
@ -59,7 +59,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
||||
| Day #10 | [Unique Binary Search Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-10_Unique-Binary-Search-Trees) | :white_check_mark: |
|
||||
| Day #11 | [Restore IP Addresses](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-11_Restore-IP-Addresses) | :white_check_mark: |
|
||||
| Day #12 | [Mountains or Valleys](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-12_Mountains_And_Valleys) | :white_check_mark: |
|
||||
| Day #13 | [Need Help With Your Packing](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-13_Need-Help-With-Packing) | :white_large_square: |
|
||||
| Day #13 | [Need Help With Your Packing](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-13_Need-Help-With-Packing) | :white_check_mark: |
|
||||
| Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :white_large_square: |
|
||||
| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_large_square: |
|
||||
| Day #16 | [Nim Game](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-16_Nim-Game) | :white_large_square: |
|
||||
|
6
Week-02/Day-13_Need-Help-With-Packing/day13/Cargo.toml
Normal file
6
Week-02/Day-13_Need-Help-With-Packing/day13/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day13"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
28
Week-02/Day-13_Need-Help-With-Packing/day13/src/lib.rs
Normal file
28
Week-02/Day-13_Need-Help-With-Packing/day13/src/lib.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub fn can_fit(weights: &[usize], bags: usize) -> bool {
|
||||
bags >= min_bags(&mut VecDeque::from(weights.to_vec()), 0)
|
||||
}
|
||||
|
||||
fn min_bags(weights: &mut VecDeque<usize>, current_weight: usize) -> usize {
|
||||
if weights.is_empty() {
|
||||
return if current_weight > 0 { 1 } else { 0 };
|
||||
}
|
||||
|
||||
let mut min = usize::MAX;
|
||||
|
||||
for _ in 0..weights.len() {
|
||||
let w = match weights.pop_front() {
|
||||
Some(x) => x,
|
||||
None => panic!("At the disco"),
|
||||
};
|
||||
if current_weight + w > 10 {
|
||||
min = std::cmp::min(min, min_bags(weights, w)) + 1;
|
||||
} else {
|
||||
min = std::cmp::min(min, min_bags(weights, current_weight + w));
|
||||
}
|
||||
weights.push_back(w);
|
||||
}
|
||||
|
||||
min
|
||||
}
|
41
Week-02/Day-13_Need-Help-With-Packing/day13/src/main.rs
Normal file
41
Week-02/Day-13_Need-Help-With-Packing/day13/src/main.rs
Normal file
@ -0,0 +1,41 @@
|
||||
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.");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
use day13::can_fit;
|
||||
|
||||
#[test]
|
||||
fn example1() {
|
||||
assert!(can_fit(&[2, 1, 2, 5, 4, 3, 6, 1, 1, 9, 3, 2], 4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example2() {
|
||||
assert!(!can_fit(&[2, 7, 1, 3, 3, 4, 7, 4, 1, 8, 2], 4))
|
||||
}
|
Loading…
Reference in New Issue
Block a user