Skip to content

Commit

Permalink
Day 7 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jpastoor committed Dec 13, 2016
1 parent 15c48f5 commit 8afcd9d
Show file tree
Hide file tree
Showing 3 changed files with 2,129 additions and 0 deletions.
91 changes: 91 additions & 0 deletions day7/day7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"strings"

"github.com/jpastoor/advent-of-code-2016/aoc2016utils"
)

func main() {
loader := aoc2016utils.AssignmentLoader{}
input := loader.Load(7)

counter := 0
for _, line := range strings.Split(input, "\n") {
if checkTLSSupport(line) {
counter++
}
}

fmt.Println(counter)

}

func checkTLSSupport(ip string) bool {

// Split string in sequences
normal := []string{}
hypernet := []string{}

current := ""
for _, ch := range ip {
str := string(ch)

switch str {
case "[":
if current != "" {
normal = append(normal, current)
current = ""
}
case "]":
if current != "" {
hypernet = append(hypernet, current)
current = ""
}
default:
current += str
}
}

// Clean buffer at the end
if current != "" {
normal = append(normal, current)
current = ""
}

// Loop over all normal parts to see if there is an abba
normalContainsABBA := false
for _, input := range normal {
if containsABBA(input) {
normalContainsABBA = true
break
}
}

// Loop over all hypernet parts to see if there is an abba
hypernetContainsABBA := false
for _, input := range hypernet {
if containsABBA(input) {
hypernetContainsABBA = true
break
}
}

return normalContainsABBA && !hypernetContainsABBA
}

/**
Check if string contains an ABBA
*/
func containsABBA(input string) bool {
// We loop over every char in groups of 4, changing 1 position at a time
for i := 0; i < len(input)-3; i++ {
// The ABBA check
if input[i] == input[i+3] && input[i+1] == input[i+2] && input[i+2] != input[i+3] {
return true
}
}

return false
}
38 changes: 38 additions & 0 deletions day7/day7_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "testing"

func TestTLSSupport(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
"abba outside square brackets",
"abba[mnop]qrst",
true,
},
{
"bddb is within square brackets, even though xyyx is outside square brackets",
"abcd[bddb]xyyx",
false,
},
{
"aaaa is invalid; the interior characters must be different",
"aaaa[qwer]tyui",
false,
},
{
"oxxo is outside square brackets, even though it's within a larger string",
"ioxxoj[asdfgh]zxcvbn",
true,
},
}

for _, tt := range tests {
if got := checkTLSSupport(tt.input); got != tt.want {
t.Errorf("%q. checkTLSSupport(%s) = %v, want %v", tt.name, tt.input, got, tt.want)
}
}
}
Loading

0 comments on commit 8afcd9d

Please sign in to comment.