Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
var xᅠ = 37;
var x = 41;
var ᅠx = 43;
if(xᅠ== 37 && x == 41 && ᅠx == 43) {
document.writeln("The truth is out there!")
} else {
document.writeln("<a href='https://goo.gl/yoX4Az'>WAT?</a>")
}
ruby -e "32.times{|l|puts (0..78).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*''}"
"jonas.elfstrom+connect@e-man.se".replaceAll("(.*)([+].*)(@.*)","$1$2");
@jonelf
jonelf / roulette.rb
Last active November 14, 2015 18:42
prng = Random.new
Trials = 37000
lost = 0
Trials.times do
bad_results = 0
250.times do
result = prng.rand(1..37)
if result <= 19
bad_results += 1
else
@jonelf
jonelf / dodips.rb
Created September 24, 2015 15:44
Number of IPs owned by DoD
require 'ipaddr'
sum = 0
File.open("us-military-dod.csv").each_line do |line|
ip1, ip2 = line.split(/;/)
sum += IPAddr.new(ip2).to_i - IPAddr.new(ip1).to_i
end
puts sum
@jonelf
jonelf / weeknumbers.java
Last active September 15, 2015 11:59
Week numbers are hard.
// Week numbers are hard. Here's a bug I found earlier today.
// If you want to construct a string like this
String yearWeekKey = year + week;
// then you can't use
String year = new SimpleDateFormat("yyyy").format(date);
String week = String.format("%02d", new LocalDate(date).getWeekOfWeekyear());
// when date is 2016-01-01 because the key will be "201653" and that week does not exist.
// Instead you have to use .weekyear() because that correctly returns 2015 for 2016-01-01.
String year = new DateTime(ets).weekyear().getAsString();
@jonelf
jonelf / rc4.rb
Last active April 29, 2016 07:58
Code golfing of the cipher RC4 in Ruby, 140 characters.
def e(k,t);s=*((j=q=w=0)..l=256);k*=l;(0..l).map{|i|j+=s[i]+k[i];s[i],s[j%=l]=s[j%=l],s[i]};(0...t.size).map{|i|q+=1;w=(w+s[q%=l])%l;s[q],s[w]=s[w],s[q];t[i]=t[i]^s[(s[q]+s[w])%l]};t;end
key = "test".chars.map(&:ord)
text = "test".chars.map(&:ord)
e(key, text) == [218, 234, 84, 101]
# e=->k,t{k*=l=256;j=q=w=0;s=*0..l;l.times{|i|j+=s[i]+k[i];s[i],s[j]=s[j%=l],s[i]};t.map{|c|w+=s[q+=1];s[q],s[w]=s[w%=l],s[q];c^s[(s[q]+s[w])%l]}}
e=->k,t{j=q=w=0;s=*0..l=256;l.times{|i|j+=s[i]+k[i];s[i],s[j]=s[j%=l],s[i]};t.map{|c|w+=s[q+=1];s[q],s[w]=s[w%=l],s[q];c^s[(s[q]+s[w])%l]}}
@jonelf
jonelf / random_fizzbuzz.rb
Last active October 3, 2017 21:32
A "random" FizzBuzz in Ruby. This is just for fun.
c = ('a'..'z').to_a
srand(1792579 * 1800)
f = (0..3).map { c[rand(26)] }
b = (0..3).map { c[rand(26)] }
a = [f, b].map {|s| s.join.capitalize }
srand(s = 93113 * 19243)
puts (0..99).map{|i| [a[0], a[0] + a[(srand(s) if i%15 < 1) || 1].to_s, a[1], i+1][rand(4)] }
/* \u002a\u002f\u0063\u006c\u0061\u0073\u0073\u0020\u0048\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0076\u0029\u007b\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u0028\u0022\u0048\u0065\u006a\u0020\u0076\u00e4\u0072\u006c\u0064\u0065\u006e\u0021\u0022\u0029\u003b\u007d\u007d\u002f\u002a */
@jonelf
jonelf / andersterapi.rb
Last active August 29, 2015 14:19
Anders terapispråk som en Ruby DSL.
class Numeric
def ≫(n)
n.replace self.to_s
end
end
class String
def ≫(s)
s.replace self
end
def +(s)