オフラインリアルタイムどう書くE02 の問題をRubyで解く


問題はこちら
http://nabetani.sakura.ne.jp/hena/orde02pire/

def solve(q)
  pos = {}
  (('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a).each_with_index{|v,i| pos[v] = i}
  sz = pos.size
  n, points = q.split(':')
  n = n.to_i
  ps = []
  points.split(',').each{|e|
    x,y = e.chars.map{|v| pos[v]}
    ps.push([x,y])
  }
  min = sz * sz + 1
  max = -1
  sz.times{|x0|
    sz.times{|y0|
      (x0...sz).each{|x1|
        (y0...sz).each{|y1|
          num = 0
          ps.each{|x,y|
            num+=1 if x >= x0 and x<= x1 and y >= y0 and y <= y1
            break if num > n
          }
          break if num > n
          if num == n
            m = (x1-x0+1)*(y1-y0+1)
            min = m if min > m
            max = m if max < m
          end
        }
      }
    }
  }
  max == -1 ? '-' : [min,max].join(',')
end

DATA.readlines.each do |line|
  no,q,a,_ = line.strip.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans
  puts ans == a ? ' o' : ' x'
end
__END__
0   3:Oh,Be,AF,in,eG,ir,l5,Q8,mC,7T,Ty,tT   108,1920    
1   3:00,zz,0z,z0   -   
2   1:ho    1,3844  
3   2:am    -   
4   4:00,zz,0z,z0   3844,3844   
5   4:00,11,zz,yy,1y,y1 3600,3721   
6   2:00,01,10,11,yz,zy,yy,zz   2,3660  
7   2:AA,AB,BA,BB,SZ,SY,TZ,TY   2,2046  
8   3:Lw,qw,cw,Ow,2w,fw,yw,ow,Zw,iw 7,2170  
9   4:0o,0s,0x,0C,0L,0k,0V,0y,0m,0S 9,2852  
10  5:Tz,gz,Ez,Pz,3z,Jz,iz,Bz,ez,9z 17,2604 
11  6:mQ,mp,mv,mW,mo,mZ,mC,mc,mt,mH 23,3100 
12  5:a0,a4,aa,ac,ag,aB,aG,aK,aO,aY,az  19,2480 
13  10:Ep,Jx,Qh,M7,Zr,lk,yp,Ya,aW,J3,OJ 1938,3720   
14  5:00,01,10,0y,0z,1z,y0,z0,z1,yz,zz,zy   3721,3721   
15  7:00,01,10,0y,0z,1z,y0,z0,z1,yz,zz,zy   -   
16  8:00,01,10,0y,0z,1z,y0,z0,z1,yz,zz,zy,TE    -   
17  11:bU,cl,d5,Fn,NW,gA,2O,YU,H4,0y,qE,Hd,ZH   1748,3658   
18  12:SM,cf,AD,6P,cm,mo,if,s0,Mt,GJ,9m,Sp,lA,x4,NE 1806,3186   
19  2:00,I0,c0,z0,0I,II,cI,zI,0c,Ic,cc,zc,0z,Iz,cz,zz   19,2520 
20  3:00,I0,c0,z0,0I,II,cI,zI,0c,Ic,cc,zc,0z,Iz,cz,zz   39,2562 
21  5:00,I0,c0,z0,0I,II,cI,zI,0c,Ic,cc,zc,0z,Iz,cz,zz   -   
22  6:00,I0,c0,z0,0I,II,cI,zI,0c,Ic,cc,zc,0z,Iz,cz,zz   741,3660    
23  7:00,I0,c0,z0,0I,II,cI,zI,0c,Ic,cc,zc,0z,Iz,cz,zz   -   
24  7:00,I0,c0,z0,0I,II,cJ,zI,0c,Ic,cc,zc,0z,Iz,cz,zz   1178,2623   
25  13:Lk,y3,uO,Gk,sF,7y,ED,FP,rK,vw,Lo,kT,ib,MR,sC,Cu,xQ   1794,3472   
26  1:lz,en,81,M2,M1,8p,7m,ND,m0,gd,3v,hr,hA,Yr,XB,x2,AR,5Q,3V,B3   1,1785  
27  2:s1,Dx,yu,H8,c4,6C,95,FK,xV,Q7,h2,Wk,BI,i0,bl,9Q,KF,1q,52,PS,Jh    6,1824  
28  2:0R,20,fz,0a,0X,zp,P0,0b,0S,zw,e0,0G,g0,06,a0,Zz,zW,zn,00,z6,zq,U0 2,3660  
29  3:H5,jF,AT,HH,k7,Ho,Mz,07,Tt,Sq,Zh,Yt,e5,oS,qf,YY,JD,bP,s4,hB,TC,PW 28,1224 
30  4:oe,pg,Np,zP,ho,pe,OV,S0,oM,wO,pM,Ah,Vq,9d,6U,3I,C9,AR,1L,rg,69,as,Nx  12,1989 
31  2:n0,V0,zL,i0,4z,Nz,x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