-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_5.rb
52 lines (38 loc) · 992 Bytes
/
day_5.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
require 'digest'
def interesting_hash?(str)
str[0..4] == "00000"
end
def hashed_string(str)
Digest::MD5.hexdigest(str)
end
def construct_password_in_order(input)
password = []
i = 0
while password.length < 8
hashed_string = hashed_string("#{input}#{i}")
password << hashed_string[5] if interesting_hash?(hashed_string)
i += 1
end
password.join
end
# Part A
# puts construct_password_in_order('cxdnnyjw')
###############################
# Part B
def construct_password_with_positioning(input)
password = Array.new(8,nil)
i = 0
valid_positions = (0..7).to_a.map {|i| i.to_s }
while password.include?(nil)
hashed_string = hashed_string("#{input}#{i}")
if interesting_hash?(hashed_string)
pos = hashed_string[5]
if valid_positions.include?(pos) && password[pos.to_i].nil?
password[pos.to_i] = hashed_string[6]
end
end
i += 1
end
password.join
end
# puts construct_password_with_positioning('cxdnnyjw')