Teaser 2586: Powerful dice
From The Sunday Times, 15th April 2012 [link] [link]
George and Martha threw a die, noted the number of spots on the uppermost face, and entered that number in one of the boxes of a 3-by-3 grid of nine squares. They repeated the exercise eight more times resulting in a digit in each box.
Then George looked at the three 3-digit numbers read across the rows and commented that each was a square or a cube. Martha looked at the three 3-digit numbers read down the columns and said the same was true for her numbers. Furthermore, their two sets of three numbers had only one in common.
What (in increasing order) were the five different 3-digit numbers?
[teaser2586]
Jim Randell 8:22 am on 13 August 2024 Permalink |
We can use the [[
SubstitutedExpression]] solver from the enigma.py library to solve this puzzle.The following run file executes in 76ms. (Internal runtime of the generated program is 2.5ms).
Run: [ @replit ]
#! python3 -m enigma -rr SubstitutedExpression # A B C # D E F # G H I --digits="1-6" --distinct="" --code="check = cached(lambda n: is_square(n) or is_cube(n))" # check rows and columns "check(ABC)" "check(DEF)" "check(GHI)" "check(ADG)" "check(BEH)" "check(CFI)" # there is exactly one number shared between the rows and columns "len(intersect([{ABC, DEF, GHI}, {ADG, BEH, CFI}])) = 1" # and there are five different numbers in total "len({ABC, DEF, GHI, ADG, BEH, CFI}) = 5" --answer="call(ordered, {ABC, DEF, GHI, ADG, BEH, CFI})" --template="rows = [ ABC DEF GHI ]; cols = [ ADG BEH CFI ]" --solution="" --verbose="1-H"Solution: The numbers entered in the box were: 121, 125, 216, 256, 512.
We have:
512 appears as both a row and a column.
The most likely scenario is:
Then George’s rows would consist of both squares and cubes, and Martha could say the same about her columns even though they are actually all cubes.
Swapping the rows and columns yields a second viable layout, but it seems unlikely George would note that his each of his rows was a square or a cube, when they are in fact all cubes.
LikeLike
ruudvanderham 11:50 am on 13 August 2024 Permalink |
from istr import istr istr.repr_mode('int') squares=[i*i for i in range(10,32)] cubes=[i*i*i for i in range (5,10)] squares_cubes=istr(squares+cubes) solutions=set() for abc in squares_cubes: for def_ in squares_cubes: for ghi in squares_cubes: horizontals={abc,def_,ghi} verticals={abc[i]|def_[i]|ghi[i] for i in range(3)} if all(vertical in squares_cubes for vertical in verticals): horizontals_verticals= horizontals | verticals if len(horizontals_verticals)==5: solutions.add(tuple(sorted(horizontals_verticals))) print(solutions)LikeLike
Ruud 7:07 pm on 13 August 2024 Permalink |
I forgot to test whether all digits were between 1 and 6.
Here’s an improved version (same output):
from istr import istr istr.repr_mode("int") squares = [i * i for i in range(10, 32)] cubes = [i * i * i for i in range(5, 10)] squares_cubes = [i for i in istr(squares + cubes) if all(j in range(1, 7) for j in i)] solutions = set() for abc in squares_cubes: for def_ in squares_cubes: for ghi in squares_cubes: horizontals = {abc, def_, ghi} verticals = {abc[i] | def_[i] | ghi[i] for i in range(3)} if all(vertical in squares_cubes for vertical in verticals): horizontals_verticals = horizontals | verticals if len(horizontals_verticals) == 5: solutions.add(tuple(sorted(horizontals_verticals))) print(solutions)LikeLike
Jim Randell 5:54 pm on 14 August 2024 Permalink |
@Ruud: I think you also need to add a test for “their two sets of three numbers had only one in common”. Your code would allow a repeated row or a repeated column.
LikeLike
Ruud 7:07 pm on 14 August 2024 Permalink |
@Jim
Thanks for pointing this out. You are right.
My updated code:
from istr import istr istr.repr_mode("int") squares = [i * i for i in range(10, 32)] cubes = [i * i * i for i in range(5, 10)] squares_cubes = [i for i in istr(squares + cubes) if all(j in range(1,7) for j in i)] solutions = set() for abc in squares_cubes: for def_ in squares_cubes: for ghi in squares_cubes: horizontals = {abc, def_, ghi} verticals = {abc[i] | def_[i] | ghi[i] for i in range(3)} if all(vertical in squares_cubes for vertical in verticals): if len(horizontals & verticals) == 1: solutions.add(tuple(sorted(horizontals | verticals))) print(solutions)LikeLike