Skip to content

Instantly share code, notes, and snippets.

@egwspiti
Created August 31, 2014 03:53
Show Gist options
  • Save egwspiti/d81db9bb7ad956b84701 to your computer and use it in GitHub Desktop.
Save egwspiti/d81db9bb7ad956b84701 to your computer and use it in GitHub Desktop.
require 'epitools'
require 'digest/sha1'
def hash_fig(arr)
Digest::SHA1.hexdigest(arr.join("\n"))
end
def columnize(arr)
arr.map(&:chars).transpose
end
ALPHABET = [*'A'..'Z']
FIGS_OPT = `figlet -f big -w 11 "#{ALPHABET.join("\n")}"`.split("\n").each_slice(8).map { |fig| columnize([fig[0], fig[2], fig[5]]) }
LUT = Hash[ FIGS_OPT.map {|fig| hash_fig([fig[0], fig[2], fig[5]]) }.zip(ALPHABET) ]
SIZES = Hash[ ALPHABET.zip(FIGS_OPT.map(&:size)) ]
figlet = File.readlines('figlet.input').map{|x| x.gsub("\n", '')}
def match_figlet(figlet)
figlet = columnize([figlet[0], figlet[2], figlet[5]])
result = ""
until figlet.empty?
hash = hash_fig([figlet[0], figlet[2], figlet[5]])
if x = LUT[hash]
result << x
figlet = figlet[SIZES[x]..-1] # drop the letter
else
abort('SHIT')
end
end
result
end
time { puts match_figlet(figlet) }
VERIFY = `figlet -f big -w 11 "#{ALPHABET.join("\n")}"`.split("\n").each_slice(8)
time { VERIFY.each { |f| puts match_figlet(f) } }
@egwspiti
Copy link
Author

$ ruby unfiglet-epi-opt-2.rb 
LVUIJQ
elapsed time: 0.00014s
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
elapsed time: 0.00098s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment