Skip to content

Instantly share code, notes, and snippets.

@natemcintosh
Created January 6, 2023 19:16
Show Gist options
  • Select an option

  • Save natemcintosh/10a8e453735adb047f212d7460652a1f to your computer and use it in GitHub Desktop.

Select an option

Save natemcintosh/10a8e453735adb047f212d7460652a1f to your computer and use it in GitHub Desktop.
Solution for a Riddler Express
using Combinatorics
"""
Riddler Express (https://fivethirtyeight.com/features/can-you-fend-off-the-alien-armada/)
You and a friend are shooting some hoops at your local basketball court when she issues
a challenge: She will name a number, which we'll call N. Your goal is to score exactly N
points in as many ways as possible using only 2-point and 3-point shots. The order of
your shots does not matter.
For example, there are two ways you could score N = 8 points: four 2-pointers or two
3-pointers and one 2-pointer.
Your apparently sadistic friend chooses 60 for the value of N. You try to negotiate this
number down, but to no avail. However, she says you are welcome to pick an even larger
value of N. Does there exist an integer N greater than 60 such that there are fewer ways
to score N points than there are ways to score 60 points?
"""
function gen_possible_values(n::Int)
# Smallest number of throws is n/3, largest number is n/2
# Ask for combinations with replacement of all lengths between those two.
sm = floor(Int, n / 3)
lg = ceil(Int, n / 2)
(nums for ni = sm:lg for nums in with_replacement_combinations((2, 3), ni))
end
count_equal_sums(n::Int) = count(x -> sum(x) == n, gen_possible_values(n))
count_equal_sums(60) # Returns 11
[n for n in 61:100 if count_equal_sums(n) < 11]
# Returns 61
# When checking 61, there are only 10 ways to combine 2 and 3 to get 61.
# The possible ways can be generated with
[nums for nums in gen_possible_values(60) if sum(nums) == 60]
[nums for nums in gen_possible_values(61) if sum(nums) == 61]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment