Skip to content

Instantly share code, notes, and snippets.

@yancya
Last active April 8, 2016 02:44
Show Gist options
  • Save yancya/996427014df75b8f8f1a7a92d6d48790 to your computer and use it in GitHub Desktop.
Save yancya/996427014df75b8f8f1a7a92d6d48790 to your computer and use it in GitHub Desktop.
class Yhpg
class Polygon
def initialize(s)
@layer, @chr = s.chars
end
def neighbors
prev_chr = [*'a'..'t'].cycle.take(21).each_cons(2).map(&:reverse).to_h[@chr]
next_chr = [*'a'..'t'].cycle.take(21).each_cons(2).to_h[@chr]
prev_layer = @layer.to_i - 1
next_layer = @layer.to_i + 1
case @layer
when "0"
[*'a'..'t'].map { |c| "1#{c}" }
when "1"
[
"00",
"#{@layer}#{prev_chr}", "#{@layer}#{next_chr}",
"#{next_layer}#{prev_chr}", "#{next_layer}#{@chr}"
]
when "2", "3"
[
"#{prev_layer}#{@chr}", "#{prev_layer}#{next_chr}",
"#{@layer}#{prev_chr}", "#{@layer}#{next_chr}",
"#{next_layer}#{prev_chr}", "#{next_layer}#{@chr}"
]
else # "4"
[
"#{prev_layer}#{@chr}", "#{prev_layer}#{next_chr}",
"#{@layer}#{prev_chr}", "#{@layer}#{next_chr}"
]
end
end
end
def initialize(materials)
@materials = materials.chars.each_slice(2).map(&:join)
end
def result
@materials.map { |m|
a = (Polygon.new(m).neighbors & @materials)
neighbor_chunk =
a.
map { |c| [c, *(Polygon.new(c).neighbors & a)].sort }.
uniq.
reduce([]) { |a, e|
related = -> (g) { (g & e).size.nonzero? }
a.reject(&related).push(a.select(&related).flatten.push(*e).uniq)
}
case m
when "00"
num = neighbor_chunk.
map { |a2| a2.size.to_i + 1 }.
reduce(:+)
20 - [20, num.to_i].min
else
num = neighbor_chunk.
map { |a2| a2.size.to_i + (a2.include?("00") ? 2 : 1) }.
reduce(:+)
6 - [6, num.to_i].min
end
}.reduce(:+).to_s
end
end
require 'test/unit'
class YhpgTest < Test::Unit::TestCase
data(
'*00*' => ["1a2t3s2s", "11" ],
'*01*' => ["1a1c1d00", "22" ],
'*02*' => ["00", "20" ],
'*03*' => ["3q", "6" ],
'*04*' => ["3t2a", "8" ],
'*05*' => ["3t3a", "8" ],
'*06*' => ["3t4a", "12" ],
'*07*' => ["004q2g", "32" ],
'*08*' => ["4c2g2k4i", "24" ],
'*09*' => ["1o1a4f4i1t", "26" ],
'*10*' => ["4t3a4g2a2o2p", "24" ],
'*11*' => ["4i4o3i3c3n3h2c", "30" ],
'*12*' => ["4m3n3m002b1b3a", "34" ],
'*13*' => ["001b2a3t4s3s2s1s", "27" ],
'*14*' => ["1n1j3o4o1h2n2r1k", "36" ],
'*15*' => ["4o2a2j1m2e4l2l3m3o", "42" ],
'*16*' => ["1j2p1a4r4b1i3h4e3i2i", "42" ],
'*17*' => ["001a1c1e1g1i1k1m1o1q1s", "30" ],
'*18*' => ["3n2j3e3a2n1f1p2q3t4t3h", "53" ],
'*19*' => ["4a4b4c3d2e1e1d1c3a2a1b", "22" ],
'*20*' => ["3n3e3t4i3m2d2g3i1j4o2i4t", "52" ],
'*21*' => ["3t2m4n2l4g4h1a1n2t4m2h3a1m", "44" ],
'*22*' => ["1p1i2n3q1d2o2c1q3m3f003k3l2s", "53" ],
'*23*' => ["3r1q3p2d4k4n1r3o4l2j2c1a4o3q4f", "56" ],
'*24*' => ["4d2f4r3f2p4t1j1p4g1q4f1k2j2s4i4j", "62" ],
'*25*' => ["002c2d1f2f3e4d4c4b4a3a1b2t1t2a2g1h", "40" ],
'*26*' => ["3r2i2j3d2t3j3g2s1p2o2p1n1m2d1k1r3i", "59" ],
'*27*' => ["4o4s1i4p3p3s3b4n3r1k4a3t4g3n1o2m3i2o", "66" ],
'*28*' => ["3k2j1i2p2n3l2f2s3f1n4s2h3s1l1m4n4k4q2k", "65" ],
'*29*' => ["1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1r1s1t", "40" ],
'*30*' => ["4n3f1c1a3o2s1h2p2k3g3n2e4s2t1j1b3h2a1n3k", "73" ],
'*31*' => ["1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1r1s1t00", "20" ],
'*32*' => ["2a2b2c2d2e2f2g2h2i2j2k2l2m2n2o2p2q2r2s2t00", "60" ],
'*33*' => ["2m1p2c2o2n4n002s1m3i2t4l2b3r2h1j4q1c4t1s1a", "65" ],
'*34*' => ["4m2t4r4h3b4b2e3g3n2i4e3m1q4i2q2b2m3i2a1b2s1h", "77" ],
'*35*' => ["1c004g1k4o3p3l3h1r4d2t2c2d1n4t2e1s1j2p1d4j1h1f", "74" ],
'*36*' => ["1s3j4a4h3h3q3n3b3f2m3o3c4i3r2r1f1c2p4s1e3a2j2o3e", "80" ],
'*37*' => ["2c1b3b3k2f1e4q1d1m4n3t4b4s3h3d3g3n1f4p4j2e4f4c3e1k", "78" ],
'*38*' => ["2p4k3t1h4e1n3g4p2j1a1k1p4f1o3a4s4q4i3p2t4l3k2k3s2r4h", "77" ],
'*39*' => ["1d1p4o3n3m4d1m2f3c1o3f3g3a2o1f4n2c4e2j4p4f1b1j1i1k1h2m", "74" ],
'*40*' => ["3m4d4e1i3t4f1f3n2p2g1q4g2c2m1s2r2i3f3o1h3g2e1o3a4r3h3r4o", "75" ],
)
test "Case" do |(actual, expected)|
yhpg = Yhpg.new(actual)
assert { yhpg.result == expected }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment