View difference between Paste ID: qCL8uBWe and tenQxcyb
SHOW: | | - or go back to the newest paste.
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 } }
9
File.read('whale.txt')
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-
p (0..255).max_by { |i| stats.sum { |k,v| v[((k.ord+i)%256).chr] } } # => d = 244
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+244)%256).chr}
17+
f = ->(c) {((c.ord+d)%256).chr}
18
19
20
# Compare the functions
21
22
INPUT = File.read('whale2.txt')
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)}"
32+
puts "distance score: #{score_function(f)}"
33
34
# Output:
35
# -----------------------
36
# space score: 1005113
37
# distance score: 1160559
38
# -----------------------