Wrote program for Day 24

This commit is contained in:
Mariano Riefolo 2024-08-18 16:56:39 +02:00
parent 2ef354eec0
commit 27747118b1
5 changed files with 49 additions and 1 deletions

View File

@ -70,7 +70,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #21 | [Random Maze Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-21_Random-Maze-Generator) | :white_check_mark: | | Day #21 | [Random Maze Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-21_Random-Maze-Generator) | :white_check_mark: |
| Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_check_mark: | | Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_check_mark: |
| Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_check_mark: | | Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_check_mark: |
| Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_large_square: | | Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: |
| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_large_square: | | Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_large_square: |
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_large_square: | | Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_large_square: |
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: | | Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: |

View File

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

View File

@ -0,0 +1,12 @@
pub fn josephus(n: usize, i: usize) -> usize {
let mut pos = 0;
let mut vec = vec![0];
for _ in 1..n {
pos = (vec.len() * i + pos - (i - 1)) % vec.len();
vec.insert(pos, vec.len());
}
println!("{}/{}, {:?}", pos, vec.len(), vec.iter());
let a = (vec.len() + pos - (i - 1)) % vec.len();
println!("pos: {pos}, i: {i}, a: {a}");
vec.len() - a
}

View File

@ -0,0 +1,6 @@
use day24::josephus;
fn main() {
let res = josephus(11, 3);
println!("{res}");
}

View File

@ -0,0 +1,24 @@
#[cfg(test)]
mod examples {
use day24::josephus;
#[test]
fn example1() {
assert_eq!(josephus(41, 3), 31);
}
#[test]
fn example2() {
assert_eq!(josephus(35, 11), 18);
}
#[test]
fn example3() {
assert_eq!(josephus(11, 1), 11);
}
#[test]
fn example4() {
assert_eq!(josephus(2, 2), 1);
}
}