Advent of Code: Day 7 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 7, part 1

Simplified problem: Given a list of inputs, determine how many contain a string in the pattern xyyx in any part of the input outside square brackets, where x is any a-z character, and y is any a-z character but not the same character as x.

The input must not contain the a string in the same pattern (consisting of any characters) in any string in the inputs inside square brackets.

Solution

Create an Ipv7Address object with the following properties:

  • OutsideSegments – a list of all of the strings outside square brackets
  • InsideSegments – a list of all of the strings inside square brackets
  1. Set input=(input)
  2. Set NumberOfValidIpAddresses = 0
  3. For each ipAddress in input
    1. Create a new Ipv7Address, generating a list of all inside and outside segments.
    2. For each outside segment
      1. For i = 0 to segment length -3 (check each set of 4 characters)
        1. If the character at segment[i] is the same as segment[i+3], and the character at segment[i+1] is the same as segment [i+2] and the character at segment[i] is not the same as segment [i+1], set PatternFoundInOutsideSegment to true. Jump to step 4
      2. End For
    3. End For
    4. If PatternFound is true:
      1. For each inside segment
        1. For i = 0 to segment length -3 (check each set of 4 characters)
          1. If the character at segment[i] is the same as segment[i+3], and the character at segment[i+1] is the same as segment [i+2] and the character at segment[i] is not the same as segment [i+1], set PatternFoundInInsideSegment to true. Jump to step 5
        2. End For
      2. End For
    5. If PatternFoundInOutsideSegment = true and PatternFoundInInsideSegment = false, add 1 to NumberOfValidIpAddresses
  4. End For
  5. Return NumberOfValidIpAddresses

Day 7, part 2

Using the same input, search for the pattern xyx outside square brackets, and mark any inputs with the pattern yxy inside square brackets as valid.

Solution

  1. Set input=(input)
  2. Set NumberOfValidIpAddresses = 0
  3. For each ipAddress in input
    1. Create a new Ipv7Address, generating a list of all inside and outside segments.
    2. Set patternList = new list of strings (holds any yxy patterns found)
    3. For each outside segment
      1. For i = 0 to segment length -2 (check each set of 3 characters)
        1. If the character at segment[i] is the same as segment[i+2], and the character at segment[i] is different to segment [i+1]
          1. Generate xyxPattern = segment[i+1] + segment[i] + segment[i+1]
          2. Add xyxPattern to patternList
        2. End If
      2. End For
    4. End For
    5. If any Inside segments contain any of the xyx patterns in patternList,  add 1 to NumberOfValidIpAddresses
  4. End For
  5. Return NumberOfValidIpAddresses

You can find the full solution on Github

Leave a Reply

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