Skip to content

Instantly share code, notes, and snippets.

@pufe
Created April 8, 2020 15:59
Show Gist options
  • Save pufe/ed29372a3318b373994a48a206cadd48 to your computer and use it in GitHub Desktop.
Save pufe/ed29372a3318b373994a48a206cadd48 to your computer and use it in GitHub Desktop.
Matt Parker's Maths Puzzles #3

What is this?

This is Pufe's solution to Matt Parker's Maths Puzzles #3 as mentioned on video https://youtu.be/JaXo_i3ktwM

How does it work?

It's a simple ruby program to test every letter combination and find out how many scrabble hands have exactly 46 points total

Why did you do it?

I'm neither good enough at Maths to find a clever way of counting, nor patient enough to do it by hand.

How to run?

You need a ruby interpreter (MacOS usually comes with one) and then run on the command line:

ruby mpmp3.rb < mpmp3.in

I've made the program accept an file describing the tiles, in the format on the website https://think-maths.co.uk just in case someone wants to try with a different language Scrabble Set.

Can I copy your code?

Sure, the code is given as is, you may do whatever with it. I provide no guarantee about its safety. If you wanna get legal about it, license is WTFPL .

0 points - _ (x2)
1 point - A (x9), E (x12), I (x9), O (x8), U (x4), L (x4), N (x6), S (x4), T (x6), R (x6)
2 points - D (x4), G (x3)
3 points - B (x2), C (x2), M (x2), P (x2)
4 points - F (x2), H (x2), V (x2), W (x2), Y (x2)
5 points - K (x1)
8 points - J (x1), X (x1)
10 points - Q (x1), Z (x1)
138
AFJKQXZ
AHJKQXZ
AJKQVXZ
AJKQWXZ
AJKQXYZ
BBFJQXZ
BBHJQXZ
BBJQVXZ
BBJQWXZ
BBJQXYZ
BCFJQXZ
BCHJQXZ
BCJQVXZ
BCJQWXZ
BCJQXYZ
BDJKQXZ
BFJMQXZ
BFJPQXZ
BGJKQXZ
BHJMQXZ
BHJPQXZ
BJMQVXZ
BJMQWXZ
BJMQXYZ
BJPQVXZ
BJPQWXZ
BJPQXYZ
CCFJQXZ
CCHJQXZ
CCJQVXZ
CCJQWXZ
CCJQXYZ
CDJKQXZ
CFJMQXZ
CFJPQXZ
CGJKQXZ
CHJMQXZ
CHJPQXZ
CJMQVXZ
CJMQWXZ
CJMQXYZ
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solutions + [prefix]
else
return solutions
end
end
if target >= 0
starting_from.upto(pool.size-1) do |i|
if pool[i][:count] > 0
pool[i][:count] -= 1
solutions = generate(pool, i, target - pool[i][:value], prefix + pool[i][:letter], count+1, solutions)
pool[i][:count] += 1
end
end
end
return solutions
end
def main
pool = read_input($<.readlines)
solutions = generate(pool, 0, 46, "", 0, [])
puts solutions.size
puts solutions
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment