day07
This commit is contained in:
parent
b2faced8a9
commit
3a9c5d69b0
97
day07/day.go
Normal file
97
day07/day.go
Normal file
@ -0,0 +1,97 @@
|
||||
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
|
||||
}
|
||||
8
inputs/7_test
Normal file
8
inputs/7_test
Normal file
@ -0,0 +1,8 @@
|
||||
abba[mnop1]qrst1[mnop2]qrst2
|
||||
abcd[bddb]xyyx
|
||||
aaaa[qwer]tyui
|
||||
ioxxoj[asdfgh]zxcvbn
|
||||
aba[bab]xyz
|
||||
xyx[xyx]xyx
|
||||
aaa[kek]eke
|
||||
zazbz[bzb]cdb
|
||||
3
main.go
3
main.go
@ -7,6 +7,7 @@ import (
|
||||
"aoc2016/day04"
|
||||
"aoc2016/day05"
|
||||
"aoc2016/day06"
|
||||
"aoc2016/day07"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -30,7 +31,7 @@ func initDayFunctions() {
|
||||
4: {1: day04.Part1, 2: day04.Part2},
|
||||
5: {1: day05.Part1, 2: day05.Part2},
|
||||
6: {1: day06.Part1, 2: day06.Part2},
|
||||
// 7: {1: day07.Part1, 2: day07.Part2},
|
||||
7: {1: day07.Part1, 2: day07.Part2},
|
||||
// 8: {1: day08.Part1, 2: day08.Part2},
|
||||
// 9: {1: day09.Part1, 2: day09.Part2},
|
||||
// 10: {1: day10.Part1, 2: day10.Part2},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user