r/dailyprogrammer • • May 28 '14
[5/28/2014] Challenge #164 [Intermediate] Part 3 - Protect The Bunkers
Description
Most of the residential buildings have been destroyed by the termites due to a bug in /u/1337C0D3R's code. All of the civilians in our far-future society now live in bunkers of a curious design - the bunkers were poorly designed using the ASCII Architect and are thus not secure. If the bunkers are breached by a hostile force, it is almost certain that all the civilians will die.
The high-tech termites have developed a taste for human flesh. Confident from their victory at the building lines, they are now staging a full attack on the bunkers. The government has hired you to design protective walls against the termite attacks. However, their supplies are limited, so you must form a method to calculate the minimum amount of walls required.
A map of an area under assault by evil termites can be described as a 2d array of length m and width n. There are five types of terrain which make up the land:
- *: A termite nest. Termites can pass through here. The termites begin their assault here. Protective walls cannot be placed here.
: Impassible terrain. Termites cannot pass through here. Protective walls cannot be placed here.
- +: Unreliable terrain. Termites can pass through here. Protective walls cannot be placed here.
- -: Reliable terrain. Termites can pass through here. Protective walls can be placed here.
- o: Bunker. Termites can pass through here. If they do, the civilians die a horrible death. Protective walls cannot be placed here.
Termites will begin their attack from the nest. They will then spread orthogonally (at right angles) through terrain they can pass through.
A map will always follow some basic rules:
- There will only be one nest.
- Bunkers will always be in a single filled rectangle (i.e. a contiguous block).
- A bunker will never be next to a nest.
- There will always be a solution (although it may require a lot of walls).
Formal Inputs And Outputs
Input Description
Input will be given on STDIN, read from a file map.txt, or supplied as a command line argument. The first line of input will contain 2 space separated integers m and n. Following that line are n lines with m space seperated values per line. Each value will be one of five characters: *, #, +, -, or o.
Input Limits
1 <= n < 16
3 <= m < 16Output Description
Output will be to STDOUT or written to a file output.txt. Output consists of a single integer which is the number of walls required to protect all the bunkers.
Sample Inputs and Outputs
Sample Input 1
6 6
++++*
-#+++
--#++
ooo--
ooo-
Sample Output 1
2
(The walls in this example are placed as follows, with @ denoting walls:
++++*
@#+++
--#++
ooo@-
ooo-
Notes
Thanks again to /u/202halffound
3
u/mian2zi3 May 28 '14
I'm new to /r/dailyprogrammer. Is this related to #164 easy? Is the intention we write it in a/the language we've never used before?
7
u/KillerCodeMonky May 28 '14 edited May 28 '14
Did this one in C#. Working with multi-dimensional arrays in PowerShell is a huge pain. This is REALLY FAST, solving even /u/chunes 15x15s about as fast as I can hit the enter key.
EDIT: Figured out how to find the walled spaces! Also put code into .NET Fiddle with the pathological case from /u/skeeto.
Code
https://dotnetfiddle.net/MQCRTF
.NET Fiddle does not output in a fixed-width font, so you'll have to copy-paste into something to see the output correctly. I wrote a UserVoice ticket for it; feel free to vote.
Explanation
6
u/chunes 1 2 May 28 '14 edited May 28 '14
Here are some more sample inputs:
3
u/Fruglemonkey 1 0 May 28 '14
Seems kinda hard at first glance...
3
u/skeeto -9 8 May 28 '14 edited May 28 '14
C++11. I started out with a greedy solver that finds a non-optimal solution instantly, but decided it wasn't interesting enough. This version brute-force searches for an optimal solution (minimum required walls).
Unfortunately this means it takes hours to solve /u/chunes's last two maps in any reasonable period of time. There are 132 reliable locations on the fourth map with an optimal solution of 7 walls. This means my program has to try at least 5 trillion and up to 798 trillion possible solutions.
In addition to the count, it outputs the solution map, too.
2
u/Godspiral 3 3 May 28 '14 edited May 28 '14
in J,
here is random map generation instead of spec:
utilities:
eval =: 1 : ' a: 1 : m'
advswap =: 2 : (':';'m x v eval y')
codes=. '#+-o*'
rndbox =: [: <@:(+ ([: i. 2 >. ]))/"1 ] (] ,.~ [: ? 2 >. <:@:<:@:<:@:-) [: ? <:
rndnest =: (_1 -.~ ] , <:@:{. , >:@{:) each
redditfmt"1 codes {~ (] $ ([: (] {~ [: ? #) [: I. [: , 0 (< rndnest a)} $&1) 4 advswap '}' [:, 3 (<a)} [: ? ] $ 3:) b [ a=. rndbox b=.6 8
all of those are solvable afaik.
rndbox draws a random box leaving at least 1 space away from bottom and right edge. Ensures it is at least 2x2. Example output of box
rndbox 6 6
left side is row indexes to make box, right is columns within those rows. This is used for bunkers.
to find the nest, just make a bigger square around the bunkers
redditfmt"1 ": rndnest rndbox 6 6
which is to add a previous and subsequent row and column, and then remove any -1's. A random number that does not fall within this larger box's index range is generated for nest.
row and column indexes do not have to be in order when feeding to } (amend)
2
u/mbcook Jun 01 '14 edited Jun 02 '14
OK I got a chance to start on this last night. At this point I'm about 5 hours in. I'm using Haskell and I've put it in a GitHub repo:
https://github.com/MBCook/BunkerMaster
It can solve some problems, but gives up (or produces an incorrect output) on others. Instead of using A* and just continually blocking that path I decided to try something very different. I'm doing a flood fill from both positions on the map and putting a wall wherever they meet (within constraints). I keep track of two bits for each position on the map: if the termites have been there and if the humans have been there. If both get set it becomes a wall. This isn't the minimal wall set, but I think I can make it work. It doesn't print out the number of walls right now, just the final map.
The problem is that humans aren't allowed to 'walk' where there are +s in my code since they wouldn't be able to put walls there. That means that I can't solve maps that involve them being trapped by +s. Here are some examples of what it's doing:
EDIT: Spent another 1/2 hour and got it fixed (since I already knew what was going on). Updated solutions below. We often get double thickness walls, but that's not too bad. Humans often control a lot of territory.
Example:
Here are the 'solutions' to a few of /u/chunes maps. This first one works, though non-optimal:
And the one row one is easy, again it's overwalled:
Not only is this little guy solved, I believe it's the optimal solution:
Humans control quite a bit of territory here:
This last one took a lot of walls (it's non-optimal), but the humans are safe.
I could avoid the double placement issue if I kept track of the 'source' cell that I was coming from and re-checked to see if it got walled during the last update, but I'm not going to go that far. This works well enough for my fun.
Total time: 5h 30m.
1
u/YouAreNotASlave May 28 '14
I'm inclined to use a brute force approach...
I'd code this up but I'm flat out today. Will attempt tomorrow.