Facebook Careers Puzzle—swarm solution in Ruby
From Facebook’s Job Puzzles page:
You are the Zerg Queen in command of the 654195331th legion of Zerg forces, the only side worth playing in StarCraft. The wise Overmind has given you the important task of cleaning up some remote planets for mineral mining operations. Apparently, some pesky Terran forces have decided to set up base defenses in these locations, prior to your arrival. With your limited forces, you must determine which Terran bases to attack. Your Zerg forces on each planet have free movement over that one planet, and may split up to attack any number of Terran bases on that planet. However, your Zerg ground forces have not yet evolved space flight and thus cannot travel from planet to planet to assist each other.
Odds of victory over a particular Terran base are equal to:
P(z,s) = e(-63s+10)/(e(-63s+10)+e(-21z))… etc
This problem is hard. On the surface, it seems like an optimization problem of two variables: minerals vs zerg, and then on top of that, deciding on which bases to attack. The equation above may look daunting, but with some pretty basic pre-calc, can be simplified. Look at the comments in my solution for the steps I took.
The first problem is deciding whether a base is worth attacking, and how many zerg are needed. That’s not the end of the story, as you can actually attack each base with different amounts of zerg to get different amounts of minerals. It’s a cluster..bleep. Thank goodness for the exp(), as it drastically limits the possible amount of forces to use for each base. I had to use a trick to make it work for large minerals. Can you spot it and figure out why it works?
After that, it’s just another 0-1 knapsack problem to solve, with thankfully only a very few amount of possibilities to consider.
For testing, I used forum David’s test generator to make this file (4MB).
time ./swarm swarm.txt > out
real 0m21.126s
user 0m20.167s
sys 0m0.107s
MD5 (out) = 14fc14d34d05d485e1e261a86dd5a499
The following solution in Ruby passed the Facebook puzzle robot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |