From 7ee015e03bef1eab5ef6ea1f48867a069b323801 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Mon, 23 Sep 2024 09:10:15 +0200 Subject: [PATCH] Wrote program for Day 60 --- README.md | 2 +- .../Day-60_A-Game-Of-Thrones/day60/Cargo.toml | 6 +++++ .../Day-60_A-Game-Of-Thrones/day60/src/lib.rs | 25 ++++++++++++++++++ .../day60/src/main.rs | 26 +++++++++++++++++++ .../day60/tests/example.rs | 12 +++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Week-09/Day-60_A-Game-Of-Thrones/day60/Cargo.toml create mode 100644 Week-09/Day-60_A-Game-Of-Thrones/day60/src/lib.rs create mode 100644 Week-09/Day-60_A-Game-Of-Thrones/day60/src/main.rs create mode 100644 Week-09/Day-60_A-Game-Of-Thrones/day60/tests/example.rs diff --git a/README.md b/README.md index d344079..6da335f 100644 --- a/README.md +++ b/README.md @@ -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: | diff --git a/Week-09/Day-60_A-Game-Of-Thrones/day60/Cargo.toml b/Week-09/Day-60_A-Game-Of-Thrones/day60/Cargo.toml new file mode 100644 index 0000000..f3a8173 --- /dev/null +++ b/Week-09/Day-60_A-Game-Of-Thrones/day60/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day60" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-09/Day-60_A-Game-Of-Thrones/day60/src/lib.rs b/Week-09/Day-60_A-Game-Of-Thrones/day60/src/lib.rs new file mode 100644 index 0000000..4df6eef --- /dev/null +++ b/Week-09/Day-60_A-Game-Of-Thrones/day60/src/lib.rs @@ -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 +} diff --git a/Week-09/Day-60_A-Game-Of-Thrones/day60/src/main.rs b/Week-09/Day-60_A-Game-Of-Thrones/day60/src/main.rs new file mode 100644 index 0000000..15bb7e2 --- /dev/null +++ b/Week-09/Day-60_A-Game-Of-Thrones/day60/src/main.rs @@ -0,0 +1,26 @@ +use std::io::{self, Write}; + +use day60::game_of_trees; + +fn read_usize() -> Result { + 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"); +} diff --git a/Week-09/Day-60_A-Game-Of-Thrones/day60/tests/example.rs b/Week-09/Day-60_A-Game-Of-Thrones/day60/tests/example.rs new file mode 100644 index 0000000..9edeaa3 --- /dev/null +++ b/Week-09/Day-60_A-Game-Of-Thrones/day60/tests/example.rs @@ -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)] + ); + } +}