Wrote program for Day 17

This commit is contained in:
Mariano Riefolo 2024-08-11 12:08:41 +02:00
parent d6cdeb76fa
commit d30e1215b4
5 changed files with 75 additions and 1 deletions

View File

@ -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 #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 #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 #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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day17"
version = "0.1.0"
edition = "2021"
[dependencies]

View 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
}

View 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);
}

View 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);
}