98 lines
1.8 KiB
Go
98 lines
1.8 KiB
Go
package day07
|
|
|
|
import (
|
|
"regexp"
|
|
"tools"
|
|
)
|
|
|
|
var reSplit = regexp.MustCompile(`^([^\[]+)|(?:\[([^]]+)]([^\[]+))`)
|
|
|
|
func isABBA(s string) bool {
|
|
for i := 3; i < len(s); i++ {
|
|
if s[i] == s[i-3] && s[i-1] == s[i-2] && s[i] != s[i-1] {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func getABAs(s string) (abaList []string) {
|
|
for i := 2; i < len(s); i++ {
|
|
if s[i] == s[i-2] && s[i] != s[i-1] {
|
|
abaList = append(abaList, string(s[i-1])+string(s[i])+string(s[i-1]))
|
|
}
|
|
}
|
|
|
|
return abaList
|
|
}
|
|
|
|
func hasABA(s, aba string) bool {
|
|
for i := 2; i < len(s); i++ {
|
|
if s[i] == aba[2] && s[i-1] == aba[1] && s[i-2] == aba[0] {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
tlsCounter := 0
|
|
for _, line := range puzzle.GetInputArray() {
|
|
parts := reSplit.FindAllStringSubmatch(line, -1)
|
|
thisABBA := false
|
|
thisHNABBA := false
|
|
if isABBA(parts[0][0]) {
|
|
thisABBA = true
|
|
}
|
|
for i := 1; i < len(parts); i++ {
|
|
if isABBA(parts[i][2]) {
|
|
thisHNABBA = true
|
|
}
|
|
if isABBA(parts[i][3]) {
|
|
thisABBA = true
|
|
}
|
|
}
|
|
if thisABBA && !thisHNABBA {
|
|
tlsCounter++
|
|
}
|
|
}
|
|
|
|
return tlsCounter
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
sslCounter := 0
|
|
for _, line := range puzzle.GetInputArray() {
|
|
var abaList []string
|
|
var hnList []string
|
|
abaInHN := false
|
|
parts := reSplit.FindAllStringSubmatch(line, -1)
|
|
for _, aba := range getABAs(parts[0][0]) {
|
|
abaList = append(abaList, aba)
|
|
}
|
|
for i := 1; i < len(parts); i++ {
|
|
for _, aba := range getABAs(parts[i][3]) {
|
|
abaList = append(abaList, aba)
|
|
}
|
|
hnList = append(hnList, parts[i][2])
|
|
}
|
|
for _, aba := range abaList {
|
|
for _, hn := range hnList {
|
|
if hasABA(hn, aba) {
|
|
abaInHN = true
|
|
break
|
|
}
|
|
}
|
|
if abaInHN {
|
|
break
|
|
}
|
|
}
|
|
if abaInHN {
|
|
sslCounter++
|
|
}
|
|
}
|
|
|
|
return sslCounter
|
|
}
|