Wrote program for Day 60
This commit is contained in:
parent
4ed0deba71
commit
7ee015e03b
@ -106,7 +106,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
||||
| Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_check_mark: |
|
||||
| Day #58 | [Create A Dice Roller](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-58_Create-A-Dice-Roller) | :white_check_mark: |
|
||||
| Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_check_mark: |
|
||||
| Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_large_square: |
|
||||
| Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_check_mark: |
|
||||
| Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_large_square: |
|
||||
| Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_large_square: |
|
||||
| Day #63 | [The Rabbit Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-63_The-Rabbit-Problem) | :white_large_square: |
|
||||
|
6
Week-09/Day-60_A-Game-Of-Thrones/day60/Cargo.toml
Normal file
6
Week-09/Day-60_A-Game-Of-Thrones/day60/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day60"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
25
Week-09/Day-60_A-Game-Of-Thrones/day60/src/lib.rs
Normal file
25
Week-09/Day-60_A-Game-Of-Thrones/day60/src/lib.rs
Normal file
@ -0,0 +1,25 @@
|
||||
pub fn game_of_trees(mut number: usize) -> Vec<(usize, i8)> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
while number != 1 {
|
||||
match number % 3 {
|
||||
0 => {
|
||||
result.push((number, 0));
|
||||
number /= 3;
|
||||
}
|
||||
1 => {
|
||||
result.push((number, -1));
|
||||
number = (number - 1) / 3;
|
||||
}
|
||||
2 => {
|
||||
result.push((number, 1));
|
||||
number = (number + 1) / 3;
|
||||
}
|
||||
_ => {
|
||||
eprintln!("???");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
26
Week-09/Day-60_A-Game-Of-Thrones/day60/src/main.rs
Normal file
26
Week-09/Day-60_A-Game-Of-Thrones/day60/src/main.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use std::io::{self, Write};
|
||||
|
||||
use day60::game_of_trees;
|
||||
|
||||
fn read_usize() -> Result<usize, std::num::ParseIntError> {
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
input.trim().parse()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
print!("Enter the starting number for the game of trees: ");
|
||||
io::stdout().flush().unwrap();
|
||||
let n = match read_usize() {
|
||||
Ok(n) => n,
|
||||
Err(_) => {
|
||||
println!("Invalid input. Please enter a valid number.");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let result = game_of_trees(n);
|
||||
for el in result {
|
||||
println!("{} {}", el.0, el.1);
|
||||
}
|
||||
println!("1");
|
||||
}
|
12
Week-09/Day-60_A-Game-Of-Thrones/day60/tests/example.rs
Normal file
12
Week-09/Day-60_A-Game-Of-Thrones/day60/tests/example.rs
Normal file
@ -0,0 +1,12 @@
|
||||
#[cfg(test)]
|
||||
mod example {
|
||||
use day60::game_of_trees;
|
||||
|
||||
#[test]
|
||||
fn example() {
|
||||
assert_eq!(
|
||||
game_of_trees(100),
|
||||
vec![(100, -1), (33, 0), (11, 1), (4, -1)]
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user