2019. Day 4. Part 2

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2019-12-07 01:28:00 +01:00
parent fc806e8961
commit ee32ee2545

View File

@@ -18,8 +18,19 @@ func intToSlice(n int) []int {
} }
func twoEqualAdjacentDigits(pwd []int) bool { func twoEqualAdjacentDigits(pwd []int) bool {
for i, digit := range pwd { l := len(pwd)
if i < len(pwd)-1 && digit == pwd[i+1] { for i := 0; i < l-1; i++ {
if pwd[i] == pwd[i+1] {
return true
}
}
return false
}
func twoExclusiveAdjacentDigits(pwd []int) bool {
l := len(pwd)
for i := 0; i < l-1; i++ {
if pwd[i] == pwd[i+1] && (i == 0 || pwd[i] != pwd[i-1]) && (i == l-2 || pwd[i] != pwd[i+2]) {
return true return true
} }
} }
@@ -37,12 +48,20 @@ func increasingDigits(pwd []int) bool {
func main() { func main() {
// Part 1: // Part 1:
count := 0 count1, count2 := 0, 0
for i := min; i < max; i++ { for i := min; i < max; i++ {
pwd := intToSlice(i) pwd := intToSlice(i)
if twoEqualAdjacentDigits(pwd) && increasingDigits(pwd) { two := twoEqualAdjacentDigits(pwd)
count++ onlyTwo := twoExclusiveAdjacentDigits(pwd)
if increasingDigits(pwd) {
if two {
count1++
}
if onlyTwo {
count2++
}
} }
} }
fmt.Printf("Part 1: %d\n", count) fmt.Printf("Part 1: %d\n", count1)
fmt.Printf("Part 2: %d\n", count2)
} }