Skip to content

Commit 56b87ec

Browse files
committed
Add day 05: MD5 door
1 parent b9034cc commit 56b87ec

4 files changed

+81
-0
lines changed

05_md5_door.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'digest'
2+
3+
real = ARGV.delete('-r')
4+
input = (!ARGV.empty? && !File.exist?(ARGV.first) ? ARGV.first : ARGF.read).freeze
5+
zeroes = '00000'.freeze
6+
7+
pass = ''
8+
pass2 = [nil] * 8
9+
10+
is = 0.step
11+
if !real
12+
# So as not to make Travis run forever:
13+
# Indices producing 5 zeroes are precomputed.
14+
precomputed_file = "#{__dir__}/hashes/zeroes-#{Digest::SHA256.hexdigest(input)}"
15+
is = File.readlines(precomputed_file).map(&:chomp) if File.exist?(precomputed_file)
16+
end
17+
18+
is.each { |i|
19+
md5 = Digest::MD5.hexdigest(input + i.to_s)
20+
next unless md5.start_with?(zeroes)
21+
puts i if real
22+
pass << md5[5]
23+
puts pass if pass.size == 8
24+
25+
pos = Integer(md5[5], 16)
26+
if 0 <= pos && pos < 8 && pass2[pos].nil?
27+
pass2[pos] = md5[6]
28+
break unless pass2.any?(&:nil?)
29+
end
30+
}
31+
puts pass2.join

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ In general, all solutions can be invoked in both of the following ways:
2020
Some may additionally support other ways:
2121

2222
* 1 (Manhattan Distance): Pass the entire sequence of instructions as a single argument in ARGV.
23+
* 5 (MD5 Door): Pass the door ID in ARGV.
24+
Due to long running time, by default the indices generating five zeroes have been pre-computed for the default door ID.
25+
To redo the computation (necessary if using a different ID), pass the `-r` flag.
2326

2427
# Posting schedule and policy
2528

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2307654
2+
2503851
3+
3020934
4+
4275978
5+
7416166
6+
7730074
7+
7739164
8+
8202539
9+
9035235
10+
9196770
11+
9937660
12+
11165269
13+
12053024
14+
12194130
15+
13333252
16+
14466563
17+
15532581
18+
16503992
19+
16579900
20+
16677035
21+
17242708
22+
17399584
23+
17582424
24+
17903193
25+
18701307
26+
19024826
27+
19241402
28+
21799866
29+
22382460
30+
23817947
31+
25370046
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
3231929
2+
5017308
3+
5278568
4+
5357525
5+
5708769
6+
6082117
7+
8036669
8+
8605828
9+
8609554
10+
8760605
11+
9495334
12+
10767910
13+
11039607
14+
12763908
15+
13666005
16+
13753421

0 commit comments

Comments
 (0)