Perseus

MyFirstTask

Jul 2nd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.12 KB | None | 0 0
  1. // Task
  2. =====================================================================================================
  3. Codenavirus
  4. Assignment
  5. Codenavirus is strange contagious disease. This is why:
  6.  
  7. One infected person will infect only one healthy person next to him each day
  8. If a person is infected, he will infect the person on his right, unless the latter is already infected, then he will infect the person on his top instead, but if that person is also already infected, then he will infect the one on his left, lastly if this one is also already infected, the he will infect the one below if this one is still healthy. (The examples will clarify)
  9. A newly infected person will start infecting other people one day after the infection.
  10. An infected person will recover after exactly 3 days of illness, counting from the day he got infected, and he will become immune to the virus (he won't be infected anymore)
  11. To help the WHO study this new virus, you must create a program that simulates the spread of the virus.
  12.  
  13. Inputs
  14.  
  15. world: a matrix of characters representing the proximity of people in a the world. A person is represented by "#" and empty spaces are represented by ".". One person is next to another one if they share the same row OR the same column and there is no empty space or another person between them.
  16. firstInfected: an array of integer of size 2 ([row, col]) storing the coordinates of the first person to contract the virus on day 1.
  17. Output
  18. An array of 4 integers [a, b, c, d] where:
  19.  
  20. a : number of days until the virus stops spreading
  21. b : number of infected people at the end of the spread
  22. c : number of recovered people at the end
  23. d : number of uninfected people
  24. Prioritize lower indexes if there is a case where one person is about to be infected by two neighbors, in that case the neighbor with lower row/col will win (Eg. Day 3 of test 4).
  25.  
  26. Example
  27.  
  28. For
  29. world = [["#","#","#"],
  30.         ["#","#","#"],
  31.         ["#","#","#"]]
  32. and firstInfected = [1, 1], the answer is [7, 3, 6, 0]
  33. Explanation: H for healthy, I for infected, R for recovered
  34.  
  35. DAY 0:
  36. [
  37. ["H", "H", "H"],
  38. ["H", "H", "H"],
  39. ["H", "H", "H"],
  40. ]
  41. DAY 1:
  42. [
  43. ["H", "H", "H"],
  44. ["H", "I", "H"],
  45. ["H", "H", "H"],
  46. ]
  47. DAY 2:
  48. [
  49. ["H", "H", "H"],
  50. ["H", "I", "I"],
  51. ["H", "H", "H"],
  52. ]
  53. DAY 3:
  54. [
  55. ["H", "I", "I"],
  56. ["H", "I", "I"],
  57. ["H", "H", "H"],
  58. ]
  59. DAY 4:
  60. [
  61. ["I", "I", "I"],
  62. ["H", "R", "I"],
  63. ["H", "H", "I"],
  64. ]
  65. DAY 5:
  66. [
  67. ["I", "I", "I"],
  68. ["I", "R", "R"],
  69. ["H", "I", "I"],
  70. ]
  71. DAY 6:
  72. [
  73. ["I", "R", "R"],
  74. ["I", "R", "R"],
  75. ["I", "I", "I"],
  76. ]
  77. DAY 7:
  78. [
  79. ["R", "R", "R"],
  80. ["I", "R", "R"],
  81. ["I", "I", "R"],
  82. ]
  83. [input] array.array.char world
  84.  
  85. [input] array.integer firstInfected
  86.  
  87. [output] array.integer
  88.  
  89.  
  90. Deliverables
  91. a single .cs file
  92. the file must contain the following method signature, otherwise your submission is considered invalid
  93. int[] Codenavirus (char[][] world, int[] firstInfected) {
  94.  
  95. }
  96. 3. The application is tested automatically and global variables are forbidden.
  97.  
  98. ================================================================================================================
  99.  
  100. // Solution
  101. ================================================================================================================
  102.  
  103. using System;
  104.  
  105. namespace Codenavirus
  106. {
  107.    public class Program
  108.    {
  109.        public static void Main(string[] args)
  110.        {
  111.            char[][] world = new char[][]
  112.            {
  113.                new char[] { '#', '#', '#' },
  114.                 new char[] { '#', '#', '#' },
  115.                 new char[] { '#', '#', '#' },
  116.             };
  117.  
  118.             int[] firstInfected = new int[2] { 1, 1 };
  119.  
  120.             var output = new Program();
  121.             var results = output.Codenavirus(world, firstInfected);
  122.  
  123.             foreach (var score in results)
  124.             {
  125.                 Console.WriteLine(score);
  126.             }
  127.            
  128.         }
  129.  
  130.         public int[] Codenavirus(char[][] world, int[] firstInfected)
  131.         {
  132.             int[] populationHealthStatus = new int[4];
  133.             int days = 0;
  134.             int indexRow = firstInfected[0];
  135.             int indexCol = firstInfected[1];
  136.             const int indexRecover = 3;
  137.  
  138.             if (world[indexRow][indexCol] == '#')
  139.             {
  140.                 world[indexRow][indexCol] = 'I';
  141.                 days++;
  142.  
  143.                 int[][] worldStatusBoard = CreateHealthStatusBoard(world);
  144.  
  145.                 worldStatusBoard[indexRow][indexCol] = indexRecover;
  146.  
  147.                 Print(world);
  148.                
  149.                 bool virusSpread = true;
  150.  
  151.                 while (virusSpread == true)
  152.                 {
  153.                     virusSpread = false;
  154.  
  155.                     RecoverInfectedPeople(world, worldStatusBoard);
  156.                     SpreadCodenavirus(world, worldStatusBoard, ref virusSpread);
  157.                     CureInfectedPeople(world, worldStatusBoard);
  158.                     days++;
  159.                     QuarantinePeople(world, worldStatusBoard, indexRecover);
  160.                     Print(world);
  161.                 }
  162.  
  163.             }
  164.  
  165.             populationHealthStatus = ScoreHealthStatus(world, days);
  166.  
  167.             return populationHealthStatus;
  168.         }
  169.  
  170.         private void QuarantinePeople(char[][] world, int[][] worldStatusBoard, int indexRecover)
  171.         {
  172.             for (int i = 0; i < world.Length; i++)
  173.             {
  174.                 for (int j = 0; j < world[i].Length; j++)
  175.                 {
  176.                     if (world[i][j] == 'I' && worldStatusBoard[i][j] == 0)
  177.                     {
  178.                         worldStatusBoard[i][j] = indexRecover;
  179.                     }
  180.                 }
  181.             }
  182.         }
  183.  
  184.         private void CureInfectedPeople(char[][] world, int[][] worldStatusBoard)
  185.         {
  186.             for (int i = 0; i < world.Length; i++)
  187.             {
  188.                 for (int j = 0; j < world[i].Length; j++)
  189.                 {
  190.                     if (world[i][j] == 'I' && worldStatusBoard[i][j] != 0)
  191.                     {
  192.                         worldStatusBoard[i][j]--;
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.  
  198.         private void RecoverInfectedPeople(char[][] world, int[][] worldStatusBoard)
  199.         {
  200.             for (int i = 0; i < world.Length; i++)
  201.             {
  202.                 for (int j = 0; j < world[i].Length; j++)
  203.                 {
  204.                     if (world[i][j] == 'I' && worldStatusBoard[i][j] == 1)
  205.                     {
  206.                         world[i][j] = 'R';
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.  
  212.         private void Print(char[][] world)
  213.         {
  214.             for (int i = 0; i < world.Length; i++)
  215.             {
  216.                 for (int j = 0; j < world[i].Length; j++)
  217.                 {
  218.                     Console.Write($"{world[i][j]} ");
  219.                 }
  220.  
  221.                 Console.WriteLine();
  222.             }
  223.  
  224.             Console.WriteLine();
  225.         }
  226.  
  227.         private int[][] CreateHealthStatusBoard(char[][] model)
  228.         {
  229.             int[][] board = new int[model.Length][];
  230.  
  231.             for (int i = 0; i < model.Length; i++)
  232.             {
  233.                 board[i] = new int[model[i].Length];
  234.             }
  235.  
  236.             return board;
  237.         }
  238.  
  239.         private int[] ScoreHealthStatus(char[][] world, int days)
  240.         {
  241.             int infected = 0;
  242.             int recovered = 0;
  243.             int uninfected = 0;
  244.  
  245.             for (int i = 0; i < world.Length; i++)
  246.             {
  247.                 for (int j = 0; j < world[i].Length; j++)
  248.                 {
  249.                     switch (world[i][j])
  250.                     {
  251.                         case '#':
  252.                             uninfected++;
  253.                             break;
  254.                         case 'I':
  255.                             infected++;
  256.                             break;
  257.                         case 'R':
  258.                             recovered++;
  259.                             break;
  260.                         default:
  261.                             break;
  262.                     }
  263.                 }
  264.             }
  265.  
  266.             int[] healthStatusScore = new int[] { days, infected, recovered, uninfected };
  267.  
  268.             return healthStatusScore;
  269.         }
  270.  
  271.         private void SpreadCodenavirus(char[][] world, int[][] worldStatusBoard, ref bool virusSpread)
  272.         {
  273.             for (int i = 0; i < world.Length; i++)
  274.             {
  275.                 for (int j = 0; j < world[i].Length; j++)
  276.                 {
  277.                     if (world[i][j] == 'I' && worldStatusBoard[i][j] != 0)
  278.                     {
  279.                         if (j + 1 < world[i].Length && world[i][j + 1] == '#')
  280.                         {
  281.                             world[i][j + 1] = 'I';
  282.                             virusSpread = true;
  283.                             continue;
  284.                         }
  285.                         else if (i - 1 >= 0 && j<world[i-1].Length && world[i - 1][j] == '#')
  286.                         {
  287.                             world[i - 1][j] = 'I';
  288.                             virusSpread = true;
  289.                             continue;
  290.                         }
  291.                         else if (j - 1 >= 0 && world[i][j - 1] == '#')
  292.                         {
  293.                             world[i][j - 1] = 'I';
  294.                             virusSpread = true;
  295.                             continue;
  296.                         }
  297.                         else if (i + 1 < world.Length && j < world[i + 1].Length && world[i + 1][j] == '#')
  298.                         {
  299.                             world[i + 1][j] = 'I';
  300.                             virusSpread = true;
  301.                             continue;
  302.                         }
  303.                     }
  304.  
  305.                 }
  306.             }
  307.  
  308.         }
  309.     }
  310. }
  311. ====================================================================================================================
  312.  
  313. // Result
  314. ====================================================================================================================
  315.  
  316. # # #
  317. # # #
  318. # # #
  319.  
  320. # # #
  321. # I #
  322. # # #
  323.  
  324. # # #
  325. # I I
  326. # # #
  327.  
  328. # I I
  329. # I I
  330. # # #
  331.  
  332. I I I
  333. # R I
  334. # # I
  335.  
  336. I I I
  337. I R R
  338. # I I
  339.  
  340. I R R
  341. I R R
  342. I I I
  343.  
  344. R R R
  345. I R R
  346. I I R
  347.  
  348. 7
  349. 3
  350. 6
  351. 0
Add Comment
Please, Sign In to add comment