-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.rb
73 lines (54 loc) · 1.48 KB
/
solution.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
####
# Entry by Delton Ding
# see https://rubytalk.org/t/ruby-quiz-challenge-8-base32-alphabet-convert-the-super-sekretoooo-240-bit-cryptokitties-genome-to-kai-notation-annipurrsary/74871/2
module Delton
def kai_encode( num )
num.to_s(2).rjust(240, '0').scan(/.{5}/).map {|n| '123456789abcdefghijklmnopqrstuvwx'[n.to_i(2)]}.join
end
def kai_fmt( kai )
kai.scan(/.{4}/).join(' ')
end
end ## module Delton
####
# Entry by Frank J. Cameron
# see https://rubytalk.org/t/ruby-quiz-challenge-8-base32-alphabet-convert-the-super-sekretoooo-240-bit-cryptokitties-genome-to-kai-notation-annipurrsary/74871/3
module Frank
Encode = [(1..9),('a'..'k'),('l'..'x')].map(&:to_a).inject(&:+)
def kai_encode( num )
num
.to_s(2)
.rjust(240, '0')
.yield_self{|bs| (0..235).step(5).map{|i| bs[i,5]}}
.map{|n| Encode[n.to_i(2)]}
.join
end
def kai_fmt( kai )
kai
.chars
.each_slice(4)
.map(&:join)
.join(' ')
end
end
end ## module Frank
#############
# Test entry
# by Gerald Bauer
module TestAnswer
ALPHABET = '123456789abcdefghijkmnopqrstuvwx'
BASE = ALPHABET.length ## 32 chars/letters/digits
def kai_encode( num )
buf = String.new
while num >= BASE
mod = num % BASE
buf = ALPHABET[mod] + buf
num = (num - mod)/BASE
end
ALPHABET[num] + buf
end
def kai_fmt( kai )
## note: allow spaces; remove them all first
kai = kai.gsub( ' ', '' )
kai.reverse.gsub( /(.{4})/, '\1 ').reverse.strip
end
end