Skip to content

Instantly share code, notes, and snippets.

@mugi-uno
Last active July 15, 2018 13:25
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 mugi-uno/6d1a3990de7695cd71e24db32330acac to your computer and use it in GitHub Desktop.
Save mugi-uno/6d1a3990de7695cd71e24db32330acac to your computer and use it in GitHub Desktop.
2018/7/14 Toyama.rb #30 オフラインリアルタイムどう書く
# http://nabetani.sakura.ne.jp/hena/ord18notfork/
require 'minitest/unit'
require 'minitest/autorun'
class Regi
POWER = [
2, 7, 3, 5, 2
]
def initialize(input)
@input = input
@regi = [
[], [], [], [], []
]
end
def self.call(input)
Regi.new(input).run
end
def run
@input.chars do |c|
if c == '.'
func
else
# add to shortest list
@regi[shortest_index].push(c == 'x' ? c : c.to_i)
end
end
@regi.map { |r| sum_waiting(r)}.join(',')
end
def func
# 力の限り処理させる
@regi.map!.with_index do |r, index|
pw = POWER[index]
new_r = []
r.each do |n|
if n == 'x'
new_r.push(n)
pw = 0 # もう無理です
next
end
if n <= pw
pw = pw - n
else
new_r.push(n - pw)
pw = 0
end
end
new_r
end
end
def shortest_index
min = Float::INFINITY
index = 0
@regi.each_with_index do |r, i|
cnt = sum_waiting(r)
if cnt < min
index = i
min = cnt
end
end
index
end
def sum_waiting(list)
list.map { |n| n == 'x' ? 1 : n }.sum
end
end
class TestRegi < MiniTest::Unit::TestCase
def test_call
assert_equal('1,0,0,0,0', Regi.call('1'))
assert_equal('9,9,7,0,0', Regi.call('997'))
assert_equal('1,0,0,0,0', Regi.call('x'))
assert_equal('1,0,0,0,0', Regi.call('31.'))
assert_equal('0,0,0,0,0', Regi.call('.'))
assert_equal('1,9,0,0,0', Regi.call('3.9'))
assert_equal('6,6,0,0,0', Regi.call('832.6'))
assert_equal('3,5,6,8,0', Regi.call('5.568'))
assert_equal('4,8,0,0,0', Regi.call('475..48'))
assert_equal('1,4,6,9,0', Regi.call('7.2..469'))
assert_equal('1,1,0,0,0', Regi.call('3x.'))
assert_equal('0,4,2,0,0', Regi.call('42873x.3.'))
assert_equal('9,9,6,6,9', Regi.call('99569x'))
assert_equal('9,9,9,9,9', Regi.call('99569x33'))
assert_equal('7,2,6,4,7', Regi.call('99569x33.'))
assert_equal('5,0,4,0,5', Regi.call('99569x33..'))
assert_equal('4,0,3,2,3', Regi.call('12345x3333.'))
assert_equal('3,0,3,0,4', Regi.call('54321x3333.'))
assert_equal('3,4,4,0,4', Regi.call('51423x3333.'))
assert_equal('1,0,1,0,2', Regi.call('12x34x.'))
assert_equal('7,6,4,10,5', Regi.call('987x654x.32'))
assert_equal('20,10,12,5,20', Regi.call('99999999999x99999999.......9.'))
assert_equal('3,3,1,7,1', Regi.call('574x315.3'))
assert_equal('10,9,5,4,1', Regi.call('5.2893.x98'))
assert_equal('2,1,4,1,1', Regi.call('279.6xxx..4'))
assert_equal('7,1,0,0,0', Regi.call('1.1.39..93.x'))
assert_equal('16,12,17,18,12', Regi.call('7677749325927'))
assert_equal('7,2,0,0,0', Regi.call('x6235.87.56.9.'))
assert_equal('0,0,3,0,0', Regi.call('4.1168.6.197.6.'))
assert_equal('6,2,0,0,0', Regi.call('2.8.547.25..19.6'))
assert_equal('5,0,5,0,7', Regi.call('5.3x82x32.1829..'))
assert_equal('1,0,0,0,7', Regi.call('x.1816..36.24.429.'))
assert_equal('1,0,2,1,1', Regi.call('79.2.6.81x..26x31.1'))
assert_equal('14,13,10,15,14', Regi.call('574296x6538984..5974'))
assert_equal('5,6,0,0,3', Regi.call('99.6244.4376636..72.6'))
assert_equal('17,16,16,18,17', Regi.call('1659.486x5637168278123'))
assert_equal('14,0,3,5,8', Regi.call('5.17797.x626x5x9457.3.'))
assert_equal('1,1,0,0,0', Regi.call('..58624.85623..4.7..23.x'))
assert_equal('7,3,5,8,1', Regi.call('716.463.9.x.8..4.15.738x4'))
assert_equal('10,11,18,12,9', Regi.call('22xx.191.96469472.7232377.'))
assert_equal('2,0,0,0,0', Regi.call('24..4...343......4.41.6...2'))
assert_equal('7,5,7,8,5', Regi.call('32732.474x153.866..4x29.2573'))
assert_equal('4,4,8,4,10', Regi.call('786.1267x9937.17.15448.1x33.4'))
assert_equal('13,16,13,10,12', Regi.call('671714849.149.686852.178.895x3'))
assert_equal('7,11,8,8,10', Regi.call('86x.47.517..29621.61x937..xx935'))
assert_equal('4,6,10,8,8', Regi.call('2233.78x.94.x59511.5.86x3.x714.'))
assert_equal('8,11,8,6,9', Regi.call('793...218.687x415x13.1...x58576x'))
assert_equal('15,13,15,12,15', Regi.call('6.6x37.3x51x932.72x4x33.9363.x7761'))
assert_equal('6,7,8,6,5', Regi.call('6..4.x187..681.2x.2.713276.669x.252'))
assert_equal('19,17,19,20,17', Regi.call('6.xx64..5146x897231.x.21265392x9775'))
assert_equal('14,14,12,16,10', Regi.call('334.85413.263314.x.6293921x3.6357647x'))
assert_equal('12,10,13,6,10', Regi.call('4.1..9..513.266..5999769852.2.38x79.x7'))
end
end
# http://nabetani.sakura.ne.jp/hena/ord13blocktup/
require 'minitest/unit'
require 'minitest/autorun'
class Water
def initialize(input)
@input = input
end
def self.call(input)
Water.new(input).run
end
def run
# 最大数の高さを得る
max = @input.chars.map {|n| n.to_i}.max
# 全ブロックを配列に突っ込む
# 両端は死んだ列にする
@area = @input.chars.map do | char |
n = char.to_i
arr = Array.new(max, 'E')
n.times.each do |index|
arr[index] = 'B'
end
arr
end
@area = @area.unshift(Array.new(max, 'D')).push(Array.new(max, 'D'))
# 列ごとの生存状態
# 1列目がEMPTYの場合は、その列は死んでる
status = Array.new(@area.size, 'LIVE')
status[0] = 'DEATH'
status[status.length - 1] = 'DEATH'
@area.each_with_index do |cols, index|
if cols.first == 'E'
status[index] = 'DEATH'
end
end
# 1行ごとチェック
max.times.each do |row_id|
# 死んでる列はもう強制的に殺す
status.each_with_index do |s, idx|
if status[idx] == 'DEATH' && @area[row_id][idx] == 'E'
@area[row_id][idx] = 'D'
# BLOCKにぶつかるまで隣接EMPTYは全部死ぬ
((idx - 1)..0).times.each do |n|
end
((idx + 1)..(@area.size - 1)).times.each do |n|
end
end
end
# EMPTYかつ隣接列が死んでる場合、道連れで死ぬ
@area.each_with_index do |cols, col_id|
next if col_id == 0 || col_id == @area.size - 1
p "#{cols} / #{row_id} / #{col_id} / #{status}"
if cols[row_id] == 'E' && (status[col_id - 1] == 'DEATH' || status[col_id + 1] == 'DEATH')
status[col_id] = 'DEATH'
cols[row_id] = 'D'
end
end
end
# @area.each_with_index do |cols, col_index|
# # col_index -> 0が一番左の列
# cols.each ||
# end
p @area
# 残ったEMPTYの数を返す
@area.map { |cols| cols.map { |c| c == 'E' ? 1 : 0 }.sum }.sum.to_s
end
end
class TestWater < MiniTest::Unit::TestCase
def test_call
# assert_equal("24", Water.call("83141310145169154671122")) # 0
# assert_equal("45", Water.call("923111128")) # 1
# assert_equal("1", Water.call("923101128")) # 2
# assert_equal("9", Water.call("903111128")) # 3
assert_equal("0", Water.call("3")) # 4
assert_equal("0", Water.call("31")) # 5
# assert_equal("1", Water.call("412")) # 6
# assert_equal("3", Water.call("3124")) # 7
# assert_equal("0", Water.call("11111")) # 8
# assert_equal("0", Water.call("222111")) # 9
# assert_equal("0", Water.call("335544")) # 10
# assert_equal("0", Water.call("1223455321")) # 11
# assert_equal("0", Water.call("000")) # 12
# assert_equal("1", Water.call("000100020003121")) # 13
# assert_equal("56", Water.call("1213141516171819181716151413121")) # 14
# assert_equal("117", Water.call("712131415161718191817161514131216")) # 15
# assert_equal("64", Water.call("712131405161718191817161514031216")) # 16
# assert_equal("1", Water.call("03205301204342100")) # 17
# assert_equal("18", Water.call("0912830485711120342")) # 18
# assert_equal("20", Water.call("1113241120998943327631001")) # 19
# assert_equal("41", Water.call("7688167781598943035023813337019904732")) # 20
# assert_equal("79", Water.call("2032075902729233234129146823006063388")) # 21
# assert_equal("44", Water.call("8323636570846582397534533")) # 22
# assert_equal("41", Water.call("2142555257761672319599209190604843")) # 23
# assert_equal("51", Water.call("06424633785085474133925235")) # 24
# assert_equal("21", Water.call("503144400846933212134")) # 25
# assert_equal("21", Water.call("1204706243676306476295999864")) # 26
# assert_equal("29", Water.call("050527640248767717738306306596466224")) # 27
# assert_equal("65", Water.call("5926294098216193922825")) # 28
# assert_equal("29", Water.call("655589141599534035")) # 29
# assert_equal("34", Water.call("7411279689677738")) # 30
# assert_equal("102", Water.call("268131111165754619136819109839402")) # 31
end
end
# http://nabetani.sakura.ne.jp/hena/ord13blocktup/
require 'minitest/unit'
require 'minitest/autorun'
class Water
def initialize(input)
@input = input
end
def self.call(input)
Water.new(input).run
end
def run
# 最大数の高さを得る
max = @input.chars.map {|n| n.to_i}.max
# 全ブロックを配列に突っ込む
# 両端は死んだ列にする
@area = @input.chars.map do | char |
n = char.to_i
arr = Array.new(max, 'E')
n.times.each do |index|
arr[index] = 'B'
end
arr
end
@area = @area.unshift(Array.new(max, 'D')).push(Array.new(max, 'D'))
# 列ごとの生存状態
# 1列目がEMPTYの場合は、その列は死んでる
@status = Array.new(@area.size, 'LIVE')
@status[0] = 'DEATH'
@status[@status.length - 1] = 'DEATH'
@area.each_with_index do |cols, index|
if cols.first == 'E'
@status[index] = 'DEATH'
end
end
# 1行ごとチェック
max.times.each do |row_id|
# 死んでる列はもう強制的に殺す
@status.each_with_index do |s, idx|
if @status[idx] == 'DEATH' && @area[idx][row_id] == 'E'
fill_line(idx, row_id)
end
end
@area.each_with_index do |cols, col_id|
next if col_id == 0 || col_id == @area.size - 1
if cols[row_id] == 'E' && (@status[col_id - 1] == 'DEATH' || @status[col_id + 1] == 'DEATH')
@status[col_id] = 'DEATH'
fill_line(col_id, row_id)
end
end
end
# 残ったEMPTYの数を返す
@area.map { |cols| cols.map { |c| c == 'E' ? 1 : 0 }.sum }.sum.to_s
end
# 指定箇所を死んだマスとし、そこに隣接する空白箇所も道連れにする
def fill_line(col_id, row_id)
@area[col_id][row_id] = 'D'
# BLOCKにぶつかるまで隣接EMPTYは全部死ぬ
(0..(col_id - 1)).to_a.reverse.each do |n|
break if @area[n][row_id] == 'B'
@area[n][row_id] = 'D'
@status[n] = 'DEATH'
end
((col_id + 1)..(@area.size - 1)).each do |n|
break if @area[n][row_id] == 'B'
@area[n][row_id] = 'D'
@status[n] = 'DEATH'
end
end
end
class TestWater < MiniTest::Unit::TestCase
def test_call
assert_equal("24", Water.call("83141310145169154671122")) # 0
assert_equal("45", Water.call("923111128")) # 1
assert_equal("1", Water.call("923101128")) # 2
assert_equal("9", Water.call("903111128")) # 3
assert_equal("0", Water.call("3")) # 4
assert_equal("0", Water.call("31")) # 5
assert_equal("1", Water.call("412")) # 6
assert_equal("3", Water.call("3124")) # 7
assert_equal("0", Water.call("11111")) # 8
assert_equal("0", Water.call("222111")) # 9
assert_equal("0", Water.call("335544")) # 10
assert_equal("0", Water.call("1223455321")) # 11
assert_equal("0", Water.call("000")) # 12
assert_equal("1", Water.call("000100020003121")) # 13
assert_equal("56", Water.call("1213141516171819181716151413121")) # 14
assert_equal("117", Water.call("712131415161718191817161514131216")) # 15
assert_equal("64", Water.call("712131405161718191817161514031216")) # 16
assert_equal("1", Water.call("03205301204342100")) # 17
assert_equal("18", Water.call("0912830485711120342")) # 18
assert_equal("20", Water.call("1113241120998943327631001")) # 19
assert_equal("41", Water.call("7688167781598943035023813337019904732")) # 20
assert_equal("79", Water.call("2032075902729233234129146823006063388")) # 21
assert_equal("44", Water.call("8323636570846582397534533")) # 22
assert_equal("41", Water.call("2142555257761672319599209190604843")) # 23
assert_equal("51", Water.call("06424633785085474133925235")) # 24
assert_equal("21", Water.call("503144400846933212134")) # 25
assert_equal("21", Water.call("1204706243676306476295999864")) # 26
assert_equal("29", Water.call("050527640248767717738306306596466224")) # 27
assert_equal("65", Water.call("5926294098216193922825")) # 28
assert_equal("29", Water.call("655589141599534035")) # 29
assert_equal("34", Water.call("7411279689677738")) # 30
assert_equal("102", Water.call("268131111165754619136819109839402")) # 31
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment