From 12180acb0201d8fbe37a50e2f2aaa51d1c8dfc69 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 3 Jul 2026 21:42:33 -0400 Subject: [PATCH] Add Generate Permutations Of All Valid 9-ball Racks as a Math TIL --- README.md | 7 +++- ...-permutations-of-all-valid-9-ball-racks.md | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 math/generate-permutations-of-all-valid-9-ball-racks.md diff --git a/README.md b/README.md index 560ba20..de91659 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1807 TILs and counting..._ +_1808 TILs and counting..._ See some of the other learning resources I work on: @@ -60,6 +60,7 @@ If you've learned something here, support my efforts writing daily TILs by * [Linux](#linux) * [LLM](#llm) * [Mac](#mac) +* [Math](#math) * [Mise](#mise) * [MongoDB](#mongodb) * [MySQL](#mysql) @@ -771,6 +772,10 @@ If you've learned something here, support my efforts writing daily TILs by - [View All Windows Of The Current App](mac/view-all-windows-of-the-current-app.md) - [Write System Clipboard To A File](mac/write-system-clipboard-to-a-file.md) +### Math + +- [Generate Permutations Of All Valid 9-ball Racks](math/generate-permutations-of-all-valid-9-ball-racks.md) + ### Mise - [Create Umbrella Task For All Test Tasks](mise/create-umbrella-task-for-all-test-tasks.md) diff --git a/math/generate-permutations-of-all-valid-9-ball-racks.md b/math/generate-permutations-of-all-valid-9-ball-racks.md new file mode 100644 index 0000000..0454d84 --- /dev/null +++ b/math/generate-permutations-of-all-valid-9-ball-racks.md @@ -0,0 +1,42 @@ +# Generate Permutations Of All Valid 9-ball Racks + +I wanted to produce a full listing of all valid rack arrangements for the game +of [9-ball](https://en.wikipedia.org/wiki/Nine-ball). The constraints on how a +9-ball rack can be arranged are, first, that the 1 ball must be placed at the +head of the diamond and, second, that the 9 ball must be placed at the center of +the diamond. After that, all other balls (2 through 8) can be placed in any +arrangement. + +Because each of those seven remaining balls can be arranged in distinct +orderings where each ball is placed once, this is a +[_permutation_](https://en.wikipedia.org/wiki/Permutation) problem. + +> In elementary combinatorics, the k-permutations, or partial permutations, are +> the ordered arrangements of k distinct elements selected from a set. When k is +> equal to the size of the set, these are the permutations in the previous +> sense. + +For this problem, the seven distinct elements can be arranged into `7!` (seven +factorial) unique permutations. That is, 5040 permutations. + +I can use [Ruby's `Array#permutations` +method](https://docs.ruby-lang.org/en/4.0/Array.html#method-i-permutation) to +enumerate these 5040 permutations like so: + +```ruby +[2,3,4,5,6,7,8].permutation.map do |perm| + [1, *perm[0..2], 9, *perm[3..7]] +end.to_a +=> [[1, 2, 3, 4, 9, 5, 6, 7, 8], + [1, 2, 3, 4, 9, 5, 6, 8, 7], + [1, 2, 3, 4, 9, 5, 7, 6, 8], + [1, 2, 3, 4, 9, 5, 7, 8, 6], + [1, 2, 3, 4, 9, 5, 8, 6, 7], + [1, 2, 3, 4, 9, 5, 8, 7, 6], + [1, 2, 3, 4, 9, 6, 5, 7, 8], + ... + [1, 8, 7, 6, 9, 5, 3, 2, 4], + [1, 8, 7, 6, 9, 5, 3, 4, 2], + [1, 8, 7, 6, 9, 5, 4, 2, 3], + [1, 8, 7, 6, 9, 5, 4, 3, 2]] +```