Created
October 2, 2024 05:07
-
-
Save jmanian/68bdbc791b1ba012b182f03770ba3199 to your computer and use it in GitHub Desktop.
Box problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.theguardian.com/science/2024/sep/30/did-you-solve-it-the-box-problem-that-baffled-the-boffins | |
def all_games | |
(0...15).to_a.combination(2).map do |prize_a, prize_b| | |
game(prize_a, prize_b) | |
end.tally | |
end | |
def game(prize_a, prize_b) | |
field = make_field(prize_a, prize_b) | |
andrew_score = andrew_score(field) | |
barbara_score = barbara_score(field) | |
if andrew_score == barbara_score | |
:tie | |
elsif andrew_score < barbara_score | |
:andrew | |
else | |
:barbara | |
end | |
end | |
def make_field(prize_a, prize_b) | |
field = [ | |
Array.new(5) { false }, | |
Array.new(5) { false }, | |
Array.new(5) { false } | |
] | |
prize_a_row = prize_a / 5 | |
prize_a_col = prize_a % 5 | |
prize_b_row = prize_b / 5 | |
prize_b_col = prize_b % 5 | |
field[prize_a_row][prize_a_col] = true | |
field[prize_b_row][prize_b_col] = true | |
field | |
end | |
def andrew_score(field) | |
(0...3).to_a.product((0...5).to_a).index do |row, col| | |
field[row][col] | |
end | |
end | |
def barbara_score(field) | |
(0...5).to_a.product((0...3).to_a).index do |col, row| | |
field[row][col] | |
end | |
end | |
all_games | |
# {:tie=>23, :andrew=>43, :barbara=>39} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment