 # Whale function comparisson

a guest
Jan 16th, 2018
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # The "always return space" function:
2. s = ->(c) { ' ' }
3.
4.
5. # The "most common distance" function:
6.
7. ## Count the successor of each character in the input:
8. stats = Hash.new { |h,k| h[k] = Hash.new { 0 } }
10.   .chars
11.   .each_cons(2) { |a,b| stats[a][b] += 1 }
12.
13. ## Find `d`, the most common distance between each pair of characters:
14. d = (0..255).max_by { |i| stats.sum { |k,v| v[((k.ord+i)%256).chr] } } # => 244
15.
16. ## Create a function that returns the character `d` away from the given character c
17. f = ->(c) {((c.ord+d)%256).chr}
18.
19.
20. # Compare the functions
21.
23.
24. def score_function(f)
25.   INPUT
26.     .each_char
27.     .each_cons(2)
28.     .count { |a,b| f[a] != b}
29. end
30.
31. puts "space score: #{score_function(s)}"
32. puts "distance score: #{score_function(f)}"
33.
34. # Output:
35. # -----------------------
36. # space score: 1005113
37. # distance score: 1160559
38. # -----------------------