Feel free to send your feedback or ask some help here!
This was a HUGE task. For me the second hardest in CodinGame so far, after the Mars Lander puzzle. It really tweaked my imagination and got me thinking. I tried several solutions but non of them came close to 100% result, especially with the βYou may have to anticipate the arrival of some rocks if you want Indy to surviveβ¦β idea in the problem statement. In the end I decided to implement DFS as much optimized as possible, run it every turn when I see a collision danger for Indy and it worked quite well. Definitely one of my favorite puzzles.
Oh manβ¦ I was going here to see if anyone has the idea of solving thisβ¦ Such empty manβ¦
How far have you come with solving the task?
Or you have a specific problem/question?
Sorry for not replying you man⦠I thought no one answered so I had to figure out the solution by myself ~.~
This was funny oneβ¦ Itβs not that hard to solve, but itβs definitely hard to properly represent all the data and transformations. My solution is about 250 lines of code and in theory works as follows:
- Get all the paths from actual indi position to end including counter for rotations (0 = no rotation, +1 = left rotation, +2 = 2xleft rotation, -1 = right rotation) (very similar to image in Rotations section of this blogpost)
- For each Rock compute all βHit Pathsβ - paths, that rock has to follow in order to hit end of the board or wall of other piece. Also skip computation if rock at any point hits the position indi was already in (rock is behind indi)
- Iβve implemented funny middle step - mapping obtained paths to paths with boolean metadata for each position where hitting βtrueβ on some cell means, yo have to act on actual path in order to complete it - false true false means you can wait one turn, but you have to act on next one
- Term you are looking for to make all possible combinations of indi/rock paths is called cartesian product
- Finding solution: for each item of cartesian product:
β Shorten rock paths if they colide with each other
β If any rock path intercepts indi path (same x,y value at the same time) - NOT A SOLUTION
β If paths create paradox (tiles at any x,y for any pair path are not matching) - NOT A SOLUTION
β If canβt rotate cells fast enough - NOT A SOLUTION
β otherwise - SOLUTION - Pick any item of cartesian product marked as SOLUTION and do a move on path that requires interaction in nearest future
Worst part for me was data representation so Iβll post some snippets (Kotlin lang). Hopefully it isnβt too much to share and will help you:
- To define moves in a
Piece
Iβve used customMove
objects, for example:object TT : Move(Direction.TOP, Direction.TOP)
- This is how my
Piece9
looked like:object Piece9 : Piece(9, { Piece8 }, { Piece6 }, listOf(TT, LT))
- Tile was composed of
Piece
andcanRotate
flag - Board was composed as 2D Array of
Tiles
- Paths from step 1. and 2. were just list of -
x, y, enterDirection, requiredPiece, requiredRotations
- Path from step 3. is list of -
x, y, requiredPiece, haveToActFlag, requiredRotations
Cartesian Product in Kotlin:
fun <T> cartesianProduct(a: List<T>, vararg sets: List<T>): List<List<T>> =
(setOf(a) + sets)
.fold(listOf(listOf<T>())) { acc, set ->
acc.flatMap { list -> set.map { element -> list + element } }
}.toList()
ΠΏΠ°ΡΡ ΠΊΠ΅Π½ΡΡ ΠΏΠΎΠΌΠΎΠ³ΠΈΡΠ΅ Ρ Π·Π°Π΄ΡΠ΅ΠΉ ΠΏΠΆ ΠΌΠ΅Π½Ρ Ρ Π½Π°Π·ΡΠ²Π°Π΅ΡΡΡ The Fall - Episode 3 ΠΏΠΆΠΏΠΆΠΏΠΆΠΏΠΆΠΏΠΏΠΆΠΏΠΆΠΏΠΆΠΏΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΆΠΏΠΏΠΆΠΏΠΆΠΏΠΆΠΏΠΆΠΏΠΆ
I just passed the last two test cases βMultiple choice and rocksβ + βOnly one wayβ with my uncompleted solution, without considering the presence of the rocks at all. Thatβs odd! I currently have in place a DFS search to find all possible paths for the player. After this my intention was to find the hitpaths for the rocks and compare these paths to find the solution. Kind of disappointing!
Just to be clear, it does not pass the first two!