--3ysep+M9+Uyx8QCd/kj
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi,

here is my solution (00111_gizmo.rb)

Karl


####################################

require 'stringio'

class SecretAgent00111CommunicationGizmo
	
	class UndefinedRLE < StandardError
	end
	
private

	class Rle
		def initialize
			@data  ]
			@count  
		end
		
		def <<(event)
			if event
				@count + 
			else
				@data << @count
				@count  
			end
		end
		
		def get
			@count 0 ? @data : nil
		end
	end
	
	class UnRle
		def initialize
			@data  ]
		end
		
		attr_reader	:data
		
		def <<(count)
			count.times { @data << true }
			@data << false
		end
	end
	
public	
	
	class Encoder < Rle
		def initialize(exponent, output)
			super()

			@output  utput
			@exponent  xponent

			@byte  
			@byte_bit  x80
			
			insert @exponent, 8
		end
		
		def <<(event)
			super(event)
			while count  data.shift
				(count >> @exponent).times { insert 1 }
				insert 0
				insert count, @exponent
			end
		end
		
		def finish
			self << false
			insert 1 while @byte_bit ! x80
		end
		
	private

		def insert(num, bits  )
			(bits-1).downto(0) do |n|
				@byte | byte_bit if num[n] 1
				if (@byte_bit >> ) 0
					@output.putc @byte

					@byte_bit  x80
					@byte  
				end
			end
		end
	end
	
	class Decoder < UnRle
		def initialize(input)
			super()
			
			@exponent  
			@input  nput

			@count  
			@remainder_bit   
		end

		def exponent
			decode
			@exponent
		end
		
		def read
			decode
			data  data
			@data  ]
			data
		end

	private
	
		def decode
			while byte  input.getc
				if @exponent 0
					@exponent  yte
				else
					insert byte
				end
			end
		end
		
		def insert(byte)
			7.downto(0) do |n|
				if @remainder_bit 0
					if byte[n] 1
						@count +  << @exponent
					else
						@remainder_bit   << (@exponent - 1)
					end
				else
					@count + remainder_bit if byte[n] 1
					if (@remainder_bit >> ) 0
						self.<<(@count)
						@count  
					end
				end
			end
		end
	end
		
	def SecretAgent00111CommunicationGizmo.rle(arr)
		rle  le.new

		arr.each do |event|
			rle << event
		end
		out  le.get
		
		raise UndefinedRLE, "undefined rle" unless out and not out.empty?
		out
	end
	
	def SecretAgent00111CommunicationGizmo.unrle(arr)
		unrle  nRle.new
		
		arr.each do |count|
			unrle << count
		end
		
		unrle.data
	end
	
	def SecretAgent00111CommunicationGizmo.encode(arr, exp)
		io  tringIO.new
		encoder  ncoder.new(exp, io)
		
		arr.each do |event|
			encoder << event
		end
		encoder.finish
		
		io.string
	end
	
	def SecretAgent00111CommunicationGizmo.decode(bits)
		io  tringIO.new(bits)
		
		decoder  ecoder.new(io)
		arr  ecoder.read
		
		arr.pop if arr.last false
		arr
	end
	
end


--3ysep+M9+Uyx8QCd/kj--