Last active
March 8, 2016 12:53
-
-
Save yancya/37d79e02a91afcfdeed1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Yhpg | |
MAPPING = [*'0'..'9', *'A'..'Z', *'a'..'z'] | |
def initialize(data) | |
n, list = data.split(":").tap { |n, list| break [n.to_i, list.split(",").map { |str| decode(str) }] } | |
x_nominee = list.map { |x, _| x }.map { |x| [x, x + 1] }.flatten.tap { |a| a.push(*[0, 62])}.uniq | |
y_nominee = list.map { |_, y| y }.map { |y| [y, y + 1] }.flatten.tap { |a| a.push(*[0, 62])}.uniq | |
x_range_patterns = x_nominee.combination(2). map { |a| (a.min..a.max) } | |
y_range_patterns = y_nominee.combination(2). map { |a| (a.min..a.max) } | |
squares = x_range_patterns.product(y_range_patterns) | |
targets = squares.select { |xrange, yrange| list.select { |p| check(xrange, yrange, p) }.size == n } | |
(@min, _), (@max, _) = targets.map { |x, y| [(x.max - x.min) * (y.max - y.min), x, y] }.minmax_by(&:first) | |
end | |
def decode(str) | |
str.chars.map { |w| MAPPING.index(w) } | |
end | |
def check(xrange, yrange, target) | |
x, y = target | |
(xrange.include?(x) && xrange.include?(x + 1)) && | |
(yrange.include?(y) && yrange.include?(y + 1)) | |
end | |
def output | |
case res = [@min, @max].join(',') | |
when ',' | |
'-' | |
else | |
res | |
end | |
end | |
end | |
require 'test/unit' | |
class YhpgTest < Test::Unit::TestCase | |
data( | |
'/*00*/' => ["3:Oh,Be,AF,in,eG,ir,l5,Q8,mC,7T,Ty,tT", "108,1920"], | |
'/*01*/' => ["3:00,zz,0z,z0", "-"], | |
'/*02*/' => ["1:ho", "1,3844"], | |
'/*03*/' => ["2:am", "-"], | |
'/*04*/' => ["4:00,zz,0z,z0", "3844,3844"], | |
'/*05*/' => ["4:00,11,zz,yy,1y,y1", "3600,3721"], | |
'/*06*/' => ["2:00,01,10,11,yz,zy,yy,zz", "2,3660"], | |
'/*07*/' => ["2:AA,AB,BA,BB,SZ,SY,TZ,TY", "2,2046"], | |
'/*08*/' => ["3:Lw,qw,cw,Ow,2w,fw,yw,ow,Zw,iw", "7,2170"], | |
'/*09*/' => ["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,xz,z0,z1,0f,P0,zw,80,zC,zB,Az,0P,50,k0,rz,0D,jz,qz,E0", "2,2928"], | |
'/*32*/' => ["2:tz,0F,0y,zo,0K,01,qz,zU,gz,Xz,zc,0m,zD,Q0,Yz,zb,0a,zp,zW,z7,0o,h0,1z,0p", "2,3660"], | |
'/*33*/' => ["5:8r,NI,gL,3z,EK,hy,L9,g2,Kh,Gw,Dg,ZB,Sg,LY,ig,sS,I8,U0,DI,cq,Bu,qJ,C4,jP", "143,1520"], | |
'/*34*/' => ["2:7s,z7,so,zw,X2,59,r1,0Q,70,q2,C6,J6,wz,at,2w,Vq,f9,st,sI,rf,wG,zg,f3,L2,4j", "4,2340"], | |
'/*35*/' => ["2:kw,Gz,zp,se,8e,2S,C7,1A,B9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ZV,Td,Mu,tA,g8,VC,I7,N8,zN,kY,Ux,3t,mg,4m,FO,Ug,vQ,qY,jl,Ne,Zq,GN", "416,1794"], | |
'/*46*/' => ["11:lQ,EN,vO,tn,qO,F3,9k,K2,UC,P0,XY,DB,QO,ps,hy,fl,Dt,ex,Vc,vF,Pf,Vk,uo,Xc,Sh,KE,9g,3H,l6", "658,1995"], | |
) | |
test "case" do |(actual, expect)| | |
yhpg = Yhpg.new(actual) | |
assert { yhpg.output == expect } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment