Pandemic Outbreak Calculator

-3

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.

Input

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.

Output

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.

Who

Posted 2019-06-13T16:05:37.123

Reputation: 233

Question was closed 2019-06-13T17:40:29.013

6Could you add test cases? – HyperNeutrino – 2019-06-13T16:10:35.573

I think it's clear enough. – Who – 2019-06-13T16:12:37.630

Are city edges bidirectional? Is it strictly greater than (that is, a pandemic starts upon a city gaining its fourth cube)? – HyperNeutrino – 2019-06-13T16:12:40.090

3The 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. – HyperNeutrino – 2019-06-13T16:13:04.873

it would take time – Who – 2019-06-13T16:13:33.807

and yes to both of those questions – Who – 2019-06-13T16:14:00.793

Let us continue this discussion in chat.

– HyperNeutrino – 2019-06-13T16:14:11.073

A list of integers is a string of integers separated by something other than a digit (whitespace, newline, etc.) ok? – Benjamin Urquhart – 2019-06-13T16:18:05.397

Answers

2

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.

HyperNeutrino

Posted 2019-06-13T16:05:37.123

Reputation: 26 575