Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution(egg1, egg2) {
- for(i = 0; i < 100; i++) {
- egg1.drop(i);
- if(egg1.broken) return i;
- }
- }
- class Egg {
- constructor(building) {
- this.building = building;
- this.broken = false;
- this.drops = 0;
- }
- drop(floor) {
- if(this.broken) throw new Error("Jajecznica");
- this.broken = floor >= this.building.breakPoint;
- this.drops++;
- }
- }
- class Building {
- constructor(floors) {
- this.floors = floors;
- this.breakPoint = Math.floor(Math.random() * floors);
- }
- getEggs(n) {
- return [...new Array(n)].map(() => new Egg(this));
- }
- }
- const stats = [];
- const tries = 1000000;
- let lastPrint = 0;
- process.stdout.write("0%");
- for(let i = 0; i < tries; i++) {
- const building = new Building(100);
- const eggs = building.getEggs(2);
- const result = solution(...eggs);
- if(result !== building.breakPoint) throw new Error(`Wrong answer, answer is ${building.breakPoint}, got ${result}`);
- stats.push(eggs.map(egg => egg.drops).reduce((a, b) => a + b));
- if(Math.floor(i / tries * 100) > lastPrint) {
- lastPrint = Math.floor(i / tries * 100);
- process.stdout.cursorTo(0);
- process.stdout.write(lastPrint + "%");
- }
- }
- process.stdout.cursorTo(0);
- console.log("Stats");
- console.log("Min: " + stats.reduce((p, v) => p < v ? p : v));
- console.log("Max: " + stats.reduce((p, v) => p > v ? p : v));
- console.log("Avr: " + (stats.reduce((p, v) => p + v)) / stats.length);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement