37
2
Inspired by this.
Background
The evil farmer has decided to burn your wheat field down in order to drive up the prices. To ensure total destruction, he has also soaked your field in gasoline. Even more unfortunately, you happened to be walking on the field when it was lit on fire, and you must get out quickly to survive.
Challenge
Given a field containing wheat, fire and your location, determine if you can make it out of the field in time.
A field consists of wheat (here represented by .
) and fire (F
). Here your location is marked with a O
. For example:
...F...F
F.......
........
.F......
....O...
...F....
........
.F....F.
Every second you move to any adjacent cell (but not diagonally), and every fire spreads to every adjacent cell. If you can't move to a cell that will not be on fire, you die. If you make it out of the field, you survive. Let's see what happens in this example:
...F...F
F.......
........
.F......
....O...
...F....
........
.F....F.
..FFF.FF
FF.F...F
FF......
FFF.....
.F.F.O..
..FFF...
.F.F..F.
FFF..FFF
FFFFFFFF
FFFFF.FF
FFFF...F
FFFF....
FF.FF.O.
.FFFFFF.
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFF.FF
FFFFF.FF
FFFFFFFO
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFF
FFFFFFFFO <-- you made it out and survived, barely
FFFFFFFF
FFFFFFFF
FFFFFFFF
Rules
- Your input is the field as a grid. You may choose any input format, including a string with line separators or a 2D array.
- You may not take as input the locations for fire and/or yourself.
- You may use any 3 distinct values as wheat, fire and your position, including non-strings for array input.
- Fields are always at least 1x1 in size, rectangular and contain no invalid characters.
- Any field will contain exactly one of the value representing your location, and every other position may or may not be fire.
- Your output is one of two distinct values for "you survive" or "you die", as usual in decision-problem.
- Standard code-golf rules apply.
Test cases
Survived
O
....
.O..
....
FFFFF
.....
..O..
.....
FFFF
FFFO
FFFF
.F....
......
......
.F....
..O...
.FF...
.F....
..FF..
...F...F
F.......
........
.F......
....O...
...F....
........
.F....F.
Didn't survive
FFF
FOF
FFF
F.F
.O.
F.F
....F
.....
..O..
.....
F....
.F....F.
........
........
F..O....
........
.....F..
...F...F
F......F
........
.F......
....O...
...F....
........
.F....F.
F..F
.O..
FF..
This is kinda like the 'Meteor Showers' USACO problem – Oliver Ni – 2017-08-08T12:34:49.163
2I don't see why someone downvoted – Oliver Ni – 2017-08-08T12:35:07.353
@OliverNi Probably because of the inspired by comment, instead of reading it they saw that thought it was a chameleon and down voted. – TheLethalCoder – 2017-08-08T12:39:35.243
3To both downvoters, please explain why my challenge is bad. – PurkkaKoodari – 2017-08-08T13:14:23.167
1Why is there this restriction: "You may not take as input the locations for fire and/or yourself."? It just adds task to find O and Fs in input – Dead Possum – 2017-08-08T13:14:28.477
6@DeadPossum Because I feel like it would simplify the challenge too much and make it a bit too broad. Feel free to disagree, though; if others agree with you I might change the restriction. – PurkkaKoodari – 2017-08-08T13:16:05.220
2I agree with Pietu1998, I also feel that the restriction is highly appropriate. – Mr. Xcoder – 2017-08-08T13:19:50.297
@Mr.Xcoder
Any field will contain exactly one of your location
– PurkkaKoodari – 2017-08-08T14:04:22.597In all the truthy test cases you can escape following a straight line. Actually the general problem is more complicated if you have to consider non-straight paths. Since that seems to be the case, you should add a test case where you can escape but not following a straight path (or clarify that only straight paths need be considered) – Luis Mendo – 2017-08-08T16:21:26.537
2@LuisMendo If it is possible to escape when the farmer turns, it's always possible for him/her to escape in a straight line. For instance, let's say the farmer is trying to escape to the right of the field. When the farmer moves one space down, some fire will spread downwards; then, the farmer's situation is the same as the initial position (plus more fire). – JungHwan Min – 2017-08-08T16:30:14.680
I, speaking as the evil farmer, would never soak the field in gasoline first. I would tie you up, place you in the exact center of the field, and then start fires at the corners. Then I would watch as the fire slowly moves toward you so I can enjoy your terror longer. Much more evil that way. – Gryphon – 2017-08-08T16:33:03.577
Additionally, the above plan makes it impossible for you to get out alive, as you will be tied up. – Gryphon – 2017-08-08T16:35:46.053
@LuisMendo When writing the challenge I did notice that considering straight paths is sufficient. I thought about mentioning it, but I decided to let the answerers figure it out. – PurkkaKoodari – 2017-08-08T17:00:28.437