Aliendreamer

test problem Chaov solution js

Jul 5th, 2020
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const world = [["H", "H", "H"], ["H", "I", "H"], ["H", "H", "H"]];
  2.  
  3. function getKey(x, y) { return `[${x}, ${y}]`; }
  4.  
  5. function makePerson(status, siblings) {
  6.     return { status, siblings, daysSinceInfected: status === "I" ? 1 : 0, }
  7. }
  8.  
  9. function graphPeople(world) {
  10.     const g = {};
  11.  
  12.     Object.defineProperties(g, {
  13.         "infectSiblings": {
  14.             enumerable: false,
  15.             value: function (siblings) {
  16.                 if (this[siblings.right].status === "H") {
  17.                     this.infect(siblings.right);
  18.                     return true;
  19.                 }
  20.                 if (this[siblings.top].status === "H") {
  21.                     this.infect(siblings.top);
  22.                     return true;
  23.                 }
  24.                 if (this[siblings.left].status === "H") {
  25.                     this.infect(siblings.left);
  26.                     return true;
  27.                 }
  28.                 if (this[siblings.bottom].status === "H") {
  29.                     this.infect(siblings.bottom);
  30.                     return true;
  31.                 }
  32.                 return false;
  33.             }
  34.         },
  35.         "infect": {
  36.             enumerable: false,
  37.             value: function (x) { this[x].status = "I"; }
  38.         },
  39.         "tryRecover": {
  40.             enumerable: false,
  41.             value: function (x) {
  42.                 this[x].daysSinceInfected++;
  43.                 if (this[x].daysSinceInfected === 3) {
  44.                     this[x].status = "R";
  45.                 }
  46.                 return this[x].siblings;
  47.             }
  48.         },
  49.         "getInfected": {
  50.             enumerable: false,
  51.             value: function () { return Object.values(this).filter(x => x.status === "I").length }
  52.         },
  53.         "getRecovered": {
  54.             enumerable: false,
  55.             value: function () { return Object.values(this).filter(x => x.status === "R").length }
  56.         },
  57.         "getHealthy": {
  58.             enumerable: false,
  59.             value: function () { return Object.values(this).filter(x => x.status === "H").length }
  60.         }
  61.     })
  62.  
  63.     for (let i = 0; i < world.length; i++) {
  64.         for (let j = 0; j < world[i].length; j++) {
  65.             const siblings = {}
  66.             if (world[i][j + 1])
  67.                 siblings.right = getKey(i, j + 1);
  68.             if (world[i - 1])
  69.                 siblings.top = getKey(i - 1, j);
  70.             if (world[i][j - 1])
  71.                 siblings.left = getKey(i, j - 1);
  72.             if (world[i + 1])
  73.                 siblings.bottom = getKey(i + 1, j);
  74.  
  75.             g[getKey(i, j)] = makePerson(world[i][j], siblings)
  76.         }
  77.     }
  78.     return new Proxy(g, {
  79.         get(t, p) {
  80.             if (Reflect.has(t, p)) { return Reflect.get(t, p) }
  81.             return { status: undefined }
  82.         }
  83.     });
  84. }
  85.  
  86. // spreads is 2 because we start from second step of infection!
  87. function calc(people, spreads = 2) {
  88.     let spread = Object.keys(people)
  89.         .filter(x => people[x].status === "I")
  90.         .map(x => people.tryRecover(x))
  91.         .map(x => people.infectSiblings(x))
  92.         .indexOf(true) > -1;
  93.  
  94.     if (spread) {
  95.         return calc(people, spreads + 1);
  96.     }
  97.  
  98.     return {
  99.         people,
  100.         daysUntilStopsSpreading: spreads,
  101.         infected: people.getInfected(),
  102.         recovered: people.getRecovered(),
  103.         healthy: people.getHealthy()
  104.     }
  105. }
  106.  
  107. console.log(
  108.     calc(graphPeople(world))
  109. )
Advertisement
Add Comment
Please, Sign In to add comment