積み木の水槽

```
# 積み木の水槽
# http://qiita.com/Nabetani/items/936e7885f4c607472060
# http://nabetani.sakura.ne.jp/hena/ord13blocktup/
#
def tank_full(blocks_str)

	blocks = blocks_str.split("").map{ |b| b.to_i }
	water_level = blocks
	
	begin
		water_level_org = water_level
		water_level = tank_fill(water_level)
	end while water_level != water_level_org
	
	return count_water(blocks, water_level)
	
end

def tank_fill(water_level)

	water_level_next = water_level.clone

	water_level.each_with_index { |lv, idx|
	
		next if lv == 0
		
		l_lv = water_level[0...idx].reverse.find(->{0}) { |t_lv|
			t_lv != lv
		}
		
		next unless l_lv > lv
		
		r_lv = water_level[(idx+1)..-1].find(->{0}) { |t_lv|
			t_lv != lv
		}
		
		next unless r_lv > lv
		
		water_level_next[idx] = [l_lv, r_lv].min
	}
	
	return water_level_next
	
end

def count_water(blocks, water_level)
	water_level.zip(blocks).inject(0) { |sum, pair|
		sum + pair[0] - pair[1]
	}
end

def test(input, expected)
	result = tank_full(input).to_s
	print (result == expected) ? "." : "E"
end

test( "83141310145169154671122", "24" )                              # 0
test( "923111128", "45" )                                            # 1
test( "923101128", "1" )                                             # 2
test( "903111128", "9" )                                             # 3
test( "3", "0" )                                                     # 4
test( "31", "0" )                                                    # 5
test( "412", "1" )                                                   # 6
test( "3124", "3" )                                                  # 7
test( "11111", "0" )                                                 # 8
test( "222111", "0" )                                                # 9
test( "335544", "0" )                                                # 10
test( "1223455321", "0" )                                            # 11
test( "000", "0" )                                                   # 12
test( "000100020003121", "1" )                                       # 13
test( "1213141516171819181716151413121", "56" )                      # 14
test( "712131415161718191817161514131216", "117" )                   # 15
test( "712131405161718191817161514031216", "64" )                    # 16
test( "03205301204342100", "1" )                                     # 17
test( "0912830485711120342", "18" )                                  # 18
test( "1113241120998943327631001", "20" )                            # 19
test( "7688167781598943035023813337019904732", "41" )                # 20
test( "2032075902729233234129146823006063388", "79" )                # 21
test( "8323636570846582397534533", "44" )                            # 22
test( "2142555257761672319599209190604843", "41" )                   # 23
test( "06424633785085474133925235", "51" )                           # 24
test( "503144400846933212134", "21" )                                # 25
test( "1204706243676306476295999864", "21" )                         # 26
test( "050527640248767717738306306596466224", "29" )                 # 27
test( "5926294098216193922825", "65" )                               # 28
test( "655589141599534035", "29" )                                   # 29
test( "7411279689677738", "34" )                                     # 30
test( "268131111165754619136819109839402", "102" )                   # 31

```