Day 7 Complete
This commit is contained in:
parent
b1b4ea62b6
commit
f90d286916
BIN
day07/day07
Executable file
BIN
day07/day07
Executable file
Binary file not shown.
2000
day07/input
Normal file
2000
day07/input
Normal file
File diff suppressed because it is too large
Load Diff
95
day07/main.go
Normal file
95
day07/main.go
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ips := stdinToStringSlice()
|
||||||
|
var allowTLS []string
|
||||||
|
var allowSSL []string
|
||||||
|
for _, ip := range ips {
|
||||||
|
if hasTLS(ip) {
|
||||||
|
allowTLS = append(allowTLS, ip)
|
||||||
|
}
|
||||||
|
if hasSSL(ip) {
|
||||||
|
allowSSL = append(allowSSL, ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("IPs supporting TLS: ", len(allowTLS))
|
||||||
|
fmt.Println("IPs supporting SSL: ", len(allowSSL))
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasTLS(ip string) bool {
|
||||||
|
bad := false
|
||||||
|
hprSq := getHypernetSqns(ip)
|
||||||
|
for _, hpr := range hprSq {
|
||||||
|
if hasABBA(hpr) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !bad && hasABBA(ip) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasSSL(ip string) bool {
|
||||||
|
hprSq := getHypernetSqns(ip)
|
||||||
|
rest := cutHypernetSqns(ip, hprSq)
|
||||||
|
for i := range rest {
|
||||||
|
if len(rest) > i+2 {
|
||||||
|
if rest[i] == rest[i+2] && rest[i] != rest[i+1] {
|
||||||
|
bab := string(rest[i+1]) + string(rest[i]) + string(rest[i+1])
|
||||||
|
for j := range hprSq {
|
||||||
|
if strings.Contains(hprSq[j], bab) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHypernetSqns(ip string) []string {
|
||||||
|
var ret []string
|
||||||
|
re := regexp.MustCompile(`\[[^\[]*\]`)
|
||||||
|
s := re.FindAll([]byte(ip), -1)
|
||||||
|
|
||||||
|
for i := range s {
|
||||||
|
ret = append(ret, string(s[i][1:len(s[i])-1]))
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func cutHypernetSqns(ip string, hpn []string) string {
|
||||||
|
for i := range hpn {
|
||||||
|
ip = strings.Replace(ip, hpn[i], "", -1)
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasABBA(ip string) bool {
|
||||||
|
for j := range ip {
|
||||||
|
if len(ip) > j+3 {
|
||||||
|
if ip[j] == ip[j+3] && ip[j+1] == ip[j+2] && ip[j] != ip[j+1] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func stdinToStringSlice() []string {
|
||||||
|
var input []string
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
input = append(input, scanner.Text())
|
||||||
|
}
|
||||||
|
return input
|
||||||
|
}
|
62
day07/problem
Normal file
62
day07/problem
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- 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?
|
||||||
|
|
||||||
|
Your puzzle answer was _____.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
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]xyz supports SSL (aba outside square brackets with corresponding bab within square brackets).
|
||||||
|
• xyx[xyx]xyx does not support SSL (xyx, but no corresponding yxy).
|
||||||
|
• aaa[kek]eke supports SSL (eke in supernet with corresponding kek in hypernet; the aaa sequence is not related, because the interior
|
||||||
|
character must be different).
|
||||||
|
• zazbz[bzb]cdb supports SSL (zaz has no corresponding aza, but zbz has a corresponding bzb, even though zaz and zbz overlap).
|
||||||
|
|
||||||
|
How many IPs in your puzzle input support SSL?
|
||||||
|
|
||||||
|
Your puzzle answer was _______.
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. http://adventofcode.com/
|
||||||
|
. http://adventofcode.com/2016/about
|
||||||
|
. http://adventofcode.com/2016/support
|
||||||
|
. http://adventofcode.com/2016/events
|
||||||
|
. http://adventofcode.com/2016/settings
|
||||||
|
. http://adventofcode.com/2016/auth/logout
|
||||||
|
. http://adventofcode.com/2016
|
||||||
|
. http://adventofcode.com/2016
|
||||||
|
. http://adventofcode.com/2016/leaderboard
|
||||||
|
. http://adventofcode.com/2016/stats
|
||||||
|
. http://adventofcode.com/2016/sponsors
|
||||||
|
. http://adventofcode.com/2016/sponsors
|
||||||
|
. https://en.wikipedia.org/wiki/IP_address
|
||||||
|
. https://en.wikipedia.org/wiki/IPv6
|
||||||
|
. http://adventofcode.com/2016
|
||||||
|
. http://adventofcode.com/2016/day/7/input
|
Loading…
Reference in New Issue
Block a user