Guest User

Cellular Automata

a guest
Jul 17th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. Expanding Nebula
  2. ================
  3.  
  4. You've escaped Commander Lambda's exploding space station along with numerous escape pods full of bunnies. But - oh no! - one of the escape pods has flown into a nearby nebula, causing you to lose track of it. You start monitoring the nebula, but unfortunately, just a moment too late to find where the pod went. However, you do find that the gas of the steadily expanding nebula follows a simple pattern, meaning that you should be able to determine the previous state of the gas and narrow down where you might find the pod.
  5.  
  6. From the scans of the nebula, you have found that it is very flat and distributed in distinct patches, so you can model it as a 2D grid. You find that the current existence of gas in a cell of the grid is determined exactly by its 4 nearby cells, specifically, (1) that cell, (2) the cell below it, (3) the cell to the right of it, and (4) the cell below and to the right of it. If, in the current state, exactly 1 of those 4 cells in the 2x2 block has gas, then it will also have gas in the next state. Otherwise, the cell will be empty in the next state.
  7.  
  8. For example, let's say the previous state of the grid (p) was:
  9. .O..
  10. ..O.
  11. ...O
  12. O...
  13.  
  14. To see how this grid will change to become the current grid (c) over the next time step, consider the 2x2 blocks of cells around each cell. Of the 2x2 block of [p[0][0], p[0][1], p[1][0], p[1][1]], only p[0][1] has gas in it, which means this 2x2 block would become cell c[0][0] with gas in the next time step:
  15. .O -> O
  16. ..
  17.  
  18. Likewise, in the next 2x2 block to the right consisting of [p[0][1], p[0][2], p[1][1], p[1][2]], two of the containing cells have gas, so in the next state of the grid, c[0][1] will NOT have gas:
  19. O. -> .
  20. .O
  21.  
  22. Following this pattern to its conclusion, from the previous state p, the current state of the grid c will be:
  23. O.O
  24. .O.
  25. O.O
  26.  
  27. Note that the resulting output will have 1 fewer row and column, since the bottom and rightmost cells do not have a cell below and to the right of them, respectively.
  28.  
  29. Write a function answer(g) where g is an array of array of bools saying whether there is gas in each cell (the current scan of the nebula), and return an int with the number of possible previous states that could have resulted in that grid after 1 time step. For instance, if the function were given the current state c above, it would deduce that the possible previous states were p (given above) as well as its horizontal and vertical reflections, and would return 4. The width of the grid will be between 3 and 50 inclusive, and the height of the grid will be between 3 and 9 inclusive. The answer will always be less than one billion (10^9).
  30.  
  31. Languages
  32. =========
  33.  
  34. To provide a Python solution, edit solution.py
  35. To provide a Java solution, edit solution.java
  36.  
  37. Test cases
  38. ==========
  39.  
  40. Inputs:
  41. (boolean) g = [[true, false, true], [false, true, false], [true, false, true]]
  42. Output:
  43. (int) 4
  44.  
  45. Inputs:
  46. (boolean) g = [[true, false, true, false, false, true, true, true], [true, false, true, false, false, false, true, false], [true, true, true, false, false, false, true, false], [true, false, true, false, false, false, true, false], [true, false, true, false, false, true, true, true]]
  47. Output:
  48. (int) 254
  49.  
  50. Inputs:
  51. (boolean) g = [[true, true, false, true, false, true, false, true, true, false], [true, true, false, false, false, false, true, true, true, false], [true, true, false, false, false, false, false, false, false, true], [false, true, false, false, false, false, true, true, false, false]]
  52. Output:
  53. (int) 11567
Add Comment
Please, Sign In to add comment