From 508f898e44d7fb10da3048b32596d7055b4e8b0e Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:10:13 +0100 Subject: [PATCH] feat(24): complete lexicographic permutations --- readme.md | 4 ++-- src/bin/24.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/commands/run.rs | 5 ++++- src/main.rs | 2 ++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/bin/24.rs diff --git a/readme.md b/readme.md index 31dd22f..e4cab24 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins). ## Challenge Completion -### 26 out of 100 public challenges completed. +### 27 out of 100 public challenges completed. - [x] 1 - [Multiples of 3 or 5](src/bin/1.rs) - [x] 2 - [Even Fibonacci numbers](src/bin/2.rs) @@ -42,7 +42,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins). - [x] 21 - Amicable numbers - [x] 22 - [Names scores](src/bin/22.rs) - [x] 23 - Non-abundant sums -- [ ] 24 - Lexicographic permutations +- [x] 24 - Lexicographic permutations - [ ] 25 - 1000-digit Fibonacci number - [ ] 26 - Reciprocal cycles - [x] 27 - [Quadratic primes](src/bin/27.rs) diff --git a/src/bin/24.rs b/src/bin/24.rs new file mode 100644 index 0000000..3078a32 --- /dev/null +++ b/src/bin/24.rs @@ -0,0 +1,54 @@ +/* +Problem 24 - Lexicographic Permutations + +A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are: +012   021   102   120   201   210 +What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? +*/ + +fn permutations(array: Vec) -> Vec> +where + T: Clone, +{ + let mut out = vec![]; + + match array.len() { + 0 => vec![], + 1 => vec![array], + _ => { + for i in 0..array.len() { + let mut array = array.clone(); + let first = array.remove(i); + let mut permutations = permutations(array); + + for permutation in permutations.iter_mut() { + permutation.insert(0, first.clone()); + } + + out.append(&mut permutations); + } + + out + } + } +} + +fn nth_lexicographic_permutation(array: Vec, n: usize) -> Vec { + let mut permutations = permutations(array); + permutations.sort(); + + permutations[n - 1].clone() +} + +pub fn main() { + let digits = (0..10).collect::>(); + let millionth_permutation = nth_lexicographic_permutation(digits, 1_000_000); + + println!( + "The millionth lexicographic permutation of the digits 0-9 is {}!", + millionth_permutation + .iter() + .map(|&d| d.to_string()) + .collect::() + ); +} diff --git a/src/commands/run.rs b/src/commands/run.rs index 1959f95..b1931c0 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -54,6 +54,8 @@ mod twenty_three; mod twenty_two; #[path = "../bin/2.rs"] mod two; +#[path = "../bin/24.rs"] +mod twenty_four; pub async fn execute( problem: Option, @@ -96,6 +98,7 @@ pub async fn execute( 20 => twenty::main(), 21 => twenty_one::main(), 23 => twenty_three::main(), + 24 => twenty_four::main(), _ => { exists = false; println!( @@ -119,4 +122,4 @@ Time elapsed when executing problem {} is: {:?}", } Ok(()) -} +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 13c264c..99f007e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,9 +30,11 @@ struct RunArgs { #[derive(Subcommand)] enum Commands { /// Handles the initialisation of a new Project Euler Problem + #[clap(alias = "n")] New(NewArgs), /// Runs the solution to a problem + #[clap(alias = "r")] Run(RunArgs), }