In the board game Pandemic, an outbreak occurs when a city contains more than 3 disease cubes. When the outbreak occurs, any disease cubes in the city in excess of 3 are removed, and each city connected to it gains one disease cube. This means that chain reactions can, and will occur.

Important note: Each city may only outbreak once in each chain reaction.


A list of cities each containing:

  • An integer representing the number of disease cubes .
  • And a number of identifiers of the form (A, B ... AA, AB) (lowercase is allowed) representing which cites in list order it is connected to.


A list of integers representing the number of disease cubes in each city after outbreaks have been resolved.

Winning Condition

This is , so the shortest code in bytes wins.

  • 6
    \$\begingroup\$ Could you add test cases? \$\endgroup\$ – HyperNeutrino Jun 13 '19 at 16:10
  • \$\begingroup\$ I think it's clear enough. \$\endgroup\$ – Who Jun 13 '19 at 16:12
  • \$\begingroup\$ Are city edges bidirectional? Is it strictly greater than (that is, a pandemic starts upon a city gaining its fourth cube)? \$\endgroup\$ – HyperNeutrino Jun 13 '19 at 16:12
  • 3
    \$\begingroup\$ The point isn't that it's clear enough (which it isn't); test cases are to test people's solutions otherwise it's very inconvenient to verify that one's solution is valid. \$\endgroup\$ – HyperNeutrino Jun 13 '19 at 16:13
  • \$\begingroup\$ it would take time \$\endgroup\$ – Who Jun 13 '19 at 16:13

Python 3, 201 bytes

f=lambda d,e,v=[],n=enumerate:any((D>3)>(i in v)for i,D in n(d))and f([(i in v)and D or(min(3,D)+sum((k not in v)*(d[k]>3)for k in E))for i,(D,E) in n(zip(d,e))],e,v+[i for i,D in n(d)if D>3])or sum(d)

Try it online!

Worst case O(n) on the number of cities. d is a list of disease counts, e is a list of city connections by 0-indexed position corresponding to d, and v is the visited array (which should not be given as input, obviously).

For the case [4, 3, 2] with connections 0 <-> 1, 1 <-> 2, and 0 <-> 2, first, city 0 outbreaks and becomes 3 and sets city 1 to 4 and city 2 to 3. Then, city 1 outbreaks and becomes 3 and sets city 2 to 4 (city 0 already had an outbreak). Finally, city 2 has an outbreak and nothing can be affected, leaving all three cities on 3 disease cubes, totalling 9.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.