From 6f7fc2d1a878e0f31aebe9ab100fb0a4da346f6a Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 7 Dec 2024 13:10:21 +0100 Subject: [PATCH] Use substring iterator directly --- 2024/src/main/kotlin/Grid.kt | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/2024/src/main/kotlin/Grid.kt b/2024/src/main/kotlin/Grid.kt index 44bbda0..cfad170 100644 --- a/2024/src/main/kotlin/Grid.kt +++ b/2024/src/main/kotlin/Grid.kt @@ -13,23 +13,21 @@ class Grid(val rows: List) { fun chars(x: Int, y: Int, dx: Int, dy: Int) = Line(x, y, dx, dy) inner class Line(val x: Int, val y: Int, val dx: Int, val dy: Int) : Sequence { - override fun iterator(): Iterator = iterator { - if (dx == 1 && dy == 0) { - yieldAll(rows[y].substring(startIndex = x).asSequence()) - } else { + override fun iterator(): Iterator = if (dx == 1 && dy == 0) { + rows[y].substring(startIndex = x).iterator() + } else iterator { + var x = x + var y = y - var x = x - var y = y - - while (x in 0.. = sequence { for (y in 0..