Class: GentleBrute::Odometer
- Inherits:
-
Object
- Object
- GentleBrute::Odometer
- Defined in:
- lib/gentle_brute/odometer.rb
Instance Method Summary collapse
- #has_triple_char_pattern? ⇒ Boolean
-
#increment(total_steps = 1) ⇒ Object
Increment the odometer.
-
#initialize(start_length = 1, heuristic = true) ⇒ Odometer
constructor
A new instance of Odometer.
- #rotate_out_bad_end_pairs ⇒ Object
-
#rotate_out_bad_start_pairs ⇒ Object
def break_up_triplet_patterns return if not @heuristic.
-
#string_for_odometer ⇒ String
String representation of the odometer, with character indexes mapped to their respective characters.
- #to_s ⇒ Object
Constructor Details
#initialize(start_length = 1, heuristic = true) ⇒ Odometer
Returns a new instance of Odometer.
3 4 5 6 7 8 9 |
# File 'lib/gentle_brute/odometer.rb', line 3 def initialize(start_length=1, heuristic=true) @letters = ('a'..'z').to_a @chars = @letters + [" ", "'"] @odometer = Array.new(start_length, 0) @heuristic = heuristic @cpa_analyzer = CPAAnalyzer.new end |
Instance Method Details
#has_triple_char_pattern? ⇒ Boolean
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gentle_brute/odometer.rb', line 11 def has_triple_char_pattern? return false if not @heuristic indexes = [] odometer_length = @odometer.length (odometer_length-1).downto 0 do | i | index = @odometer[i] if indexes.length > 0 char1 = @chars[index] # figure out what char the current index is char2 = @chars[@odometer[indexes[0]]] # get the char of our first element in the indexes list indexes = [] if char1 != char2 end indexes << i break if indexes.length == 3 end return false if indexes.length < 3 true end |
#increment(total_steps = 1) ⇒ Object
Increment the odometer
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gentle_brute/odometer.rb', line 137 def increment(total_steps=1) steps_taken = 0 while steps_taken < total_steps odometer_length = @odometer.length (odometer_length-1).downto 0 do | i | element = @odometer[i] element += 1 if element != @chars.length @odometer[i] = element break end @odometer[i] = 0 @odometer << 0 if i == 0 end rotate_out_bad_end_pairs rotate_out_bad_start_pairs next if has_triple_char_pattern? steps_taken += 1 end end |
#rotate_out_bad_end_pairs ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gentle_brute/odometer.rb', line 92 def rotate_out_bad_end_pairs return if not @heuristic return if @odometer.length < 3 char1 = @chars[@odometer[-1]] char2 = @chars[@odometer[-2]] char3 = @chars[@odometer[-3]] return if not @letters.include? char1 return if not @letters.include? char2 return if not @letters.include? char3 last = @odometer.length-1 if @cpa_analyzer.get_ender_neighbor_score(char1, char3)[1] == 0 last.downto 0 do | i | element = @odometer[i] element += 1 if element != @chars.length @odometer[i] = element break end @odometer[i] = 0 @odometer << 0 if i == 0 end rotate_out_bad_end_pairs end if @cpa_analyzer.get_ender_neighbor_score(char1, char2)[0] == 0 last.downto 0 do | i | element = @odometer[i] element += 1 if element != @chars.length @odometer[i] = element break end @odometer[i] = 0 @odometer << 0 if i == 0 end rotate_out_bad_end_pairs end end |
#rotate_out_bad_start_pairs ⇒ Object
def break_up_triplet_patterns
return if not @heuristic
word = string_for_odometer
pattern_data = PatternFinder.patterns_in_strintg word
return if pattern_data == nil
return if pattern_data[3] < 3
index = pattern_data[4][1]
index.downto 0 do | i |
element = @odometer[i]
element += 1
if element != @chars.length
@odometer[i] = element
break
end
@odometer[i] = 0
@odometer << 0 if i == 0
end
break_up_triplet_patterns
end
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/gentle_brute/odometer.rb', line 52 def rotate_out_bad_start_pairs return if not @heuristic return if @odometer.length < 3 char1 = @chars[@odometer[0]] char2 = @chars[@odometer[1]] char3 = @chars[@odometer[2]] return if not @letters.include? char1 return if not @letters.include? char2 return if not @letters.include? char3 if @cpa_analyzer.get_starter_neighbor_score(char1, char3)[1] == 0 2.downto 0 do | i | element = @odometer[i] element += 1 if element != @chars.length @odometer[i] = element break end @odometer[i] = 0 @odometer << 0 if i == 0 end rotate_out_bad_start_pairs end if @cpa_analyzer.get_starter_neighbor_score(char1, char2)[0] == 0 1.downto 0 do | i | element = @odometer[i] element += 1 if element != @chars.length @odometer[i] = element break end @odometer[i] = 0 @odometer << 0 if i == 0 end rotate_out_bad_start_pairs end end |
#string_for_odometer ⇒ String
Returns string representation of the odometer, with character indexes mapped to their respective characters.
159 160 161 |
# File 'lib/gentle_brute/odometer.rb', line 159 def string_for_odometer return @odometer.map {|i| @chars[i]}.join "" end |
#to_s ⇒ Object
163 164 165 |
# File 'lib/gentle_brute/odometer.rb', line 163 def to_s string_for_odometer end |