This commit is contained in:
Stefan Harmuth 2020-12-26 07:19:58 +01:00
parent b3d99b6975
commit a3abcfd0c5
4 changed files with 74 additions and 0 deletions

68
day11/day11.go Normal file
View File

@ -0,0 +1,68 @@
package day11
import (
"aoc2015/aoclib"
"fmt"
)
func incrementPassword(password []byte) {
allFine := false
index := len(password) - 1
for !allFine {
password[index]++
if password[index] > 122 {
password[index] = 'a'
index--
} else {
allFine = true
}
}
}
func checkPasswordValid(password []byte) bool {
ascendingTripple := false
firstDouble := 0
doubleDouble := false
for i := 0; i < len(password); i++ {
if password[i] == 'i' || password[i] == 'o' || password[i] == 'l' {
return false
}
if !ascendingTripple && i < len(password)-3 {
if password[i] == password[i+1]-1 && password[i+1] == password[i+2]-1 {
ascendingTripple = true
}
}
if !doubleDouble && i > 0 {
if password[i] == password[i-1] {
if firstDouble > 0 && firstDouble+2 < i {
doubleDouble = true
} else {
firstDouble = i
}
}
}
}
return ascendingTripple && doubleDouble
}
func Part1(puzzle aoclib.Puzzle) interface{} {
currentPassword := []byte(puzzle.GetInputArray()[0])
for !checkPasswordValid(currentPassword) {
incrementPassword(currentPassword)
}
return fmt.Sprintf("%s", string(currentPassword))
}
func Part2(puzzle aoclib.Puzzle) interface{} {
currentPassword := []byte(Part1(puzzle).(string))
incrementPassword(currentPassword)
for !checkPasswordValid(currentPassword) {
incrementPassword(currentPassword)
}
return fmt.Sprintf("%s", string(currentPassword))
}

1
inputs/11 Normal file
View File

@ -0,0 +1 @@
vzbxkghb

1
inputs/11_test Normal file
View File

@ -0,0 +1 @@
ghijklmn

View File

@ -12,6 +12,7 @@ import (
"aoc2015/day08"
"aoc2015/day09"
"aoc2015/day10"
"aoc2015/day11"
"flag"
"fmt"
"os"
@ -58,6 +59,9 @@ func initDayFunctions() {
dayFunctions[10] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[10][1] = day10.Part1
dayFunctions[10][2] = day10.Part2
dayFunctions[11] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[11][1] = day11.Part1
dayFunctions[11][2] = day11.Part2
}
func execute(thisDay int) {