Skip to content

Instantly share code, notes, and snippets.

@kunitoo
Created July 17, 2018 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunitoo/0cb7b1a57b2475ccd60dd2a38ec6c7aa to your computer and use it in GitHub Desktop.
Save kunitoo/0cb7b1a57b2475ccd60dd2a38ec6c7aa to your computer and use it in GitHub Desktop.
積み木の水槽 〜 横へな 2013.9.6 の回答 http://nabetani.sakura.ne.jp/hena/ord13blocktup/
require 'bundler'
Bundler.require
def solve(input)
nums = input.chars.map(&:to_i)
width = nums.size
hight = nums.max
res = width.times.map { Array.new(hight) }
fill_block(res, nums)
fill_side_blank(res, nums)
reverse(res)
res = res.transpose
# ブロックと必ず水が溜らない箇所
fill(res, nums)
res.flatten.count {|r| r == 'w' }.to_s
end
def fill_block(res, nums)
nums.each.with_index do |v, i|
if v == 0
res[i].fill('x')
else
res[i].fill('b', 0...v)
end
end
end
def fill_side_blank(res, nums)
res[0].fill('x', nums[0])
res[-1].fill('x', nums[-1])
end
def reverse(res)
res.each do |r|
r.reverse!
end
end
def fill(res, nums)
res.each do |r|
s = {index: nil, kind: nil}
r.each.with_index do |v, i|
if s[:kind] == 'b'
if v == 'b'
r.fill('w', s[:index]...i)
s[:index] = nil
s[:kind] = nil
elsif v == 'x'
r.fill('x', s[:index]...i)
s[:index] = nil
s[:kind] = nil
end
elsif s[:kind] == 'x'
if v
r.fill('x', s[:index]...i)
s[:index] = nil
s[:kind] = nil
end
end
if s[:kind] == nil
s[:index] = i + 1
s[:kind] = v
end
end
end
end
TEST_DATA = <<~EOS
/*0*/ test( "83141310145169154671122", "24" );
/*6*/ test( "412", "1" );
/*1*/ test( "923111128", "45" );
/*2*/ test( "923101128", "1" );
/*3*/ test( "903111128", "9" );
/*4*/ test( "3", "0" );
/*5*/ test( "31", "0" );
/*7*/ test( "3124", "3" );
/*8*/ test( "11111", "0" );
/*9*/ test( "222111", "0" );
/*10*/ test( "335544", "0" );
/*11*/ test( "1223455321", "0" );
/*12*/ test( "000", "0" );
/*13*/ test( "000100020003121", "1" );
/*14*/ test( "1213141516171819181716151413121", "56" );
/*15*/ test( "712131415161718191817161514131216", "117" );
/*16*/ test( "712131405161718191817161514031216", "64" );
/*17*/ test( "03205301204342100", "1" );
/*18*/ test( "0912830485711120342", "18" );
/*19*/ test( "1113241120998943327631001", "20" );
/*20*/ test( "7688167781598943035023813337019904732", "41" );
/*21*/ test( "2032075902729233234129146823006063388", "79" );
/*22*/ test( "8323636570846582397534533", "44" );
/*23*/ test( "2142555257761672319599209190604843", "41" );
/*24*/ test( "06424633785085474133925235", "51" );
/*25*/ test( "503144400846933212134", "21" );
/*26*/ test( "1204706243676306476295999864", "21" );
/*27*/ test( "050527640248767717738306306596466224", "29" );
/*28*/ test( "5926294098216193922825", "65" );
/*29*/ test( "655589141599534035", "29" );
/*30*/ test( "7411279689677738", "34" );
/*31*/ test( "268131111165754619136819109839402", "102" );
EOS
Minitest::Reporters.use!(Minitest::Reporters::ProgressReporter.new)
describe 'Doukaku' do
def self.test_order; :sorted; end
TEST_DATA.each_line do |test|
number, input, expected = test.scan(/(\d+).*"(.*)", "(.*)"/)[0]
it "##{number}" do
assert_equal expected, solve(input)
end
end
end
source 'https://rubygems.org'
gem 'activesupport', require: 'active_support/all'
gem 'minitest', require: 'minitest/autorun'
gem 'minitest-reporters'
gem 'awesome_print'
gem 'tapp'
gem 'pry'
gem 'pry-byebug'
gem 'pry-rescue', require: 'pry-rescue/minitest'
gem 'pry-stack_explorer'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment