Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The "always return space" function:
- s = ->(c) { ' ' }
- # The "most common distance" function:
- ## Count the successor of each character in the input:
- stats = Hash.new { |h,k| h[k] = Hash.new { 0 } }
- File.read('whale.txt')
- .chars
- .each_cons(2) { |a,b| stats[a][b] += 1 }
- ## Find `d`, the most common distance between each pair of characters:
- d = (0..255).max_by { |i| stats.sum { |k,v| v[((k.ord+i)%256).chr] } } # => 244
- ## Create a function that returns the character `d` away from the given character c
- f = ->(c) {((c.ord+d)%256).chr}
- # Compare the functions
- INPUT = File.read('whale2.txt')
- def score_function(f)
- INPUT
- .each_char
- .each_cons(2)
- .count { |a,b| f[a] != b}
- end
- puts "space score: #{score_function(s)}"
- puts "distance score: #{score_function(f)}"
- # Output:
- # -----------------------
- # space score: 1005113
- # distance score: 1160559
- # -----------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement