Skip to content

Instantly share code, notes, and snippets.

@supermomonga
Last active August 29, 2015 14:15
Show Gist options
  • Save supermomonga/3096d4c96341c2ce6eeb to your computer and use it in GitHub Desktop.
Save supermomonga/3096d4c96341c2ce6eeb to your computer and use it in GitHub Desktop.
Rubyによる不適切なFizzBuzzの世界 ref: http://qiita.com/supermomonga/items/b8faf7441ada9c310282
__,__,f,i,z,z,__,__,b,u,z,z,__,__=DATA.map(&method(:eval)).map &:size
F = (f* i+z+z+b+u+z+z) *(i**i*i-i**z)/i
I = (f**i+z+z+b+u+z+z) +i**i+i**z
Z = (f**i+z+z+b+u+z+z) +f*i+i
Z
B = (f* i+z+z+b+u+z+z) *(i**i*i-i**z)/i-i**i
U = (f**i+z+z+b+u+z+z) +f*i-i-i**z
Z
Z
((([[f,i,z,z,b,u,z,z].inject(&:*)]*i).inject(&:**))..(f**i+z+z+b+u+z+z)).map{|fizzbuzz|
case [F,i,z,z,B,u,z,z].inject &:*
when fizzbuzz %(f+f/i) then [F,I,Z,Z,B,U,Z,Z]
when fizzbuzz %(i+i**z) then [F,I,Z,Z]
when fizzbuzz %(i**i+i**z) then [B,U,Z,Z]
else (% %s % fizzbuzz).chars.map(&:to_i).map &(f**i/i-i).method(:+)
end.pack 'c*'
}.map &method(:puts)
__END__
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%% %%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%%%%%%%%%% %%%%%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%%%%%% %%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% %%%%%% %%%%%% %%%% %%%% %%%
%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%% %%%%%% %%%%%%%%%% %%%%%%%%%%%% %%%%%
%%%%%%%%%%%%%%%%%%%%%%%% %% %%%% %%%%%% %%%%%%%% %%%%%%%%%%%% %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% %%%%%% %%%% %%%%%% %%%%%% %%%%%%%%%%%% %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%% %%%% %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(1..100).each do |n|
puts case 0
when n % 15 then :FizzBuzz
when n % 3 then :Fizz
when n % 5 then :Buzz
else n
end
end
(1..100).map(
&->_{{0=>:FizzBuzz,3=>:Fizz,5=>:Buzz,6=>:Fizz,9=>:Fizz,10=>:Buzz,12=>:Fizz}.fetch _%15,_}
).map &method(:puts)
[70, 105, 122, 122, 66, 117, 122, 122].pack 'c*'
#=> "FizzBuzz"
eval "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
#=> ""
eval "%% %%%% %%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%"
#=> " "
eval "%% %%%%%%%%%%%%%%%% %%%%%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%"
#=> " "
eval "%% %%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
#=> " "
%% %%%% %%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%
%% %.%(
%%%.%(
%% %.%(
%%%.%(
%%%.%(
%% %.%(
%%%.%(
%%%.%(
%% %.%(
%%%.%(
%%%.%(
%%%.%(
%%%.%(
%%%.%(
%%%.%(%%%)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
%` `.%(
%``.%(
%` `.%(
%``.%(
%``.%(
%` `.%(
%``.%(
%``.%(
%` `.%(
%``.%(
%``.%(
%``.%(
%``.%(
%``.%(
%``.%(%``)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
module Kernel
define_method '0', -> idx, _ { %w`Fizz Buzz FizzBuzz`[idx] }
end
def method_missing n, _ = nil, orig = nil
v, i = [3,5,15].map{|_|n.to_s.to_i % _}.each_with_index.sort_by{|_|[_[0],_[1]*-1]}.first
return _ ? orig : send(v.to_s, i, n)
end
(1..100).map(&:to_s).map(&method(:send)).map(&:to_s).map(&method(:puts))
p <<'PHP_VERSION;'
<?php
PHP_VERSION;
print "\033[1F\033[1M";
//.tap{ define_method :range, -> s,e { s.upto e } }
//.tap{ define_method :array_map, -> f,seq { seq.map{ |x| f.(x) } } }
//.tap{ define_method :function, -> x,&b { -> x { $x=x;b.call } } }
array_map(function($x){
print $x % 15 == 0 ? 'FizzBuzz' : ($x % 3 == 0 ? 'Fizz' : ($x % 5 == 0 ? 'Buzz' : $x));
print "\n";
}, range(1,100));
(1..100).map{|_|
_.tap{|_|
break :FizzBuzz if _ % 15 == 0
break :Fizz if _ % 3 == 0
break :Buzz if _ % 5 == 0
}
}.map &method(:puts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment