|
|
|
def decompress(input)
|
|
|
|
output = ''
|
|
|
|
ss = StringScanner.new(input)
|
|
|
|
until ss.eos?
|
|
|
|
if ss.scan(/(.*?)\((\d+)x(\d+)\)/)
|
|
|
|
output << ss[1]
|
|
|
|
|
|
|
|
times = ss[3].to_i
|
|
|
|
seq = ss.scan(/.{#{ss[2].to_i}}/)
|
|
|
|
|
|
|
|
output << (seq * times)
|
|
|
|
else
|
|
|
|
output << ss.scan(/.*/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
|
|
|
def decompressed_length(input)
|
|
|
|
length = 0
|
|
|
|
ss = StringScanner.new(input)
|
|
|
|
until ss.eos?
|
|
|
|
case
|
|
|
|
when ss.scan(/\((\d+)x(\d+)\)/)
|
|
|
|
times = ss[2].to_i
|
|
|
|
seq = ss.scan(/.{#{ss[1].to_i}}/)
|
|
|
|
length += times * decompressed_length(seq)
|
|
|
|
when ss.scan(/[^(]+/)
|
|
|
|
length += ss.matched.length
|
|
|
|
else
|
|
|
|
raise 'omg!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
length
|
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
puts decompressed_length(DATA.read.chomp)
|
|
|
|
end
|
|
|
|
|
|
|
|
require 'minitest'
|
|
|
|
# require 'minitest/autorun'
|
|
|
|
|
|
|
|
class TestDecompress < Minitest::Test
|
|
|
|
def test_decompress
|
|
|
|
assert_equal 'ADVENT', decompress('ADVENT')
|
|
|
|
assert_equal 'ABBBBBC', decompress('A(1x5)BC')
|
|
|
|
assert_equal 'XYZXYZXYZ', decompress('(3x3)XYZ')
|
|
|
|
assert_equal 'ABCBCDEFEFG', decompress('A(2x2)BCD(2x2)EFG')
|
|
|
|
assert_equal '(1x3)A', decompress('(6x1)(1x3)A')
|
|
|
|
assert_equal 'X(3x3)ABC(3x3)ABCY', decompress('X(8x2)(3x3)ABCY')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_decompressed_length
|
|
|
|
assert_equal 9, decompressed_length('(3x3)XYZ')
|
|
|
|
assert_equal 20, decompressed_length('X(8x2)(3x3)ABCY')
|
|
|
|
assert_equal 241920, decompressed_length('(27x12)(20x12)(13x14)(7x10)(1x12)A')
|
|
|
|
input = '(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN'
|
|
|
|
assert_equal 445, decompressed_length(input)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
__END__
|
|
|
|
(6x6)AFPLBX(2x3)ZE(53x13)(4x7)ZGQO(2x4)NJ(1x8)M(24x11)(18x7)HMLOASMJNGZHMCEVEX(11x2)(6x6)TRDPQX(157x12)(60x12)(2x3)SB(7x1)FBSAUFK(4x1)NMZK(10x2)RLIIHFEKRY(10x6)GMDUIQDOZJ(71x8)(1x11)N(19x5)EHNAZSRTFYXOZNFTOOQ(6x4)XKARLK(13x1)IRATYZQMJXHAH(4x8)VFUJ(8x6)AOOEHHBF(27x6)(11x9)(5x15)KTTHB(5x6)AELEZ(17x10)AOGNMNTGDJDYEMMEH(7x8)BOIOHRF(6x2)ZKXONV(172x8)(6x5)(1x8)V(17x6)WEVFHKUDETBOZVKIO(2x14)ZI(75x9)(5x5)EXKDZ(4x4)BOGK(10x5)SXYSSOACJR(3x15)FHO(24x10)YBZPMZIPUCEIYNGYAUPVFOKB(43x3)(13x2)MERJWKSXBIEKP(2x4)CR(4x5)DFDQ(2x15)ZM(89x5)(43x6)(16x8)RIGCVPPOSBRJSJCB(2x9)XI(1x11)M(2x8)QT(8x6)(3x8)XVC(20x15)SBWQFWQWPQHXZXDWSXIR(4393x15)(25x5)TEAACSAHWYWPYJZEPDIHUMIPB(1026x1)(1x7)K(768x7)(388x3)(113x9)(70x11)(12x10)HDEPKSMDLHAX(2x5)RU(29x13)BIOENKVYNENIFNCSWNZIJGCZXBUVF(3x1)JRO(30x2)(11x9)NCTAXULXPBY(1x5)K(2x7)WQ(76x10)(13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