Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace testingdotnet
- {
- class Program
- {
- static void Main(string[] args)
- {
- var world = new string[,] { { "H", "H", "H" }, { "H", "I", "H" }, { "H", "H", "H" } };
- var firstInfected = new int[2] { 1, 1 };
- string report = SurviveTheSpread(world,firstInfected);
- Console.WriteLine(report);
- }
- public static string SurviveTheSpread(string[,]world,int[] firstInfected)
- {
- var people = FillPeople(world, firstInfected);
- bool peopleGotIll = true;
- int totalDays = 1;
- while (peopleGotIll)
- {
- UpdateQuarantines(people);
- var currentInfected = people.Where(x => x.Value.Status == "I")
- .OrderBy(x=>x.Key.Row)
- .Select(x=>x.Key).ToList();
- peopleGotIll = SpreadDesease(people, currentInfected);
- totalDays++;
- }
- int infected = people.Values.Where(x=>x.Status=="I").Count();
- int recovered = people.Values.Where(x => x.Status == "R").Count();
- int uninfected = people.Values.Where(x => x.Status == "H").Count();
- var results = new int[]
- {
- totalDays,infected,recovered,uninfected
- };
- return string.Join(" ",results);
- }
- public static void UpdateQuarantines(Dictionary<Address,Occupant>people)
- {
- var infectedAdresses = people.Where(x=>x.Value.Status=="I").Select(x=>x.Key);
- foreach (var address in infectedAdresses)
- {
- var healed = people[address].Quarantine;
- people[address].Quarantine++;
- people[address].Status= healed==3?"R":"I";
- }
- }
- public static bool SpreadDesease(Dictionary<Address,Occupant>people,List<Address>currentInfected)
- {
- bool newInfected= false;
- foreach (var current in currentInfected)
- {
- var result = Infect(people,current);
- if(!newInfected && result)
- newInfected =true;
- }
- return newInfected;
- }
- public static bool Infect(Dictionary<Address,Occupant>people,Address address)
- {
- var possibleInfected = new List<Address>
- {
- new Address
- {
- Col=address.Col,
- Row=address.Row+1
- },
- new Address
- {
- Col =address.Col-1,
- Row = address.Row
- },
- new Address
- {
- Col=address.Col,
- Row=address.Row-1
- },
- new Address
- {
- Col=address.Col+1,
- Row= address.Row
- }
- };
- foreach (var shouldBeInfected in possibleInfected)
- {
- if(CanBeInfected(people,shouldBeInfected))
- {
- var adressToGetInfected = people.Keys.First(x => x.Col == shouldBeInfected.Col && x.Row == shouldBeInfected.Row);
- people[adressToGetInfected].Status = "I";
- people[adressToGetInfected].Quarantine = 1;
- return true;
- }
- }
- return false;
- }
- public static bool CanBeInfected(Dictionary<Address,Occupant>people,Address address)
- {
- return people.Any(x => x.Key.Col == address.Col && x.Key.Row == address.Row && x.Value.Status =="H");
- }
- public static Dictionary<Address, Occupant> FillPeople(string[,] world, int[] firstInfected)
- {
- var startingPoing = new Dictionary<Address, Occupant>();
- for (int i = 0; i < world.GetLength(0); i++)
- {
- for (int j = 0; j < world.GetLength(1); j++)
- {
- string status = (i == firstInfected[0] && j == firstInfected[1]) ? "I" : world[i, j];
- int quarantine = status == "I" ? 1 : 0;
- var currentAddress = new Address
- {
- Row = i,
- Col = j
- };
- var occupant = new Occupant
- {
- Quarantine = quarantine,
- Status = status
- };
- startingPoing.Add(currentAddress, occupant);
- }
- }
- return startingPoing;
- }
- }
- public class Address
- {
- public int Row { get; set; }
- public int Col { get; set; }
- }
- public class Occupant
- {
- public string Status { get; set; }
- public int Quarantine { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement