Skip to content

Instantly share code, notes, and snippets.

@tompng
Last active June 2, 2018 15:56
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 tompng/c445259784f99a5bb52d44f9092b450c to your computer and use it in GitHub Desktop.
Save tompng/c445259784f99a5bb52d44f9092b450c to your computer and use it in GitHub Desktop.
s6: whitespace
def self.method_missing name, *args
name.to_s
end
FizzMessage = Fizz()
BuzzMessage = Buzz()
def ​​ fizzbuzz_loop n=100
n = n.to_i
return 0 if n == 0
(1..n).each do |i|
puts fizzbuzz(i)
end
error = get_error_message
puts error if error
end
def   fizzbuzz n
set_error 'wrong argument type' if n.nil?
if n < ​0
n = -n​
retval =​ fizzbuzz n
return ​​​​​ retval
else
if mod3(n) == 0 && mod5(n) == 0
FizzMessage + BuzzMessage
elsif mod3(n) == 0
FizzMessage
elsif mod5(n) == 0
BuzzMessage
else
int2string n
end
end
end
def ​​​​​​ int2string n
if n < ​0
n = -n​
tmp =​​​ int2string n
return​ '-' + tmp
end
base =​ 10
charcode_offset =​​ 0x30
n =​​​​ n.to_i unless n.is_a? Integer
chars = n.digits(​ ​ ​ base ).map do |n|
(n + charcode_offset).chr
end
​​​ chars.reverse.join
end
def ​​​​​ mod3 n
if n < ​0
n = -n​
tmp ​= mod3 n
return​ -tmp
end
return +0 if n % 3 == 0
return +2 if n % 3 == 2 || n % 3 == -1
return ​+1
end
def ​ mod5 n
if n < ​0
n = -n​
tmp = mod5 n
flip =​​​​​ ​​​​​ ​​​​​ -1
tmp * flip
else
n.modulo 5
end
end
def ​​​ set_error msg
if ​!msg
msg = 'UnknownError'
retval ​= set_error msg
return​ retval
end
$ERROR = ​
msg
end
def ​​​​ get_error_message
$ERROR
end
​​​     ​       ​ ​           ​   ​ ​         ​ ​ ​ ​   ​ ​ ​ ​ ​ ​ ​         ​ ​         ​             ​ ​ ​   ​   ​ ​   ​     ​ ​         ​ ​   ​ ​ ​     ​   ​ ​     ​ ​ ​   ​ ​     ​ ​   ​​
​​​       ​           ​ ​ ​           ​     ​     ​   ​ ​     ​ ​   ​   ​ ​       ​ ​ ​     ​ ​   ​ ​   ​ ​     ​ ​ ​       ​       ​       ​   ​ ​     ​   ​ ​   ​     ​   ​ ​ ​   ​ ​ ​     ​ ​   ​​​​ ​​
​​​   ​       ​ ​   ​   ​   ​ ​       ​   ​   ​   ​       ​     ​ ​ ​ ​   ​ ​         ​     ​ ​   ​   ​ ​     ​ ​ ​ ​ ​   ​ ​       ​     ​ ​   ​ ​ ​     ​ ​ ​ ​           ​ ​   ​         ​   ​   ​ ​​​​ ​​
​​​ ​ ​     ​ ​ ​ ​   ​   ​ ​     ​     ​         ​ ​   ​ ​   ​   ​ ​ ​   ​ ​ ​         ​   ​ ​ ​   ​ ​ ​ ​         ​   ​   ​           ​     ​   ​   ​     ​     ​ ​ ​ ​ ​ ​   ​   ​     ​ ​ ​   ​ ​     ​​​​ ​​
​​​​​​   ​       ​     ​ ​   ​   ​   ​ ​ ​ ​ ​ ​   ​ ​         ​         ​   ​     ​   ​   ​ ​ ​ ​ ​     ​     ​     ​ ​   ​ ​     ​ ​   ​ ​         ​ ​   ​   ​       ​ ​ ​ ​       ​ ​       ​   ​ ​   ​​​​ ​​
eval ​​​​'n=100;fizzbuzz_loop(n)'
output_e =(1..100).map{|i|[*[:Ruby][i%3],*[:Trick][i%5],nil,i][0,2]*''}*$/+$/
output_f = output_e.gsub(/Ruby/, 'Fizz').gsub(/Trick/, 'Buzz')
`rm entry.rb`
`ruby whitespace.rb g`
raise unless `ruby entry.rb` == output_e
raise unless `ruby fizzbuzz1.rb` == output_f
raise unless `ruby fizzbuzz2.rb` == output_f
code = File.read 'entry.rb'
bsize = code.bytesize
csize = code.delete(' ').size
raise bsize.to_s unless bsize < 4096
raise csize.to_s unless csize < 2048
`rm fizzbuzz1.rb`
`rm fizzbuzz2.rb`
puts :passed
code = File.read(__FILE__).split('# __ENTRY_CODE__').last.lstrip
sp = 0xa0.chr('utf-8')
zsp = 8203.chr('utf-8')
truecode = "(1..100).map{|i|puts [*[:Ruby][i%3],*[:Trick][i%5],nil,i][0,2]*''}"
num=truecode.chars.reverse.each_with_index.map{|c,i|c.ord*160**i}.sum
ops = num.to_s(2).chars.reverse.map{|c| c=='0' ? '「dbl」' : '「incdbl」' }
visible_size = ->os{os.count('「dbl」')+os.count('「incdbl」')*2}
per = (visible_size[ops]/5.0).ceil
splitted_ops = [[]]
until ops.empty?
op = ops.shift
if visible_size[splitted_ops.last] < per
splitted_ops.last << op
else
splitted_ops << [op]
end
end
opfirst, *ops, oplast = splitted_ops.reverse
encoded = [
['「set」', *opfirst, '「zero」']*' ',
*ops.map{|o| ['「set」', *o, '「get」', '「zero」']*' '},
['「decode」', *oplast, '「get」', '「zero」']*' '
]*$/
code.gsub!('ENCODED_CODE', encoded)
vars = code.scan(/「[^」]+」/).group_by(&:itself).sort_by{|a,b|[b.size,a]}.reverse.map(&:first)
vars.each_with_index do |var, i|
code.gsub! var, i==0 ? sp : zsp*i
end
code.gsub! '□', sp
code.gsub! '■', zsp
if ARGV[0].to_s[0]=='g'
File.write 'entry.rb', code
File.write 'fizzbuzz1.rb', code.tr(sp+zsp, ' ')
File.write 'fizzbuzz2.rb', code.delete(zsp).tr(sp, ' ')
exit
end
# __ENTRY_CODE__
def self.method_missing name, *args
name.to_s
end
FizzMessage = Fizz()
BuzzMessage = Buzz()
def 「zero」 fizzbuzz_loop□n=100
n = n.to_i
return 0 if n == 0
(1..n).each do |i|
puts fizzbuzz(i)
end
error = get_error_message
puts error if error
end
def 「incdbl」 fizzbuzz□n
set_error 'wrong argument type' if n.nil?
if n < ■0
n = -n■
retval =「dbl」 fizzbuzz□n
return 「inc」 retval
else
if mod3(n) == 0 && mod5(n) == 0
FizzMessage + BuzzMessage
elsif mod3(n) == 0
FizzMessage
elsif mod5(n) == 0
BuzzMessage
else
int2string n
end
end
end
def 「decode」 int2string□n
if n < ■0
n = -n■
tmp =「set」 int2string□n
return■ '-' + tmp
end
base =「dbl」 10
charcode_offset =「zero」 0x30
n =「get」 n.to_i unless n.is_a? Integer
chars = n.digits(「dbl」 「dbl」 「dbl」 base ).map do |n|
(n + charcode_offset).chr
end
「set」 chars.reverse.join
end
def 「inc」 mod3□n
if n < ■0
n = -n■
tmp ■= mod3□n
return■ -tmp
end
return +0 if n % 3 == 0
return +2 if n % 3 == 2 || n % 3 == -1
return ■+1
end
def 「dbl」 mod5□n
if n < ■0
n = -n■
tmp = mod5□n
flip =「inc」 「inc」 「inc」 -1
tmp * flip
else
n.modulo 5
end
end
def 「set」 set_error□msg
if ■!msg
msg = 'UnknownError'
retval ■= set_error□msg
return■ retval
end
$ERROR = ■
msg
end
def 「get」 get_error_message
$ERROR
end
ENCODED_CODE
eval 「get」'n=100;fizzbuzz_loop(n)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment