Clean up day 4 a bit

master
Nick Krichevsky 2019-12-04 00:57:49 -05:00
parent 8bfeb4d53b
commit 1cc3bd4199
1 changed files with 15 additions and 7 deletions

View File

@ -27,6 +27,18 @@ func isValidPart1Password(password int) bool {
return hasDoubleDigitPair
}
// getAllPart1Passwords gets all valid passwords for part 1 in [lowerBound, upperBound]
func getAllPart1Passwords(lowerBound, upperBound int) []int {
res := []int{}
for password := lowerBound; password <= upperBound; password++ {
if isValidPart1Password(password) {
res = append(res, password)
}
}
return res
}
func isValidPart2Password(password int) bool {
// The part 2 rules are stricter than part 1; they must be a valid part 1 password
if !isValidPart1Password(password) {
@ -36,6 +48,7 @@ func isValidPart2Password(password int) bool {
lastChar := rune(0)
charCount := 0
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 {
if lastChar == char {
@ -74,15 +87,10 @@ func main() {
panic(fmt.Sprintf("could not parse upper bound: %s", err))
}
part1Passwords := []int{}
for password := lowerBound; password <= upperBound; password++ {
if isValidPart1Password(password) {
part1Passwords = append(part1Passwords, password)
}
}
part1Passwords := getAllPart1Passwords(lowerBound, upperBound)
fmt.Println(len(part1Passwords))
// We can save some computation by just going over our existing part 1 passwords for part 2
numPart2Passwords := 0
for _, password := range part1Passwords {
if isValidPart2Password(password) {