Skip to content

Commit b9c1373

Browse files
committed
Implement the Traverser
1 parent d8a38ba commit b9c1373

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.org.codingteam.icfpc_2025
2+
3+
object Traverser:
4+
def traverse(solution: SolutionDefinition, route: Seq[Int]): Seq[Int] =
5+
var currentRoomIndex = solution.startingRoom
6+
var result = List(solution.rooms(currentRoomIndex))
7+
for (openDoor <- route)
8+
val fromConnection = solution.connections
9+
.filter { conn => conn.from.room == currentRoomIndex && conn.from.door == openDoor }
10+
.toVector
11+
val toConnection = solution.connections
12+
.filter { conn => conn.to.room == currentRoomIndex && conn.to.door == openDoor }
13+
.toVector
14+
15+
if (fromConnection.size > 1 || toConnection.size > 1)
16+
throw new Exception(s"Invalid solution: found more than one connection for door $openDoor in room $currentRoomIndex.")
17+
18+
if (fromConnection.nonEmpty && toConnection.nonEmpty && fromConnection.head != toConnection.head)
19+
throw new Exception(s"Invalid solution: connection $fromConnection does not match $toConnection (room $currentRoomIndex, door $openDoor).")
20+
21+
if (fromConnection.isEmpty && toConnection.isEmpty)
22+
throw new Exception(s"Invalid solution: cannot find a door $openDoor in room $currentRoomIndex.")
23+
24+
val connection = if fromConnection.nonEmpty then fromConnection.head else toConnection.head
25+
currentRoomIndex = if connection.from.room == currentRoomIndex then connection.to.room else connection.from.room
26+
result = currentRoomIndex :: result
27+
28+
result.reverse
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.org.codingteam.icfpc_2025
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class TraverserTests extends AnyFunSuite:
6+
7+
test("simple route") {
8+
val solution = SolutionDefinition(
9+
Vector(0, 1, 2),
10+
0,
11+
Vector(
12+
ConnectionDefinition(Door(0, 0), Door(1, 0)),
13+
ConnectionDefinition(Door(1, 1), Door(2, 1)),
14+
ConnectionDefinition(Door(2, 2), Door(0, 2))
15+
)
16+
)
17+
val route = Seq(0, 1, 2)
18+
val result = Traverser.traverse(solution, route)
19+
assert(result == Seq(0, 1, 2, 0))
20+
}

0 commit comments

Comments
 (0)