Advent of Code – Day 5 Solution

This year, I’m posting my solutions to the puzzles in the Advent of Code. Each post will consist of pseudocode, with a link to the actual code I used to solve the puzzle on Github.

And now, on with the solution:

Day 5, part 1

Simplified problem: Determine an 8-character code by calculating the MD5 sum of the puzzle input followed by an index starting at 0. The code is determined by the 6th digit in the first MD5 sum that starts with 00000. If the sum does not start with 00000, increase the index by 1, and calculate the sum of that input.

 

Solution

  1. Set input=(your input)
  2. Set index=0
  3. Set password=nothing
  4. While password length < 8
    1. Set md5sum =  MD5 sum of the string input + index (e.g. abc0)
    2. While md5sum does not start with “00000”
      1. Add 1 to index
      2. Set md5sum =  MD5 sum of the string input + index (e.g. abc1)
    3. End While
    4. Add the 6th character of md5sum to password
  5. End While
  6. Return password

md5sum is calculated using the following function:

Day 5, part 2

Use the same input and index pattern to determine the password. This time, however, the MD5 hash must start with 00000, and you must use the 6th and 7th characters to determine the password.

The 6th character determines the location in the password (position 0-7). the 7th character is the password character itself. For instance MD5 hash 000006B472146F38A8D1C5B39E620EDC sets the 7th character in the password to B. The hash 000008EBAB696528EBCD9C5CA89C8243 is invalid, because 8 is not a valid position in the password. In this case, we continue calculating MD5 sums, ignoring this result.

Solution

  1. Set input=(your input)
  2. Set index=0
  3. Set password=”——–“
  4. While password contains the character ‘-‘
    1. Set md5sum =  MD5 sum of the string input + index (e.g. abc0)
    2. While md5sum does not start with “00000” and the 6th character in md5sum is not in the range 0-7
      1. Add 1 to index
      2. Set md5sum =  MD5 sum of the string input + index (e.g. abc1)
    3. End While
    4. Add the 7th character of md5sum to password at position represented by the 6th character in md5sum
  5. End While
  6. Return password

You can find the full solution on Github

Leave a Reply

Your email address will not be published. Required fields are marked *