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.
1 2 3 4 |
abba[mnop]qrst is valid (abba outside square brackets). abcd[bddb]xyyx is not valid (bddb is within square brackets, even though xyyx is outside square brackets). aaaa[qwer]tyui is not valid (aaaa is invalid; the interior characters must be different). ioxxoj[asdfgh]zxcvbn is not valid (oxxo is outside square brackets, even though it's within a larger string). |
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
- Set input=(input)
- Set NumberOfValidIpAddresses = 0
- For each ipAddress in input
- Create a new Ipv7Address, generating a list of all inside and outside segments.
- For each outside segment
- For i = 0 to segment length -3 (check each set of 4 characters)
- 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
- End For
- For i = 0 to segment length -3 (check each set of 4 characters)
- End For
- If PatternFound is true:
- For each inside segment
- For i = 0 to segment length -3 (check each set of 4 characters)
- 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
- End For
- For i = 0 to segment length -3 (check each set of 4 characters)
- End For
- For each inside segment
- If PatternFoundInOutsideSegment = true and PatternFoundInInsideSegment = false, add 1 to NumberOfValidIpAddresses
- End For
- 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
- Set input=(input)
- Set NumberOfValidIpAddresses = 0
- For each ipAddress in input
- Create a new Ipv7Address, generating a list of all inside and outside segments.
- Set patternList = new list of strings (holds any yxy patterns found)
- For each outside segment
- For i = 0 to segment length -2 (check each set of 3 characters)
- 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]
- Generate xyxPattern = segment[i+1] + segment[i] + segment[i+1]
- Add xyxPattern to patternList
- End If
- 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]
- End For
- For i = 0 to segment length -2 (check each set of 3 characters)
- End For
- If any Inside segments contain any of the xyx patterns in patternList, add 1 to NumberOfValidIpAddresses
- End For
- Return NumberOfValidIpAddresses
You can find the full solution on Github