adventofcode/2016/day07/main.go

96 lines
1.7 KiB
Go
Raw Permalink Normal View History

2016-12-07 13:54:13 +00:00
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
}