Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit of my various packing scripts.

  • Loading branch information...
commit 45528fefed39542e44903ad0122041786adcf852 1 parent 8ee5a8a
@torsten authored
View
66 torsten/pack_bin.rb
@@ -0,0 +1,66 @@
+# Packs the ASCII art into a hex or base-36 number.
+# See progress.rb for the unpacking code.
+#
+# Originally written by Torsten Becker <torsten.becker@gmail.com> in 2012.
+
+
+HD = false
+
+if HD
+ msg = "\
+.-----.--.--.----.--.--.| |--.-----.
+| -__| | | _| | || <| _ |
+|_____|_____|__| |_____||__|__|_____|
+"
+ chars = " -_|.<\n"
+ bin = ['000', '001', '010', '011', '100', '101', '110', '111']
+
+else
+ msg = "\
+ _____ __ __ ____ __ __ | |__ _____
+| -__| | | _| | || -| _ |
+|_____|_____|__| |_____||__|__|_____|
+"
+ chars = " -_|"
+ bin = ['00', '01', '10', '11']
+end
+
+best = [99]
+
+chars.chars.to_a.permutation.each do |perm|
+ num = ''
+ msg.chars.reverse_each do |chr|
+ case chr
+ when perm[0]
+ num += bin[0]
+ when perm[1]
+ num += bin[1]
+ when perm[2]
+ num += bin[2]
+ when perm[3]
+ num += bin[3]
+ when perm[4]
+ num += bin[4]
+ when perm[5]
+ num += bin[5]
+ when perm[6]
+ num += bin[6]
+ end
+ end
+ n = num.to_i(2)
+
+ # ber = [n].pack'w'
+ # puts "#{ber.length} 0x#{n.to_s 16} #{perm.join.inspect}"
+ # puts "#{ber} #{perm.join.inspect} #{n}"
+
+ # hex = n.to_s 16
+ # puts "#{hex.length} 0x#{hex} #{perm.join.inspect}"
+
+ hex = n.to_s 36
+
+ if hex.length <= best[0]
+ best = [hex.length, hex, perm.join]
+ puts best.inspect
+ end
+
+end
View
145 torsten/pack_floats.rb
@@ -0,0 +1,145 @@
+# Packs the ASCII art using naiive float-based arithmetic compression
+# with equal size buckets (so that the unpacker is less code).
+# See #unzip and progress.rb for the unpacking code.
+#
+# Originally written by Torsten Becker <torsten.becker@gmail.com> in 2012.
+
+
+msg="\
+ ___ _ _ ___ _ _| |_ ___
+| -_| | | _| | | -_| - |
+|___|___|_| |___|_|_|___|
+"
+
+
+# Unpacking routine (floats)
+# a,b,c: float
+# perm: string
+# top: int
+# bottom: float
+def unzip(a, b, c, perm, top, bottom)
+ [a,b,c].map{|f|
+ t,b,out=top,bottom,''
+ 25.times{
+ s=t-b
+ i=(f-b)*4/s
+ b+=s/4*i.to_i
+ t=b+s/4
+ out += perm[i].chr
+ }
+ out + "\n"
+ }.join
+end
+
+# puts( unzip(4.01666285319154,9.7874952316723,8.03138113220488,16,0.0,4) == msg)
+
+
+def zipped_lines msg, perm, initial_top, initial_bottom
+ msg.lines.map do |line|
+ i = 0
+
+ top = initial_top.to_f
+ bottom = initial_bottom.to_f
+
+ line.chars.each do |chr|
+ scale = (top - bottom)
+ # puts "i: #{i}, chr: '#{chr}', scale: #{scale}"
+ i += 1
+ case chr
+ when perm[0].chr
+ # bottom = bottom + scale * 0.0
+ top = bottom + scale * 0.25
+ when perm[1].chr
+ bottom = bottom + scale * 0.25
+ top = bottom + scale * 0.25
+ when perm[2].chr
+ bottom = bottom + scale * 0.5
+ top = bottom + scale * 0.25
+ when perm[3].chr
+ bottom = bottom + scale * 0.75
+ # top = bottom + scale * 0.25
+ end
+ # puts "%.80f"%top, "%.80f"%bottom, ""
+ # puts bottom + 0.5 * (top - bottom)
+ # puts "(#{bottom}, #{top}) = #{(bottom + 0.5 * (top - bottom))}"
+ end
+
+ bottom + 0.5 * (top - bottom)
+ end
+end
+
+
+def each_rounding num
+ yield num
+ yield num[0..-2]
+ i = num[-2].chr.to_i
+ if i != 9
+ yield num[0..-3] + (i + 1).to_s
+ elsif i != 0
+ yield num[0..-3] + (i - 1).to_s
+ else
+ yield num[0..-3]
+ end
+end
+
+
+def each_cropping(stringified)
+ a, b, c = stringified.split(', ')
+ a = a[1..-1]
+ c = c[0..-2]
+
+ each_rounding(a) do |var_a|
+ each_rounding(b) do |var_b|
+ each_rounding(c) do |var_c|
+ yield "[#{var_a}, #{var_b}, #{var_c}]"
+ end
+ end
+ end
+end
+
+
+results = []
+shortest = [52]
+
+permutations = '_ |-'.chars.to_a.permutation.map{|x|x.join}
+
+((0.0)..(9.9)).step(0.1) do |bottom|
+ ((bottom.floor+1)..30).each do |top|
+ permutations.each do |perm|
+ # puts "top: #{top}, bottom: #{bottom}"
+
+ params = zipped_lines(msg, perm, top, bottom)
+ stringified = params.inspect
+
+ each_cropping(stringified) do |stringi|
+ begin
+ a, b, c = eval(stringi)
+
+ result = unzip(a, b, c, perm, top, bottom)
+ if result == msg
+ score = stringi.length + top.to_s.length
+
+ result_tuple = [score, stringi, perm, top, bottom]
+ results << result_tuple
+
+ if score <= shortest[0]
+ shortest = result_tuple
+ puts shortest.inspect
+ end
+ end
+
+ rescue NoMethodError, FloatDomainError
+ end
+ end
+ end
+ end
+
+ puts bottom
+end
+
+puts "---"
+
+# Show top 10 results
+puts( (results.sort do |a, b|
+ (a[0]) <=> (b[0])
+end)[0..10].map{|x|x.inspect})
View
116 torsten/pack_int.rb
@@ -0,0 +1,116 @@
+# Packs the ASCII art using a naiive integer-based arithmetic compression
+# with equal size buckets (so that the unpacker is less code).
+# See #unzip and progress.rb for the unpacking code.
+#
+# Originally written by Torsten Becker <torsten.becker@gmail.com> in 2012.
+
+
+HD = false
+
+if HD
+ msg = "\
+ _____ __ __ ____ __ __ | |__ _____
+| -__| | | _| | || -| _ |
+|_____|_____|__| |_____||__|__|_____|
+"
+else
+ msg = "\
+ ___ _ _ ___ _ _| |_ ___
+| -_| | | _| | | -_| - |
+|___|___|_| |___|_|_|___|
+"
+end
+
+
+# Unpacker for inters
+def unzip(a, b, c, perm, top, bottom)
+ [a,b,c].map{|f|
+ t,b,out=top,bottom,''
+ 25.times{
+ s=t-b
+ i=((f-b)*4)/s
+ b=b+s/4*i
+ t=b+s/4
+ out+=perm[i].chr
+ }
+ out + "\n"
+ }.join
+end
+
+
+def zipped_lines msg, perm, initial_top, initial_bottom
+ msg.lines.map do |line|
+ i = 0
+
+ top = initial_top
+ bottom = initial_bottom
+
+ line.chars.each do |chr|
+ scale = (top - bottom)
+ # puts "i: #{i}, chr: '#{chr}', scale: #{scale}"
+ i += 1
+ case chr
+ when perm[0].chr
+ # bottom = bottom + scale * 0.0
+ top = bottom + scale / 4
+ when perm[1].chr
+ bottom = bottom + scale / 4
+ top = bottom + scale / 4
+ when perm[2].chr
+ bottom = bottom + scale / 2
+ top = bottom + scale / 4
+ when perm[3].chr
+ bottom = bottom + (scale * 3) / 4
+ # top = bottom + scale * 0.25
+ end
+ # puts "%.80f"%top, "%.80f"%bottom, ""
+ # puts bottom + 0.5 * (top - bottom)
+ # puts "(#{bottom}, #{top}) = #{(bottom + ((top - bottom) / 2))}"
+ end
+
+ bottom + ((top - bottom) / 2)
+ end
+end
+
+
+results = []
+shortest = [99]
+
+
+'_ |-'.chars.to_a.permutation.map{|x|x.join}.each do |perm|
+ (0..9).each do |bottom|
+ (23..39).each do |power|
+ top = 4**power
+
+ params = zipped_lines(msg, perm, top, bottom)
+ stringified = params.inspect
+
+ begin
+ a, b, c = eval(stringified)
+ result = unzip(a, b, c, perm, top, bottom)
+
+ if result == msg
+ score = stringified.length
+
+ result_tuple = [score, stringified, perm, power, bottom]
+ results << result_tuple
+
+ if score <= shortest[0]
+ shortest = result_tuple
+ puts shortest.inspect
+ end
+ end
+
+ rescue NoMethodError, FloatDomainError, ZeroDivisionError
+ end
+
+ end
+ end
+end
+
+puts "---"
+
+
+puts( (results.sort do |a, b|
+ a[0] <=> b[0]
+end)[0..10].map{|x|x.inspect})
View
49 torsten/pack_lines.rb
@@ -0,0 +1,49 @@
+# Packs each line of the ASCII art into a numer.
+# See progress.rb for the unpacking code.
+#
+# Originally written by Torsten Becker <torsten.becker@gmail.com> in 2012.
+
+
+msg = "\
+ _____ __ __ ____ __ __ | |__ _____
+| -__| | | _| | || -| _ |
+|_____|_____|__| |_____||__|__|_____|
+"
+chars = " -_|"
+
+bin = ['00', '01', '10', '11']
+
+chars.chars.to_a.permutation.each do |perm|
+ triplet = []
+
+ msg.lines.each do |line|
+ num = ''
+
+ line.chars.reverse_each do |chr|
+ case chr
+ when perm[0]
+ num += bin[0]
+ when perm[1]
+ num += bin[1]
+ when perm[2]
+ num += bin[2]
+ when perm[3]
+ num += bin[3]
+ when perm[4]
+ num += bin[4]
+ end
+ end
+
+ n = num.to_i(2)
+ hex = "0x#{n.to_s(16)}"
+
+ if hex.length < n.to_s.length
+ triplet << hex
+ else
+ triplet << n.to_s
+ end
+ end
+
+ puts "#{triplet.inspect.length} #{triplet.inspect} #{perm.join.inspect}"
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.