横浜へなちょこプログラミング勉強会にて過去に出題された折って切るを解いてみた。
回答にかかった時間は60分を少しオーバー。
class OrigamiFold
def initialize input
order, @cut = input.split "-"
@order = order.chars
@origami = Origami.new
end
def exec
@order.each{|o| @origami.fold o}
@origami.cut @cut
end
end
class Origami
def initialize
@count = 1
@layer = {t: 0, b: 0, l: 0, r: 0}
end
def fold order
case order.to_sym
when :T then @layer[:t], @layer[:b] = calc_layer(@layer[:t], @layer[:b])
when :B then @layer[:b], @layer[:t] = calc_layer(@layer[:t], @layer[:b])
when :L then @layer[:l], @layer[:r] = calc_layer(@layer[:l], @layer[:r])
when :R then @layer[:r], @layer[:l] = calc_layer(@layer[:l], @layer[:r])
end
end
def cut corner
corner.chars.map{|c| @layer[c.to_sym]}.inject(1, &:*)
end
private
def calc_layer f1, f2
[f1 + f2 + 1, f1 + f2]
end
end
test = <<_TEST
/*0*/ test( "RRTRB-bl", "6" );
/*1*/ test( "R-tr", "0" );
/*2*/ test( "L-br", "0" );
/*3*/ test( "T-tl", "0" );
/*4*/ test( "B-tl", "0" );
/*5*/ test( "BL-br", "0" );
/*6*/ test( "LB-tl", "0" );
/*7*/ test( "RL-tl", "0" );
/*8*/ test( "BL-tl", "0" );
/*9*/ test( "TL-bl", "0" );
/*10*/ test( "RT-tr", "1" );
/*11*/ test( "TRB-tl", "0" );
/*12*/ test( "TRL-bl", "0" );
/*13*/ test( "TRB-br", "2" );
/*14*/ test( "LLB-bl", "2" );
/*15*/ test( "RTL-tr", "1" );
/*16*/ test( "LBB-tr", "0" );
/*17*/ test( "TLL-tl", "2" );
/*18*/ test( "RLRR-tr", "0" );
/*19*/ test( "BBTL-tl", "4" );
/*20*/ test( "TBBT-tr", "0" );
/*21*/ test( "LLBR-tl", "0" );
/*22*/ test( "LBRT-tl", "2" );
/*23*/ test( "RLBL-bl", "4" );
/*24*/ test( "BRRL-br", "3" );
/*25*/ test( "TBBTL-tl", "8" );
/*26*/ test( "TLBBT-br", "0" );
/*27*/ test( "LRBLL-br", "7" );
/*28*/ test( "TRRTT-br", "6" );
/*29*/ test( "BBBLB-br", "0" );
/*30*/ test( "RTTTR-tl", "4" );
/*31*/ test( "BBLLL-br", "6" );
/*32*/ test( "RRLLTR-tr", "16" );
/*33*/ test( "TTRBLB-br", "8" );
/*34*/ test( "LRBRBR-bl", "14" );
/*35*/ test( "RBBLRL-tl", "8" );
/*36*/ test( "RTRLTB-tl", "12" );
/*37*/ test( "LBLRTR-tl", "14" );
/*38*/ test( "RRLTRL-tl", "16" );
/*39*/ test( "TBLTRR-br", "12" );
/*40*/ test( "TTTRLTT-bl", "30" );
/*41*/ test( "TBBRTBL-tr", "15" );
/*42*/ test( "TRTRTLL-tr", "28" );
/*43*/ test( "TLLRTRB-tr", "24" );
/*44*/ test( "RLLBRLB-tr", "15" );
/*45*/ test( "LTLRRBT-tr", "32" );
/*46*/ test( "RBBRBLT-br", "21" );
/*47*/ test( "LLRLRLR-tr", "0" );
_TEST
require 'minitest/autorun'
describe 'FoldCut' do
test.split("\n").each do |line|
t, n, input, expect = line.match(/^\/\*(\d+)\*\/\s*test\(\s*"([^"]+)",\s*"([^"]+)"\s*\);\s*$/).to_a
of = OrigamiFold.new input
it input do
assert_equal expect, of.exec.to_s
end
end
end