Skip to content

Commit

Permalink
Year 2016: Day 05
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Feb 22, 2024
1 parent 4621f49 commit 26fd8fb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ I'm also adding notes that may be useful if you're learning Ruby.

Notes for solving:
* [2015, complete](year_2015.md)
* [2016, up to day 04](year_2016.md)
* [2016, up to day 05](year_2016.md)
* [2023, up to day 03](year_2023.md)

# How to use
Expand Down
29 changes: 29 additions & 0 deletions spec/year_2016/day_05_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'year_2016/day_05'

describe Year2016::Day05 do
context 'when Part 1' do
it 'gives a final result' do
expect(described_class.new('abc', true).code).to eq('18f47a30')
end
end

context 'when Part 2' do
it 'gives a final result' do
expect(described_class.new('abc').code).to eq('05ace8e3')
end
end

context 'when Results' do
subject(:input_data) do
'ojvtpuvg'
end

it 'correctly answers part 1' do
expect(described_class.new(input_data, true).code).to eq('4543c154')
end

it 'correctly answers part 2' do
expect(described_class.new(input_data).code).to eq('1050cbbd')
end
end
end
15 changes: 15 additions & 0 deletions year_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ Year2016::Day04
```

The second part of this exercise involves implementing a [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher), which is not a hard concept to grasp. It's a bit annoying to work through ASCII values of characters and modulo properly to get the right number.

## Day 05: How About a Nice Game of Chess?

```
Year2016::Day05
when Part 1
gives a final result
when Part 2
gives a final result
when Results
correctly answers part 1
correctly answers part 2
```

Not gonna lie, brute-forcing [MD5 hashes](https://en.wikipedia.org/wiki/MD5) is not something interesting.
49 changes: 49 additions & 0 deletions year_2016/day_05.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'digest'

class Year2016
class Day05
def initialize(input_data, input_part_one = false)
@version = input_part_one ? 1 : 2
@input = input_data.chomp
end

def md5
@md5 ||= Digest::MD5.new
end

def hash(str)
md5.reset
md5 << str
md5.hexdigest
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def build_code
@code = '_' * 8
added = 0
0.upto(100**100) do |i|
break if added == 8

digest = hash("#{@input}#{i}")
next unless digest.start_with?('0' * 5)

if @version == 1
@code[added] = digest[5]
next added += 1
end

pos = digest[5].ord - 48
next if pos.negative? || pos > 7 || @code[pos] != '_'

code[pos] = digest[6]
added += 1
end
@code
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def code
@code ||= build_code
end
end
end

0 comments on commit 26fd8fb

Please sign in to comment.