File tree Expand file tree Collapse file tree 1 file changed +12
-10
lines changed
src/main/kotlin/de/ronny_h/aoc/year2015/day11 Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change @@ -13,13 +13,12 @@ class CorporatePolicy : AdventOfCode<String>(2015, 11) {
1313
1414 // must include one increasing straight of at least three letters
1515 fun rule1Applies (password : String ): Boolean {
16- var applies = false
17- password.windowed(3 ) {
18- if (it[2 ] == it[1 ].inc() && it[1 ] == it[0 ].inc()) {
19- applies = true
16+ for (i in 0 .. password.length - 3 ) {
17+ if (password[i].inc() == password[i + 1 ] && password[i + 1 ].inc() == password[i + 2 ]) {
18+ return true
2019 }
2120 }
22- return applies
21+ return false
2322 }
2423
2524 private val lettersToSkip = listOf (' i' , ' o' , ' l' )
@@ -30,20 +29,23 @@ class CorporatePolicy : AdventOfCode<String>(2015, 11) {
3029
3130 // must contain at least two different, non-overlapping pairs of letters
3231 fun rule3Applies (password : String ): Boolean {
33- var pairs = 0
32+ var foundOnePair = false
3433 var lastWasAPair = false
35- password.windowed( 2 ) {
34+ for (i in 0 .. password.length - 2 ) {
3635 if (lastWasAPair) {
3736 // no overlappings
3837 lastWasAPair = false
3938 } else {
40- if (it[0 ] == it[1 ]) {
41- pairs++
39+ if (password[i] == password[i + 1 ]) {
40+ if (foundOnePair) {
41+ return true
42+ }
43+ foundOnePair = true
4244 lastWasAPair = true
4345 }
4446 }
4547 }
46- return pairs >= 2
48+ return false
4749 }
4850
4951 private fun String.rotate (): String {
You can’t perform that action at this time.
0 commit comments