diff --git a/day11/day11.go b/day11/day11.go new file mode 100644 index 0000000..3ff1822 --- /dev/null +++ b/day11/day11.go @@ -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)) +} diff --git a/inputs/11 b/inputs/11 new file mode 100644 index 0000000..0b7a426 --- /dev/null +++ b/inputs/11 @@ -0,0 +1 @@ +vzbxkghb \ No newline at end of file diff --git a/inputs/11_test b/inputs/11_test new file mode 100644 index 0000000..cb0890d --- /dev/null +++ b/inputs/11_test @@ -0,0 +1 @@ +ghijklmn \ No newline at end of file diff --git a/main.go b/main.go index 740ca61..b4e5fd8 100644 --- a/main.go +++ b/main.go @@ -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) {