SHARE
TWEET

Untitled

szymski Oct 17th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(egg1, egg2) {
  2.     for(i = 0; i < 100; i++) {
  3.         egg1.drop(i);
  4.         if(egg1.broken) return i;
  5.     }
  6. }
  7.  
  8.  
  9. class Egg {
  10.     constructor(building) {
  11.         this.building = building;
  12.         this.broken = false;
  13.         this.drops = 0;
  14.     }
  15.  
  16.     drop(floor) {
  17.         if(this.broken) throw new Error("Jajecznica");
  18.         this.broken = floor >= this.building.breakPoint;
  19.         this.drops++;
  20.     }
  21. }
  22.  
  23. class Building {
  24.     constructor(floors) {
  25.         this.floors = floors;
  26.         this.breakPoint = Math.floor(Math.random() * floors);
  27.     }
  28.  
  29.     getEggs(n) {
  30.         return [...new Array(n)].map(() => new Egg(this));
  31.     }
  32. }
  33.  
  34. const stats = [];
  35. const tries = 1000000;
  36. let lastPrint = 0;
  37. process.stdout.write("0%");
  38.  
  39. for(let i = 0; i < tries; i++) {
  40.     const building = new Building(100);
  41.     const eggs = building.getEggs(2);
  42.     const result = solution(...eggs);
  43.     if(result !== building.breakPoint) throw new Error(`Wrong answer, answer is ${building.breakPoint}, got ${result}`);
  44.     stats.push(eggs.map(egg => egg.drops).reduce((a, b) => a + b));
  45.     if(Math.floor(i / tries * 100) > lastPrint) {
  46.         lastPrint = Math.floor(i / tries * 100);
  47.         process.stdout.cursorTo(0);
  48.         process.stdout.write(lastPrint + "%");
  49.     }
  50. }
  51.  
  52. process.stdout.cursorTo(0);
  53. console.log("Stats");
  54. console.log("Min: " + stats.reduce((p, v) => p < v ? p : v));
  55. console.log("Max: " + stats.reduce((p, v) => p > v ? p : v));
  56. console.log("Avr: " + (stats.reduce((p, v) => p + v)) / stats.length);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top