Skip to content

Commit 567b7cf

Browse files
committed
Add day 14: One-Time Pad
1 parent 90898ea commit 567b7cf

File tree

4 files changed

+47602
-0
lines changed

4 files changed

+47602
-0
lines changed

14_one_time_pad.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'digest'
2+
3+
WINDOW = 1000
4+
NUM_KEYS = 64
5+
6+
def pads(inputs)
7+
pads = []
8+
stop = 1.0 / 0.0
9+
10+
triplets = Hash.new { |h, k| h[k] = [] }
11+
12+
0.step { |i|
13+
md5 = inputs[i]
14+
15+
md5.scan(/(.)\1\1\1\1/).each { |(char)|
16+
candidates = triplets.delete(char)
17+
candidates.select { |n| n + WINDOW >= i }.each { |n|
18+
pads << n
19+
# Still need to check for any numbers undercutting n.
20+
stop = n + WINDOW if pads.size == NUM_KEYS
21+
}
22+
}
23+
24+
m = (/(.)\1\1/.match(md5))
25+
triplets[m[1]] << i if m
26+
27+
return pads.sort if i >= stop
28+
}
29+
end
30+
31+
real = ARGV.delete('-r')
32+
input = (!ARGV.empty? && !File.exist?(ARGV.first) ? ARGV.first : ARGF.read).freeze
33+
34+
f2017 = ->i {
35+
s = input + i.to_s
36+
2017.times { s = Digest::MD5.hexdigest(s) }
37+
s
38+
}
39+
if !real
40+
precomputed_file = "#{__dir__}/hashes/md5-2017-#{Digest::SHA256.hexdigest(input)}"
41+
f2017 = File.readlines(precomputed_file) if File.exist?(precomputed_file)
42+
end
43+
44+
[
45+
->i { Digest::MD5.hexdigest(input + i.to_s) },
46+
f2017,
47+
].each { |f| puts pads(f)[NUM_KEYS - 1] }

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Some may additionally support other ways:
3434
Otherwise, pass the office designer's favourite number on ARGV.
3535
Changing the goal is not supported.
3636
The `-f` flag causes the flood-fill to be printed out.
37+
* 14 (One-Time Pad): Pass the salt in ARGV.
38+
Due to long running time, the hashes have been precomputed.
39+
To redo the computation, pass the `-r` flag.
3740

3841
# Posting schedule and policy
3942

0 commit comments

Comments
 (0)