Snippets

cia_rana トリオミノの分類 2017.3.22 問題

Created by cia_rana last modified
$patterns = {
  ?J=>[[2,2],[1,5,6]],
  ?L=>[[2,2],[0,5,6]],
  ?T=>[[2,2],[0,1,6]],
  ?R=>[[2,2],[0,1,5]],
  ?I=>[[1,3],[0,5,10]],
  ?B=>[[3,1],[0,1,2]],
}

def solve(input)
  match_pattern(input.chars.uniq.map{|e|(e.ord-?a.ord)}.sort)
end

def match_pattern(inputs)
  $patterns.each{|name, (size, pos)|
    next unless pos.size == inputs.size
    (0..(5-size[0])).each{|x|
      (0..(5-size[1])).each{|y|
        return name if (0...pos.size).all?{|i|
          y * 5 + x + pos[i] == inputs[i]
        }
      }
    }
  }
  ?-
end

def assert_equal(no, expected, actual)
  "#{no}: #{expected == actual ? ?o : ?x}"
end

DATA.each{|data|
  no, input, expected = data.chomp.match(/\/\*(\d+)\*\/ test\(\s*\"(.+)\",\s*\"(.+)\"\s*\)/)[1..3]
  puts assert_equal(no, expected, solve(input))
}

__END__
/*0*/ test( "cba", "B" );    
/*1*/ test( "yam", "-" );    
/*2*/ test( "aaa", "-" );    
/*3*/ test( "def", "-" );    
/*4*/ test( "gga", "-" );    
/*5*/ test( "bbf", "-" );    
/*6*/ test( "gmh", "T" );    
/*7*/ test( "mhn", "L" );    
/*8*/ test( "dea", "-" );    
/*9*/ test( "mrn", "R" );    
/*10*/ test( "hcm", "I" );    
/*11*/ test( "mno", "B" );    
/*12*/ test( "snr", "J" );    
/*13*/ test( "xnn", "-" );    
/*14*/ test( "nnl", "-" );    
/*15*/ test( "kop", "-" );    
/*16*/ test( "ejd", "T" );    
/*17*/ test( "txy", "J" );    
/*18*/ test( "pvu", "L" );    
/*19*/ test( "baf", "R" );    
/*20*/ test( "hhc", "-" );    
/*21*/ test( "ono", "-" );    
/*22*/ test( "wxv", "B" );    
/*23*/ test( "bdc", "B" );    
/*24*/ test( "ojt", "I" );    
/*25*/ test( "fkp", "I" );

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.