Wrote program for Day 16
This commit is contained in:
parent
bf3efe73e0
commit
d6cdeb76fa
@ -62,7 +62,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| 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 #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_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_large_square: |
|
| 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_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 #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: |
|
||||||
|
6
Week-03/Day-16_Nim-Game/day16/Cargo.toml
Normal file
6
Week-03/Day-16_Nim-Game/day16/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day16"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
28
Week-03/Day-16_Nim-Game/day16/src/lib.rs
Normal file
28
Week-03/Day-16_Nim-Game/day16/src/lib.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub fn nim_winner(n: usize) -> bool {
|
||||||
|
let mut memo: HashMap<usize, HashMap<bool, bool>> = HashMap::new();
|
||||||
|
nim_dp(n, true, &mut memo)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn nim_dp(moves: usize, turn: bool, memo: &mut HashMap<usize, HashMap<bool, bool>>) -> bool {
|
||||||
|
if moves <= 3 {
|
||||||
|
return turn;
|
||||||
|
}
|
||||||
|
|
||||||
|
if memo.contains_key(&moves) && memo[&moves].contains_key(&turn) {
|
||||||
|
return memo[&moves][&turn];
|
||||||
|
}
|
||||||
|
|
||||||
|
memo.insert(moves, HashMap::new());
|
||||||
|
|
||||||
|
for i in 1..=3 {
|
||||||
|
if nim_dp(moves - i, !turn, memo) {
|
||||||
|
memo.get_mut(&moves).unwrap().insert(turn, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memo.get_mut(&moves).unwrap().insert(turn, false);
|
||||||
|
false
|
||||||
|
}
|
25
Week-03/Day-16_Nim-Game/day16/src/main.rs
Normal file
25
Week-03/Day-16_Nim-Game/day16/src/main.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
use day16::nim_winner;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
print!("How many stones will you have during your turn? ");
|
||||||
|
io::stdout().flush().expect("Failed to flush stdout.");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read from stdin");
|
||||||
|
|
||||||
|
let stones = buffer
|
||||||
|
.trim()
|
||||||
|
.parse()
|
||||||
|
.expect("Your input is not a valid number");
|
||||||
|
|
||||||
|
if nim_winner(stones) {
|
||||||
|
println!("You will win!");
|
||||||
|
} else {
|
||||||
|
println!("You will lose!");
|
||||||
|
}
|
||||||
|
}
|
16
Week-03/Day-16_Nim-Game/day16/tests/examples.rs
Normal file
16
Week-03/Day-16_Nim-Game/day16/tests/examples.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use day16::nim_winner;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example1() {
|
||||||
|
assert!(!nim_winner(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example2() {
|
||||||
|
assert!(nim_winner(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example3() {
|
||||||
|
assert!(nim_winner(2));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user