Advent of Code 2016

Recently I have encounter the webpage AdventOfCode.com aka #AoC. I truly love puzzels so I was astonished with this webpage which is full of coding puzzles (24×2) . One of them (#7) I found really interesting for its solution. It was really fun to solve, because I have used regular expressions, which can be brain cracking. So here is day 7 puzzle and my solution.

Advent of Code 2016

Puzzle:

— Day 7: Internet Protocol Version 7 —

While snooping around the local network of EBHQ, you compile a list of IP addresses (they’re IPv7, of course; IPv6 is much too limited). You’d like to figure out which IPs support TLS (transport-layer snooping).

An IP supports TLS if it has an Autonomous Bridge Bypass Annotation, or ABBA. An ABBA is any four-character sequence which consists of a pair of two different characters followed by the reverse of that pair, such as xyyx or abba. However, the IP also must not have an ABBA within any hypernet sequences, which are contained by square brackets.

For example:

abba[mnop]qrst supports TLS (abba outside square brackets).
abcd[bddb]xyyx does not support TLS (bddb is within square brackets, even though xyyx is outside square brackets).
aaaa[qwer]tyui does not support TLS (aaaa is invalid; the interior characters must be different).
ioxxoj[asdfgh]zxcvbn supports TLS (oxxo is outside square brackets, even though it’s within a larger string).
How many IPs in your puzzle input support TLS?

Solution:

I have used javascript, because I was working on some project in it. First I split given data by new lines \n to get array of IPs. Then I use two regular expressions. In first I match ABBA pattern in brackets and exclude them.  And in second I match just ABBA pattern without brackets.

Now let’s look to regular expressions part by part. The second one (matchRegEx) is shorter so I will start with it. I use positive lookahead (?= ) and match my pattern ABBA with ([a-z])([a-z])\2\1, but this match AAAA pattern too. So I have to use negative lookahead (?! ) to ensures that the given pattern AAAA will not match.
The first one RegEx (inBracketsRegEx) use the same core but from both sides there are RegEx to match the pattern only in brackets. On the left side there is  \[[a-z]* to match the left square bracket and zero or more letters. For the right square bracket and zero or more letters there is  [a-z]*\].

Related Post

Right Click to Create New File on Mac OS X TL;DR; Windows has a lot of cons but it has also some pros. One of them is a smooth way to create empty file on right-click. Mac OS doesn't come with...
PPTP VPN on MAC OS X Sierra, High Sierra and Mojav... PPTP VPN for Mac OS X Introduction PPTP VPN is no longer supported on macOS Sierra and High Sierra and that is just fine, because there are some se...
Free Better Battery Stats and Time Remaining for M... Better Battery Stats and Time Remaining for Mac OS TL;DR; Do you miss the time remaining indicator on your MacBook, MacBook Air, or MacBook Pro? He...
Right-Click to Translate (Look Up) with Google Tra... If you are not native english speaker, you may be frustrated with default "look up" options you get after selecting a text and right clicking. There a...
Convert HEIC to JPG on Right Click for Free Convert HEIC to JPG on Right-Click In iOS 11 Apple is replacing the JPEG image format with the new HEIC alternative. But what to do when device or p...
MacBook wifi issue – OS X keeps reconnecting Last few weeks I was facing annoying wifi problems. My mac was reconnecting every 5 minutes. On top of that BSSID was reseting so I saw BSSID changing...
Buy me a coffeeOut of coffee 😱, please help!