Wrote program for Day 17
This commit is contained in:
parent
d6cdeb76fa
commit
d30e1215b4
@ -63,7 +63,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :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_check_mark: |
|
||||||
| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_check_mark: |
|
| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_check_mark: |
|
||||||
| Day #16 | [Nim Game](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-16_Nim-Game) | :white_check_mark: |
|
| Day #16 | [Nim Game](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-16_Nim-Game) | :white_check_mark: |
|
||||||
| Day #17 | [Prison Break](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-17_Prison-Break) | :white_large_square: |
|
| Day #17 | [Prison Break](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-17_Prison-Break) | :white_check_mark: |
|
||||||
| Day #18 | [Unique Paths](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-18_Unique-Paths) | :white_large_square: |
|
| Day #18 | [Unique Paths](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-18_Unique-Paths) | :white_large_square: |
|
||||||
| Day #19 | [URL Shortener](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-19_URL-Shortener) | :white_large_square: |
|
| Day #19 | [URL Shortener](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-19_URL-Shortener) | :white_large_square: |
|
||||||
| Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_large_square: |
|
| Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_large_square: |
|
||||||
|
6
Week-03/Day-17_Prison-Break/day17/Cargo.toml
Normal file
6
Week-03/Day-17_Prison-Break/day17/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day17"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
17
Week-03/Day-17_Prison-Break/day17/src/lib.rs
Normal file
17
Week-03/Day-17_Prison-Break/day17/src/lib.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
pub fn freed_prisoners(cells: &[bool]) -> usize {
|
||||||
|
if !cells[0] {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
let mut required = true;
|
||||||
|
|
||||||
|
for c in cells {
|
||||||
|
if c == &required {
|
||||||
|
count += 1;
|
||||||
|
required = !required;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count
|
||||||
|
}
|
27
Week-03/Day-17_Prison-Break/day17/src/main.rs
Normal file
27
Week-03/Day-17_Prison-Break/day17/src/main.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use day17::freed_prisoners;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
print!("Enter the cells (0s and 1s space separated): ");
|
||||||
|
io::stdout().flush().expect("Failed to flush stdout");
|
||||||
|
|
||||||
|
std::io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read from stdin");
|
||||||
|
|
||||||
|
let cells: Vec<bool> = buffer
|
||||||
|
.split_whitespace()
|
||||||
|
.map(
|
||||||
|
|x| match x.parse::<u8>().expect("Only numbers are expected") {
|
||||||
|
0 => false,
|
||||||
|
1 => true,
|
||||||
|
_ => panic!("Only 0s and 1s are allowed"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.collect();
|
||||||
|
let result = freed_prisoners(&cells);
|
||||||
|
|
||||||
|
println!("You can free {} prisoners", result);
|
||||||
|
}
|
24
Week-03/Day-17_Prison-Break/day17/tests/examples.rs
Normal file
24
Week-03/Day-17_Prison-Break/day17/tests/examples.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
use day17::freed_prisoners;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example1() {
|
||||||
|
assert_eq!(
|
||||||
|
freed_prisoners(&[true, true, false, false, false, true, false]),
|
||||||
|
4
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example2() {
|
||||||
|
assert_eq!(freed_prisoners(&[true, true, true]), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example3() {
|
||||||
|
assert_eq!(freed_prisoners(&[false, false, false]), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example4() {
|
||||||
|
assert_eq!(freed_prisoners(&[false, true, true, true]), 0);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user