Skip to content

Instantly share code, notes, and snippets.

@numinit
Created May 11, 2017 00:46
Show Gist options
  • Save numinit/e1de66c11acc9309d14f16b6306f72e5 to your computer and use it in GitHub Desktop.
Save numinit/e1de66c11acc9309d14f16b6306f72e5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# usage: echo <string> | ruby kryptos.rb
# assumes you have a spreadsheet or something open
# with the top left character (space) at column A, row 1
k4 = <<EOF.chomp.lines.map(&:chomp)
OBKR
UOXOGHULBSOLIFBBWFLRVQQPRNGKSSO
TWTQSJQSSEKZZWATJKLUDIAWINFBNYP
VTTMZFPKWGDKZXTJCDIGKUHUAUEKCAR
EOF
search = STDIN.read.strip.upcase
def search haystack, needle
max_x, max_y = haystack.first.length - 1, haystack.length - 1
haystack.each_with_index do |line, y|
line.chars.each_with_index do |char, x|
next unless needle[0] == haystack[y][x]
pairs = [
[x-1, y-1], [x-1, y], [x-1, y+1],
[x, y-1], [x, y], [x, y+1],
[x+1, y-1], [x+1, y], [x+1, y+1]
]
pairs.each do |(xx, yy)|
next if xx < 0 or xx > max_x or yy < 0 or yy > max_y
if needle[1] == haystack[yy][xx]
yield [x, y], [xx, yy]
end
end
end
end
end
def n2l n
%w[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH][n]
end
matches, pairs = 0, 0
search.chars.each_cons(2).map do |pair|
pair = pair.join
matched = false
search k4, pair do |(x, y), (xx, yy)|
puts "#{matched ? ' => ' : ''}#{pair}\t#{n2l(x)}#{y+1}, #{n2l(xx)}#{yy+1}"
matches += 1 if !matched
matched = true
end
pairs += 1
end
puts "#{matches} unique match / #{pairs} pairs: #{'%.2f%%' % (matches.to_f / pairs.to_f * 100)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment