-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
2,129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.