Skip to content

Commit d21733a

Browse files
committed
Improve solution 2025-02 (Gift Shop)
Just two small improvements: * use takeLast() instead of substring() to be nearer to take() in the line right above it * use chunked() instead of windowed() to be a bit more expressive
1 parent 348eb2e commit d21733a

File tree

1 file changed

+3
-7
lines changed
  • src/main/kotlin/de/ronny_h/aoc/year2025/day02

1 file changed

+3
-7
lines changed

src/main/kotlin/de/ronny_h/aoc/year2025/day02/GiftShop.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fun Long.isSequenceRepeatedTwice(): Boolean {
2727
return false
2828
}
2929
val first = id.take(id.length / 2)
30-
val second = id.substring(id.length / 2)
30+
val second = id.takeLast(id.length / 2)
3131
return first == second
3232
}
3333

@@ -37,12 +37,8 @@ fun Long.isSequenceRepeatedAtLeastTwice(): Boolean {
3737
if (id.length % size != 0) {
3838
continue
3939
}
40-
val subsequences = buildList {
41-
id.windowed(size, size) {
42-
add(it)
43-
}
44-
}
45-
if (subsequences.all { it == subsequences.first() }) {
40+
val chunks = id.chunked(size)
41+
if (chunks.all { it == chunks.first() }) {
4642
return true
4743
}
4844
}

0 commit comments

Comments
 (0)