Skip to content

Commit 6a9617d

Browse files
committed
Speed up solution 2015-11 (Corporate Policy) a bit
1 parent 913ca9d commit 6a9617d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/main/kotlin/de/ronny_h/aoc/year2015/day11/CorporatePolicy.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff 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 {

0 commit comments

Comments
 (0)