2016 / Day 7: Internet Protocol Version 7
Puzzle
puzzle =
"./puzzles/day07.txt"
|> Path.expand(__DIR__)
|> File.read!()
|> String.split("\n")
|> Enum.slice(0..-2)
:ok
Part 1
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]qrstsupports TLS (abbaoutside square brackets). -
abcd[bddb]xyyxdoes not support TLS (bddbis within square brackets, even thoughxyyxis outside square brackets). -
aaaa[qwer]tyuidoes not support TLS (aaaais invalid; the interior characters must be different). -
ioxxoj[asdfgh]zxcvbnsupports TLS (oxxois outside square brackets, even though it’s within a larger string).
How many IPs in your puzzle input support TLS?
puzzle
|> Enum.filter(fn ip ->
case Regex.run(~r/.*(\w)(\w)\2\1.*/, ip) do
[_, a, b] when a != b -> true
_ -> false
end
end)
|> Enum.reject(fn ip ->
case Regex.run(~r/.*\[\w*(\w)(\w)\2\1\w*\].*/, ip) do
[_, a, b] when a != b -> true
_ -> false
end
end)
|> Enum.count()
Part 2
You would also like to know which IPs support SSL (super-secret listening).
An IP supports SSL if it has an Area-Broadcast Accessor, or ABA, anywhere in the supernet sequences (outside any square bracketed sections), and a corresponding Byte Allocation Block, or BAB, anywhere in the hypernet sequences. An ABA is any three-character sequence which consists of the same character twice with a different character between them, such as xyx or aba. A corresponding BAB is the same characters but in reversed positions: yxy and bab, respectively.
For example:
-
aba[bab]xyzsupports SSL (abaoutside square brackets with corresponding bab within square brackets). -
xyx[xyx]xyxdoes not support SSL (xyx, but no correspondingyxy). -
aaa[kek]ekesupports SSL (ekein supernet with correspondingkekin hypernet; theaaasequence is not related, because the interior character must be different). -
zazbz[bzb]cdbsupports SSL (zazhas no correspondingaza, butzbzhas a correspondingbzb, even thoughzazandzbzoverlap).
How many IPs in your puzzle input support SSL?
puzzle
|> Enum.filter(fn entry ->
babs =
Regex.scan(~r/\[(\w+)\]/, entry, capture: :all_but_first)
|> List.flatten()
|> Enum.flat_map(fn s ->
s
|> String.graphemes()
|> Enum.chunk_every(3, 1, leftover: :discard)
|> Enum.filter(fn
[a, b, a] when a != b -> true
_ -> false
end)
end)
Regex.scan(~r/(?:^|\])([a-z]+)(?:$|\[)/, entry, capture: :all_but_first)
|> List.flatten()
|> Enum.any?(fn s ->
Enum.any?(babs, fn [b, a, b] ->
String.contains?(s, "#{a}#{b}#{a}")
end)
end)
end)
|> Enum.count()