Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Add recursion solution
Source Link
fireflame241
  • 16.4k
  • 2
  • 31
  • 74

A new 169-byte solution that replaces combinations with recursion:

g=lambda l,w,h,k=[]:all(g(l,w,h,k+[((e-(p&4)*e//2)*1j**p+p/8+p/8/w*1j)%w%(1j*h)for e in l])for p in range(8*w*h))if w*h>len(k)else len(set(k))-w*h
from itertools import*

This has the advantage of removing combinations (12 characters on its own) and one for loop, but the self-invocation takes many bytes. Currying would not save length.

A new 169-byte solution that replaces combinations with recursion:

g=lambda l,w,h,k=[]:all(g(l,w,h,k+[((e-(p&4)*e//2)*1j**p+p/8+p/8/w*1j)%w%(1j*h)for e in l])for p in range(8*w*h))if w*h>len(k)else len(set(k))-w*h
from itertools import*

This has the advantage of removing combinations (12 characters on its own) and one for loop, but the self-invocation takes many bytes. Currying would not save length.

+2-2 bytes (fix + combinations)
Source Link
fireflame241
  • 16.4k
  • 2
  • 31
  • 74
lambda l,w,h:all(w*h-len({((e-(p&4)*e//2)*1j**p+p/8+p/8j8/ww*1j)%w%(1j*h)for e in l for p in c})for c in productcombinations(range(8*w*h),repeat=w*hw*h/len(l)))
from itertools import*

Try it online Try it online with many testcases. Since this brute-forces, I have commented out a few cases to run fast enough for TIO.

lambda l,w,h:
 all( # we want any configuration to work, but De Morgan gives any(L==len) <==> not all(L!=len) <==> not all(L-len)
 w*h-len( # if two polyominos in a configuration overlap, then there are duplicate cells
 # so the length of the set is less
 { # create a set consisting of each possible position+orientation of L/len(l) polyominos:
 ( # here, e is a single cell of the given polyomino
 ( # reflect e across the imaginary axis if p >= 4 (mod 8)
 e- # e-e.real*2 = e-e//.5 reflects across the Im axis
 p&4 # Only reflect if the 2^2 bit is nonzero: gives 4* or 0* following
 *e//2 # floor(z) = z.real when z is complex, so
 ) # e//2 (floor division) gives z.real/2 (complex floor division only works in Python 2)
 *1j**p # rotate depending on the 2^0 and 2^1 bits. i**x is cyclic every 4
 +p/8 # translate horizontally (real component) by p>>3 (mod this later)
 +p/8j8/ww*1j # translate vertically (im component) by p>>3 / w
 )%w%(1j*h) # mod to within grid (complex mods only work in Python 2)
 for e in l # find where each cell e of the given polyomino goes
 for p in c # do this for each c (each set of position+orientation integers)
 }
 )
 for c in productcombinations( # iterate c over all sets of w*h/len(l) distinct integers from 0 to 8*L-1
 range(8*w*h) # each of these 8*L integers corresponds to a single position/orientation of a polyomino
 # Bits 2^0 and 2^1 give the rotation, and 2^2 gives the reflection
 # The higher bits give the position from 0 to L=w*h-1 ==> (0,0) to (w-1,h-1)
 ,repeat=w*hw*h/len(l) # w*h/len(l) is the number of polyominos needed since len(l) is the number of cells per polyomino
 # can't switch to *[range(8*w*h)]*(w*h/len(l)) because Python 3 does not allow short complex operations as above
 )
 )
from itertools import*
lambda l,w,h:all(w*h-len({((e-(p&4)*e//2)*1j**p+p/8+p/8j/w)%w%(1j*h)for e in l for p in c})for c in product(range(8*w*h),repeat=w*h/len(l)))
from itertools import*

Try it online with many testcases. Since this brute-forces, I have commented out a few cases to run fast enough for TIO.

lambda l,w,h:
 all( # we want any configuration to work, but De Morgan gives any(L==len) <==> not all(L!=len) <==> not all(L-len)
 w*h-len( # if two polyominos in a configuration overlap, then there are duplicate cells
 # so the length of the set is less
 { # create a set consisting of each possible position+orientation of L/len(l) polyominos:
 ( # here, e is a single cell of the given polyomino
 ( # reflect e across the imaginary axis if p >= 4 (mod 8)
 e- # e-e.real*2 = e-e//.5 reflects across the Im axis
 p&4 # Only reflect if the 2^2 bit is nonzero: gives 4* or 0* following
 *e//2 # floor(z) = z.real when z is complex, so
 ) # e//2 (floor division) gives z.real/2 (complex floor division only works in Python 2)
 *1j**p # rotate depending on the 2^0 and 2^1 bits. i**x is cyclic every 4
 +p/8 # translate horizontally (real component) by p>>3 (mod this later)
 +p/8j/w # translate vertically (im component) by p>>3 / w
 )%w%(1j*h) # mod to within grid (complex mods only work in Python 2)
 for e in l # find where each cell e of the given polyomino goes
 for p in c # do this for each c (each set of position+orientation integers)
 }
 )
 for c in product( # iterate c over all sets of w*h/len(l) integers from 0 to 8*L-1
 range(8*w*h) # each of these 8*L integers corresponds to a single position/orientation of a polyomino
 # Bits 2^0 and 2^1 give the rotation, and 2^2 gives the reflection
 # The higher bits give the position from 0 to L=w*h-1 ==> (0,0) to (w-1,h-1)
 ,repeat=w*h/len(l) # w*h/len(l) is the number of polyominos needed since len(l) is the number of cells per polyomino
 # can't switch to *[range(8*w*h)]*(w*h/len(l)) because Python 3 does not allow short complex operations as above
 )
 )
from itertools import*
lambda l,w,h:all(w*h-len({((e-(p&4)*e//2)*1j**p+p/8+p/8/w*1j)%w%(1j*h)for e in l for p in c})for c in combinations(range(8*w*h),w*h/len(l)))
from itertools import*

Try it online with many testcases. Since this brute-forces, I have commented out a few cases to run fast enough for TIO.

lambda l,w,h:
 all( # we want any configuration to work, but De Morgan gives any(L==len) <==> not all(L!=len) <==> not all(L-len)
 w*h-len( # if two polyominos in a configuration overlap, then there are duplicate cells
 # so the length of the set is less
 { # create a set consisting of each possible position+orientation of L/len(l) polyominos:
 ( # here, e is a single cell of the given polyomino
 ( # reflect e across the imaginary axis if p >= 4 (mod 8)
 e- # e-e.real*2 = e-e//.5 reflects across the Im axis
 p&4 # Only reflect if the 2^2 bit is nonzero: gives 4* or 0* following
 *e//2 # floor(z) = z.real when z is complex, so
 ) # e//2 (floor division) gives z.real/2 (complex floor division only works in Python 2)
 *1j**p # rotate depending on the 2^0 and 2^1 bits. i**x is cyclic every 4
 +p/8 # translate horizontally (real component) by p>>3 (mod this later)
 +p/8/w*1j # translate vertically (im component) by p>>3 / w
 )%w%(1j*h) # mod to within grid (complex mods only work in Python 2)
 for e in l # find where each cell e of the given polyomino goes
 for p in c # do this for each c (each set of position+orientation integers)
 }
 )
 for c in combinations( # iterate c over all sets of w*h/len(l) distinct integers from 0 to 8*L-1
 range(8*w*h) # each of these 8*L integers corresponds to a single position/orientation of a polyomino
 # Bits 2^0 and 2^1 give the rotation, and 2^2 gives the reflection
 # The higher bits give the position from 0 to L=w*h-1 ==> (0,0) to (w-1,h-1)
 ,w*h/len(l) # w*h/len(l) is the number of polyominos needed since len(l) is the number of cells per polyomino
 # can't switch to *[range(8*w*h)]*(w*h/len(l)) because Python 3 does not allow short complex operations as above
 )
 )
from itertools import*
-4 bytes
Source Link
fireflame241
  • 16.4k
  • 2
  • 31
  • 74

Python 2, (削除) 300 (削除ここまで) (削除) 265 (削除ここまで) 167163 bytes

-98102 bytes following encouragement + general suggestions by @user202729

from itertools import*
lambda l,w,h:all(w*h-len({((e-(p&4)*e//2)*1j**p+p/8%w+p8+p/88j/w*1jw)%w%(1j*h)for e in l for p in c})for c in product(range(8*w*h),repeat=w*h/len(l)))
from itertools import*

Try it online Try it online with many testcases. Since this brute-forces, I have commented out a few cases to run fast enough for TIO.

lambda l,w,h:
 all( # we want any configuration to work, but De Morgan gives any(L==len) <==> not all(L!=len) <==> not all(L-len)
 Lw*h-len( # if two polyominos in a configuration overlap, then there are duplicate cells
 # so the length of the set is less
 { # create a set consisting of each possible position+orientation of L/len(l) polyominos:
 ( # here, e is a single cell of the given polyomino
 ( # reflect e across the imaginary axis if p >= 4 (mod 8)
 e- # e-e.real*2 = e-e//.5 reflects across the Im axis
 p&4 # Only reflect if the 2^2 bit is nonzero: gives 4* or 0* following
 *e//2 # floor(z) = z.real when z is complex, so
 ) # e//2 (floor division) gives z.real/2 (complex floor division only works in Python 2)
 *1j**p # rotate depending on the 2^0 and 2^1 bits. i**x is cyclic every 4
 +p/8%w8 # translate horizontally (real component) by p>>3 %(mod wthis later)
 +p/88j/w*1jw # translate vertically (im component) by p>>3 / w
 )%w%(1j*h) # mod to within grid (complex mods only work in Python 2)
 for e in l # find where each cell e of the given polyomino goes
 for p in c # do this for each c (each set of position+orientation integers)
 }
 )
 for c in product( # iterate c over all sets of Lw*h/len(l) integers from 0 to 8*L-1
 range(8*L8*w*h) # each of these 8*L integers corresponds to a single position/orientation of a polyomino
 # Bits 2^0 and 2^1 give the rotation, and 2^2 gives the reflection
 # The higher bits give the position from 0 to L=w*h-1 ==> (0,0) to (w-1,h-1)
 ,repeat=Lrepeat=w*h/len(l) # Lw*h/len(l) is the number of polyominos needed since len(l) is the number of cells per polyomino
 # can't switch to *[range(8*L8*w*h)]*(Lw*h/len(l)) because Python 3 does not allow short complex operations as above
 )
 )
from itertools import*

Python 2, (削除) 300 (削除ここまで) (削除) 265 (削除ここまで) 167 bytes

-98 bytes following encouragement + general suggestions by @user202729

from itertools import*
lambda l,w,h:all(w*h-len({((e-(p&4)*e//2)*1j**p+p/8%w+p/8/w*1j)%w%(1j*h)for e in l for p in c})for c in product(range(8*w*h),repeat=w*h/len(l)))
from itertools import*

Try it online with many testcases. Since this brute-forces, I have commented out a few cases to run fast enough for TIO.

lambda l,w,h:
 all( # we want any configuration to work, but De Morgan gives any(L==len) <==> not all(L!=len) <==> not all(L-len)
 L-len( # if two polyominos in a configuration overlap, then there are duplicate cells
 # so the length of the set is less
 { # create a set consisting of each possible position+orientation of L/len(l) polyominos:
 ( # here, e is a single cell of the given polyomino
 ( # reflect e across the imaginary axis if p >= 4 (mod 8)
 e- # e-e.real*2 = e-e//.5 reflects across the Im axis
 p&4 # Only reflect if the 2^2 bit is nonzero: gives 4* or 0* following
 *e//2 # floor(z) = z.real when z is complex, so
 ) # e//2 (floor division) gives z.real/2 (complex floor division only works in Python 2)
 *1j**p # rotate depending on the 2^0 and 2^1 bits. i**x is cyclic every 4
 +p/8%w # translate horizontally (real component) by p>>3 % w
 +p/8/w*1j # translate vertically (im component) by p>>3 / w
 )%w%(1j*h) # mod to within grid (complex mods only work in Python 2)
 for e in l # find where each cell e of the given polyomino goes
 for p in c # do this for each c (each set of position+orientation integers)
 }
 )
 for c in product( # iterate c over all sets of L/len(l) integers from 0 to 8*L-1
 range(8*L) # each of these 8*L integers corresponds to a single position/orientation of a polyomino
 # Bits 2^0 and 2^1 give the rotation, and 2^2 gives the reflection
 # The higher bits give the position from 0 to L=w*h-1 ==> (0,0) to (w-1,h-1)
 ,repeat=L/len(l) # L/len(l) is the number of polyominos needed since len(l) is the number of cells per polyomino
 # can't switch to *[range(8*L)]*(L/len(l)) because Python 3 does not allow short complex operations as above
 )
 )
from itertools import*

Python 2, (削除) 300 (削除ここまで) (削除) 265 (削除ここまで) 163 bytes

-102 bytes following encouragement + general suggestions by @user202729

lambda l,w,h:all(w*h-len({((e-(p&4)*e//2)*1j**p+p/8+p/8j/w)%w%(1j*h)for e in l for p in c})for c in product(range(8*w*h),repeat=w*h/len(l)))
from itertools import*

Try it online with many testcases. Since this brute-forces, I have commented out a few cases to run fast enough for TIO.

lambda l,w,h:
 all( # we want any configuration to work, but De Morgan gives any(L==len) <==> not all(L!=len) <==> not all(L-len)
 w*h-len( # if two polyominos in a configuration overlap, then there are duplicate cells
 # so the length of the set is less
 { # create a set consisting of each possible position+orientation of L/len(l) polyominos:
 ( # here, e is a single cell of the given polyomino
 ( # reflect e across the imaginary axis if p >= 4 (mod 8)
 e- # e-e.real*2 = e-e//.5 reflects across the Im axis
 p&4 # Only reflect if the 2^2 bit is nonzero: gives 4* or 0* following
 *e//2 # floor(z) = z.real when z is complex, so
 ) # e//2 (floor division) gives z.real/2 (complex floor division only works in Python 2)
 *1j**p # rotate depending on the 2^0 and 2^1 bits. i**x is cyclic every 4
 +p/8 # translate horizontally (real component) by p>>3 (mod this later)
 +p/8j/w # translate vertically (im component) by p>>3 / w
 )%w%(1j*h) # mod to within grid (complex mods only work in Python 2)
 for e in l # find where each cell e of the given polyomino goes
 for p in c # do this for each c (each set of position+orientation integers)
 }
 )
 for c in product( # iterate c over all sets of w*h/len(l) integers from 0 to 8*L-1
 range(8*w*h) # each of these 8*L integers corresponds to a single position/orientation of a polyomino
 # Bits 2^0 and 2^1 give the rotation, and 2^2 gives the reflection
 # The higher bits give the position from 0 to L=w*h-1 ==> (0,0) to (w-1,h-1)
 ,repeat=w*h/len(l) # w*h/len(l) is the number of polyominos needed since len(l) is the number of cells per polyomino
 # can't switch to *[range(8*w*h)]*(w*h/len(l)) because Python 3 does not allow short complex operations as above
 )
 )
from itertools import*
-98 bytes (!)
Source Link
fireflame241
  • 16.4k
  • 2
  • 31
  • 74
Loading
-35 bytes
Source Link
fireflame241
  • 16.4k
  • 2
  • 31
  • 74
Loading
Source Link
fireflame241
  • 16.4k
  • 2
  • 31
  • 74
Loading

AltStyle によって変換されたページ (->オリジナル) /