# \[SOLUTION\] Grid Folding (#63)

**URL:** <https://www.ruby-forum.com/t/solution-grid-folding-63/97667>\
**Category:** Ruby\
**Created:** [January 26, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/solution-grid-folding-63/97667 "2006-01-26T16:45:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 26, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/solution-grid-folding-63/97667/1 "2006-01-26T16:45:11Z")

</div>

I wonder for 4x4 the total solution is 16 \* 4! = 384

My solution about check\_fold is try to find all combinations possible.

Below is modified version of my check\_fold, it will print out all  
possible combinations and total count number.  
However I found for 4x4 there is only 96 possible not 384.  
Can someone tell me that I am wrong ( missing some combination ) or  
the previous math formula is not correct ?

Thanks,

def check\_fold(row, col, result)

# find all combinations with binary 0 for row and 1 for column

operation  
def all\_orders(r, c) #  
return [2**c - 1] if (r \<= 0) # c bits of 1 is 2**c-1  
return [0] if (c \<= 0) # r bits of 0 is 0  
table = []  
all\_orders(r-1,c).each { |t| table \<\< ((t \<\< 1) + 0) }  
all\_orders(r,c-1).each { |t| table \<\< ((t \<\< 1) + 1) }  
table  
end  
=begin  
if row \<= 0 ||  
col \<= 0 ||  
row \* col != result.size ||  
2 \*\* (Math.log(row)/Math.log(2)).to\_i != row ||  
2 \*\* (Math.log(col)/Math.log(2)).to\_i != col  
raise “Error: Parameters are not correct.”  
end  
=end  
r = Integer(Math.log(row) / Math.log(2))  
c = Integer(Math.log(col) / Math.log(2))  
all\_rc\_orders = all\_orders(r,c)  
count = 0

row.times do |tb\_operation|  
col.times do |lr\_operation|  
all\_rc\_orders.each do |order|  
operations = ‘’  
tb\_op = tb\_operation  
lr\_op = lr\_operation  
(r+c).times do  
if (order & 1 == 0)  
operations += (tb\_op & 1 == 0) ? ‘T’ : ‘B’  
tb\_op \>\>= 1  
else  
operations += (lr\_op & 1 == 0) ? ‘L’ : ‘R’  
lr\_op \>\>= 1  
end  
order \>\>= 1  
end  
puts operations  
count += 1  
# return operations if fold(row, col, operations) == result  
end  
end  
end  
p count  
end

check\_fold(4,4, nil)

#==================================#  
The output:

TTLL  
TLTL  
TLLT  
LTTL  
LTLT  
LLTT  
TTRL  
TRTL  
TRLT  
RTTL  
RTLT  
RLTT  
TTLR  
TLTR  
TLRT  
LTTR  
LTRT  
LRTT  
TTRR  
TRTR  
TRRT  
RTTR  
RTRT  
RRTT  
BTLL  
BLTL  
BLLT  
LBTL  
LBLT  
LLBT  
BTRL  
BRTL  
BRLT  
RBTL  
RBLT  
RLBT  
BTLR  
BLTR  
BLRT  
LBTR  
LBRT  
LRBT  
BTRR  
BRTR  
BRRT  
RBTR  
RBRT  
RRBT  
TBLL  
TLBL  
TLLB  
LTBL  
LTLB  
LLTB  
TBRL  
TRBL  
TRLB  
RTBL  
RTLB  
RLTB  
TBLR  
TLBR  
TLRB  
LTBR  
LTRB  
LRTB  
TBRR  
TRBR  
TRRB  
RTBR  
RTRB  
RRTB  
BBLL  
BLBL  
BLLB  
LBBL  
LBLB  
LLBB  
BBRL  
BRBL  
BRLB  
RBBL  
RBLB  
RLBB  
BBLR  
BLBR  
BLRB  
LBBR  
LBRB  
LRBB  
BBRR  
BRBR  
BRRB  
RBBR  
RBRB  
RRBB  
96

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 26, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/solution-grid-folding-63/97667/2 "2006-01-26T17:15:27Z")

</div>

Actually, you are right, that my math was wrong. (All this talk about  
loving math and here I am messing it up…)

Some of my 4! permutations overlapped with possible folds… That is,  
“RR” backwards is still “RR”, etc.

So really, rather than 4! permutations, I have to not permute the  
change the relative order of [LR] and [TB] folds. So a 4x4 would be  
16 \* 6 == 96 possible complete folds.

You are correct, sir.
