day11
This commit is contained in:
parent
b3d99b6975
commit
a3abcfd0c5
68
day11/day11.go
Normal file
68
day11/day11.go
Normal 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_test
Normal file
1
inputs/11_test
Normal file
@ -0,0 +1 @@
|
|||||||
|
ghijklmn
|
||||||
4
main.go
4
main.go
@ -12,6 +12,7 @@ import (
|
|||||||
"aoc2015/day08"
|
"aoc2015/day08"
|
||||||
"aoc2015/day09"
|
"aoc2015/day09"
|
||||||
"aoc2015/day10"
|
"aoc2015/day10"
|
||||||
|
"aoc2015/day11"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -58,6 +59,9 @@ func initDayFunctions() {
|
|||||||
dayFunctions[10] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
|
dayFunctions[10] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
|
||||||
dayFunctions[10][1] = day10.Part1
|
dayFunctions[10][1] = day10.Part1
|
||||||
dayFunctions[10][2] = day10.Part2
|
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) {
|
func execute(thisDay int) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user