Shorten day 4 part 2 solution from reddit inspiration
parent
1cc3bd4199
commit
536ba72860
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isValidPart1Password(password int) bool {
|
func isValidPart1Password(password int) bool {
|
||||||
|
@ -45,31 +46,16 @@ func isValidPart2Password(password int) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
lastChar := rune(0)
|
|
||||||
charCount := 0
|
|
||||||
passwordStr := strconv.Itoa(password)
|
passwordStr := strconv.Itoa(password)
|
||||||
// We will define an isolated pair to be a set like 22, but not 222
|
|
||||||
hasIsolatedRepeatedPair := false
|
|
||||||
for _, char := range passwordStr {
|
for _, char := range passwordStr {
|
||||||
if lastChar == char {
|
// Because we know the digits must already be ascending, we know that there must be exactly two of one of the
|
||||||
charCount++
|
// characters, and if there is more than one, they must be next to each other
|
||||||
} else if hasIsolatedRepeatedPair {
|
if strings.Count(passwordStr, fmt.Sprintf("%c", char)) == 2 {
|
||||||
// Once we find an isolated pair, the other digits don't matter
|
|
||||||
return true
|
return true
|
||||||
} else {
|
|
||||||
charCount = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
lastChar = char
|
|
||||||
if charCount > 2 {
|
|
||||||
// If the char count ever exceeds two, we need to say that we don't have an isolated pair
|
|
||||||
hasIsolatedRepeatedPair = false
|
|
||||||
} else if charCount == 2 {
|
|
||||||
hasIsolatedRepeatedPair = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return hasIsolatedRepeatedPair
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in New Issue