81 lines
1.3 KiB
Go
81 lines
1.3 KiB
Go
package day05
|
|
|
|
import (
|
|
"aoc2015/aoclib"
|
|
"bytes"
|
|
)
|
|
|
|
var vowels = []byte{'a', 'e', 'i', 'o', 'u'}
|
|
var badComps = map[string]struct{}{
|
|
"ab": {},
|
|
"cd": {},
|
|
"pq": {},
|
|
"xy": {},
|
|
}
|
|
|
|
func stringIsNiceP1(toTest []byte) bool {
|
|
doubleChar := false
|
|
vowelCount := 0
|
|
for x, c := range toTest {
|
|
if x > 0 {
|
|
if toTest[x-1] == c {
|
|
doubleChar = true
|
|
}
|
|
if _, ok := badComps[string(toTest[x-1:x+1])]; ok {
|
|
return false
|
|
}
|
|
}
|
|
if bytes.Contains(vowels, []byte{c}) {
|
|
vowelCount++
|
|
}
|
|
}
|
|
|
|
if doubleChar && vowelCount >= 3 {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func stringIsNiceP2(toTest []byte) bool {
|
|
spacedChar := false
|
|
doubleRepeat := false
|
|
for x, c := range toTest {
|
|
if x > 1 {
|
|
if toTest[x-2] == c {
|
|
spacedChar = true
|
|
}
|
|
}
|
|
if x > 2 {
|
|
for i := 0; i < x-2; i++ {
|
|
if toTest[i] == toTest[x-1] && toTest[i+1] == toTest[x] {
|
|
doubleRepeat = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return spacedChar && doubleRepeat
|
|
}
|
|
|
|
func Part1(puzzle aoclib.Puzzle) interface{} {
|
|
niceCount := 0
|
|
for _, toCheck := range puzzle.GetInputArray() {
|
|
if stringIsNiceP1([]byte(toCheck)) {
|
|
niceCount++
|
|
}
|
|
}
|
|
|
|
return niceCount
|
|
}
|
|
|
|
func Part2(puzzle aoclib.Puzzle) interface{} {
|
|
niceCount := 0
|
|
for _, toCheck := range puzzle.GetInputArray() {
|
|
if stringIsNiceP2([]byte(toCheck)) {
|
|
niceCount++
|
|
}
|
|
}
|
|
|
|
return niceCount
|
|
}
|