1 時間半くらいでした。敗北。
def solve(input)
fold, punch_pos = input.split(?-)
punch_pos = punch_pos.upcase.split("")
h_count = accum_h_count(input)
v_count = accum_v_count(input)
punch(h_count, v_count, last_pos(fold), punch_pos)
end
def accum_v_count(fold)
fold.scan(/R|L/).count
end
def accum_h_count(fold)
fold.scan(/T|B/).count
end
def last_fold(fold)
h = fold.scan(/T|B/).last
v = fold.scan(/R|L/).last
[h, v]
end
def last_pos(fold)
h, v = last_fold(fold)
[toggle(h), toggle(v)]
end
def toggle(str)
case str
when ?R then ?L
when ?L then ?R
when ?T then ?B
when ?B then ?T
end
end
def punch(h_count, v_count, last_pos, punch_pos)
return 0 unless last_pos.first && last_pos.last
tl_based_punch_pos =
case last_pos.join
when 'TL'
punch_pos
when 'TR'
[punch_pos.first, toggle(punch_pos.last)]
when 'BL'
[toggle(punch_pos.first), punch_pos.last]
when 'BR'
[toggle(punch_pos.first), toggle(punch_pos.last)]
end
h_offset, v_offset =
case tl_based_punch_pos.join
when 'TL' then [-1, -1]
when 'TR' then [-1, 0]
when 'BL' then [0, -1]
when 'BR' then [0, 0]
end
h = ((2 ** h_count - 1) / 2.0).ceil + h_offset
v = ((2 ** v_count - 1) / 2.0).ceil + v_offset
h * v
end
DATA.each_line do |l|
input, expect = l.scan(/"(.*)", "(.*)"/).flatten
actual = solve(input).to_s
unless actual == expect
raise "input: #{input}, expected #{expect.inspect}, but got #{actual.inspect}"
end
end
p 'passed! yey!'
__END__
/*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" );