Skip to content

Instantly share code, notes, and snippets.

@Nakilon
Nakilon / 111.cr
Created September 7, 2015 08:33
Crystal "almost" solution to Project Euler 111
macro break_if_enough divisible, divisor, to_return = nil
q, r = {{divisible}}.divmod {{divisor}}
{% if to_return %}
return false if r == 0
return true if q <= {{divisor}}
{% else %}
break false if r == 0
break true if q <= {{divisor}}
{% end %}
end
@Nakilon
Nakilon / prime.cr
Last active September 7, 2015 00:27
prime? method (in fact a lambda) implemented in Crystal
macro break_if_enough divisible, divisor, to_return = nil
q, r = {{divisible}}.divmod {{divisor}}
{% if to_return %}
return false if r == 0
return true if q <= {{divisor}}
{% else %}
break false if r == 0
break true if q <= {{divisor}}
{% end %}
end
a=*$<.map{|s|s.scan /[a-z]+/i}
a[0]+=x=[i=0]
a.map{n=*x
a.map{|b|(b&x)[0]&&n+=b}
exit p i if(["Erdos"]&x=n)[0]
i+=1}
puts"Inf"
@Nakilon
Nakilon / 2048.rb
Last active July 26, 2023 22:15
2048 golfed to 379 chars in Ruby by Nakilon for http://www.reddit.com/r/tinycode/comments/22wozn/
require"io/console"
_=[[_]*4]*4
t=->{_=_.transpose}
r=->{_.map! &:reverse}
f=->{_=_.map{|_|_=(_*?\s).gsub(/\b(\d+) +\1\b/){|_|_.to_i*2}.split;_[3]||=p;_}}
f[]
loop{(0while _[i=rand(4)][j=rand(4)];_[i][j]=[*[?2]*9,?4].sample)if$n!=_*?_
system"clear"
_.map{|_|puts"%5s"*4%_}
(c=STDIN.getch)[?q]|0