def AND x, *y [[x, *y], [x, x]] end def OR x, y, z = nil w = z || x [[y, y], [w, w], [x, y, *z]] end q = [ [1, 0] + [?_] * 6 + [?a], [?R], *"bcdefghy".chars.map {|x| [?_] * 8 + [x] }, *"abcdefgh".chars.map {|x| [x.upcase, x, x] }, [?z, ?g, ?H], # ==> *"abcdef".chars.flat_map {|x| AND(?z, x.upcase) }, *(3..22).flat_map {|i| n = "%08b" % (i * (i+1) / 2) a, *r = "abcdefgh".chars.map.with_index {|x, j| n[j] == ?1 ? x : x.upcase } [ *r.flat_map.with_index {|w, j| j == 0 ? AND(?y, a, w) : AND(?y, w) }[0..-2], *OR(?z, ?y)[1..-1] # remove two !y ] }, [0, 0, 1, 1, 0, 0, 0, ?z], [?R] + [?_] * 7 + [?a], [?R] ] print q.map{|x| x.join ?\s }.join ?\n ##########################################################################################