[Community Puzzle] Forest Fire

https://www.codingame.com/training/medium/forest-fire

Send your feedback or ask for help here!

Created by @AlBlazing,validated by @eulerscheZahl,@bbb000bbbyyy and @JBM.
If you have any issues, feel free to ping them.

Errrā€¦ I canā€™t seem to get a submission through.
The IDEā€™s results pane is stuck in a ā€œcomputing scoreā€ status.

2 Likes

Fixed.
The validators have to be named test<number>.json, not validator<number>.json.
I renamed them and reuploaded.

In the validation process I would really like to see the puzzle in a way as it will be shown after approval (including hidden statement for reverse clashes).

3 Likes

18/06 /2019 - 17:10

Still stuck for me.

a

Also, 1 on every 3 attempts at stage 2, I get this error:

Thanks, now it works.

1 Like

Test 02 fails on 3/5 runs for me with:

The game has crashed. Please contact the author and enclose the following error:

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
   at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
   at java.base/java.util.Objects.checkIndex(Objects.java:372)
   at java.base/java.util.ArrayList.get(ArrayList.java:458)
   at com.codingame.game.Referee.init(Referee.java:55)
   at com.codingame.gameengine.core.GameManager.start(GameManager.java:111)
   at com.codingame.gameengine.core.RefereeMain.start(RefereeMain.java:69)
   at com.codingame.gameengine.core.RefereeMain.main(RefereeMain.java:52)
1 Like

Can you share the outputs of your bot to reproduce the crash?

Edit:
the game crashes on this line:
L = Integer.parseInt(gameManager.getTestCaseInput().get(0));
The referee randomly fails reading the map from the testcase. It works most of the time but not always. I think thatā€™s the SDKā€™s fault, not the game itself.

2 Likes
The game has crashed. Please contact the author and enclose the following error:

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at com.codingame.game.Referee.gameTurn(Referee.java:160)
	at com.codingame.gameengine.core.GameManager.start(GameManager.java:122)
	at com.codingame.gameengine.core.RefereeMain.start(RefereeMain.java:69)
	at com.codingame.gameengine.core.RefereeMain.main(RefereeMain.java:52)

As I wrote above, that line 55 reads the testcase input:

L = Integer.parseInt(gameManager.getTestCaseInput().get(0));

For whatever reason it failed to do so.
I had a similar issue with Bender, where one validator was almost always failing. A simple reupload of the game solved the problem.

Maybe reuploading will work here as well, but instead I would like staff to have a look (ping @TwoSteps) as I think the cause goes beyond the puzzle itself.

I had to play the 2nd testcase about 10 times to reproduce the crash. I think any player code should do the job, as the testcase gets loaded before executing the player.

1 Like

I can pass all IDE tests, but not with the same code :frowning:
The statement suggests ā€˜greedy algorithmsā€™ so I did not implement simulation just selecting ā€˜bestā€™ next action from all possible ones.
However, if I sort the actions by ā€˜cost per fireā€™, ā€˜fireā€™, ā€˜vehiclesizeā€™ then IDE test L fails, all other OK including XL.
If I sort the actions by ā€˜fireā€™, ā€˜cost per fireā€™, ā€˜vehiclesizeā€™ then IDE test XL fails, all others OK.
It seems I still need to take into account if an actions make good action in subsequent turn impossible - but that would need simulation and search tree which I wanted to avoid by being greedy.
Any ideas, what is the good greediness criteria to use?

2 Likes

As @TBali pointed out, greedy with weighting also didnā€™t work for me. I got 100% with some random weights associated to the fires doused, and a check for immediate completion. But I DONā€™T pass all test cases, so itā€™s not a correct solution. Can the author of the puzzle please explain what greedy approach is intended by the tag :slight_smile:

A few weeks after my earlier post I finally passed this puzzle with the greedy method by using a manual (but fixed) precedence of the possible actions. (A fixed ā€œ[coversize][firecount] => priorityā€ lookup table, I found by try-and-error.) Not nice, but at least not ā€˜test-case specific hardcodedā€™. In retrospect, the resulting order has some logic , but not one that I thought of by myself :slight_smile:

1 Like

Congratulations on not having hard coded :pensive:. But I donā€™t follow your algo. Could you elaborate?

Code excerpt (in PHP syntax, sorry if not your prefered oneā€¦)

const Order = array(
// [cover][fire] => precedence
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 => [ 0, 7, 0, 0, 0, 0, 0, 0, 0, 0],
2 => [ 0, 8, 6, 5, 4, 0, 0, 0, 0, 0],
3 => [ 0, 13, 12, 11, 10, 9, 3, 2, 1, 0]
);
static function compare3(array $a, array $b): int {
$a1 =self::Order[$a[ā€˜coverā€™]][$a[ā€˜fireā€™]];
$b1 =self::Order[$b[ā€˜coverā€™]][$b[ā€˜fireā€™]];
return $a1 <=> $b1;
}

Preparation:
I generate an array for the 14 possible actions (where cover*cover <= fire)
Could have used a class for the Action, used associative array instead.

tryOrder[] = array(ā€˜coverā€™ => $coverSize, ā€˜fireā€™ => $fireSize, ā€˜costā€™ => $perFireCost);

then sort it using the fixed priority above:

usort($tryOrder, array(ā€˜ForestFireā€™, ā€˜compare3ā€™));

Then I try to use the actions in this sorted order

foreach ($tryOrder as $try)
// check if current action is still possible (have enough water and have suitable target) and use it

1 Like

I have to wonder if I missed the point of the exercise as I was able to solve the puzzle without taking water usage into consideration (my program assumed there would always be enough water to put out all the fires).

My program just went from most dense to least dense, firing the appropriate amount of water to cover the intended area. I passed every test case, and I was wondering if my solution was possibly too long or if the puzzle wasnā€™t able to fool the conditions that I had set for it.

It doesnā€™t work for me. Iā€™m getting this error :
java.lang.NumberFormatException: For input string: ā€œā€
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.codingame.game.Referee.gameTurn(Referee.java:168)
at com.codingame.gameengine.core.GameManager.start(GameManager.java:122)
at com.codingame.gameengine.core.RefereeMain.start(RefereeMain.java:69)
at com.codingame.gameengine.core.RefereeMain.main(RefereeMain.java:52)

Iā€™ve got an problem too !
I donā€™t change the code (for Java language).

If I keep the initial result : System.out.println("C 0 0");, it works.
If I replace the result : System.out.println("J 0 0");, i have no result : screen result is all empty.
And if I do only System.out.println("J");, iā€™ve got an error

Blockquote
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

Thx for your help !

Indeed, it is possible to solve the puzzle without taking care of water, but if you want to use the less possible water (in the general case, I have not check in the problem tests) you want to maximize the availability of remaining water to extinguish the next firesā€¦

Reading through the other posts, Iā€™m pretty sure itā€™s just a bug, but I figured Iā€™d mention it - The default output command does not give a warning, however, when adding my own code I receive this - ā€œWarning: your code did not read all available input before printing an instruction, your outputs will not be synchronized with the gameā€™s turns and unexpected behaviour may occur.ā€ My code was added inside of the while true loop and after the for loop where the fireā€™s x and y variables are initialized, so Iā€™m fairly certain that all of the inputs have been read unless I am missing something, but like I said, after reading through the forum, I feel it is most likely a bug. So far it doesnā€™t seem to hinder my ability to properly solve this puzzle, it just caught me off guard.

The puzzle expects a single line of output per turn and it seems youā€™re outputting more than one line, hence desynchronizing the input and ouput in the later turns.