Advertisement
Aliendreamer

my test problem colaboration

Jul 5th, 2020
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace testingdotnet
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var world = new string[,] { { "H", "H", "H" }, { "H", "I", "H" }, { "H", "H", "H" } };
  12.             var firstInfected = new int[2] { 1, 1 };
  13.  
  14.             string report = SurviveTheSpread(world,firstInfected);
  15.             Console.WriteLine(report);
  16.         }
  17.         public static string SurviveTheSpread(string[,]world,int[] firstInfected)
  18.         {
  19.             var people = FillPeople(world, firstInfected);
  20.             bool peopleGotIll = true;
  21.             int totalDays = 1;
  22.  
  23.             while (peopleGotIll)
  24.             {
  25.  
  26.                 UpdateQuarantines(people);
  27.                 var currentInfected = people.Where(x => x.Value.Status == "I")
  28.                     .OrderBy(x=>x.Key.Row)
  29.                     .Select(x=>x.Key).ToList();
  30.                 peopleGotIll = SpreadDesease(people, currentInfected);
  31.                 totalDays++;
  32.             }
  33.             int infected = people.Values.Where(x=>x.Status=="I").Count();
  34.             int recovered = people.Values.Where(x => x.Status == "R").Count();
  35.             int uninfected = people.Values.Where(x => x.Status == "H").Count();
  36.  
  37.             var results = new int[]
  38.             {
  39.                 totalDays,infected,recovered,uninfected
  40.             };
  41.  
  42.             return string.Join(" ",results);
  43.         }
  44.  
  45.         public static void UpdateQuarantines(Dictionary<Address,Occupant>people)
  46.         {
  47.            var infectedAdresses = people.Where(x=>x.Value.Status=="I").Select(x=>x.Key);
  48.            foreach (var address in infectedAdresses)
  49.            {
  50.                var healed = people[address].Quarantine;
  51.                people[address].Quarantine++;
  52.                people[address].Status= healed==3?"R":"I";
  53.            }
  54.         }
  55.  
  56.         public static bool SpreadDesease(Dictionary<Address,Occupant>people,List<Address>currentInfected)
  57.         {
  58.              bool newInfected= false;
  59.             foreach (var current in currentInfected)
  60.             {
  61.                 var result = Infect(people,current);
  62.                 if(!newInfected && result)
  63.                     newInfected =true;
  64.  
  65.             }
  66.  
  67.             return newInfected;
  68.         }
  69.  
  70.         public static bool Infect(Dictionary<Address,Occupant>people,Address address)
  71.         {
  72.             var possibleInfected = new List<Address>
  73.             {
  74.                 new Address
  75.                 {
  76.                     Col=address.Col,
  77.                     Row=address.Row+1
  78.                 },
  79.                 new Address
  80.                 {
  81.                     Col =address.Col-1,
  82.                     Row = address.Row
  83.                 },
  84.                 new Address
  85.                 {
  86.                     Col=address.Col,
  87.                     Row=address.Row-1
  88.                 },
  89.                 new Address
  90.                 {
  91.                     Col=address.Col+1,
  92.                     Row= address.Row
  93.                 }
  94.             };
  95.             foreach (var shouldBeInfected in possibleInfected)
  96.             {
  97.                 if(CanBeInfected(people,shouldBeInfected))
  98.                 {
  99.                     var adressToGetInfected = people.Keys.First(x => x.Col == shouldBeInfected.Col && x.Row == shouldBeInfected.Row);
  100.                     people[adressToGetInfected].Status = "I";
  101.                     people[adressToGetInfected].Quarantine = 1;
  102.                     return true;
  103.                 }
  104.             }
  105.             return false;
  106.         }
  107.         public static bool CanBeInfected(Dictionary<Address,Occupant>people,Address address)
  108.         {
  109.             return people.Any(x => x.Key.Col == address.Col && x.Key.Row == address.Row && x.Value.Status =="H");
  110.         }
  111.  
  112.         public static Dictionary<Address, Occupant> FillPeople(string[,] world, int[] firstInfected)
  113.         {
  114.             var startingPoing = new Dictionary<Address, Occupant>();
  115.             for (int i = 0; i < world.GetLength(0); i++)
  116.             {
  117.                 for (int j = 0; j < world.GetLength(1); j++)
  118.                 {
  119.                     string status = (i == firstInfected[0] && j == firstInfected[1]) ? "I" : world[i, j];
  120.                     int quarantine = status == "I" ? 1 : 0;
  121.                     var currentAddress = new Address
  122.                     {
  123.                         Row = i,
  124.                         Col = j
  125.                     };
  126.                     var occupant = new Occupant
  127.                     {
  128.                         Quarantine = quarantine,
  129.                         Status = status
  130.                     };
  131.                     startingPoing.Add(currentAddress, occupant);
  132.                 }
  133.             }
  134.             return startingPoing;
  135.         }
  136.     }
  137.     public class Address
  138.     {
  139.         public int Row { get; set; }
  140.         public int Col { get; set; }
  141.     }
  142.     public class Occupant
  143.     {
  144.         public string Status { get; set; }
  145.         public int Quarantine { get; set; }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement