Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
895 B
5
Indexable
Never
const input = (`5
2 5 10 100
2 6 0 3
2 8 11 7
1 2 4 5
1 4 7 6`).split("\n");

const apples = [];
const cows = [];

for(i = 1; i < +input[0] + 1; i++) {

    const data = input[i].split(" ").map(Number);
    const time = data[1];
    const place = data[2];
    const count = data[3];

    if(data[0] == 2) for(x = 0; x < count; x++) apples.push({ time, place });
    else for(x = 0; x < count; x++) cows.push({ time, place });


};

let count = 0;

function grabApples() {

    let grabbed = 0;

    cows.forEach(cow => {

        let apple = apples.find(a => Math.abs(a.place - cow.place) <= (a.time - cow.time));
        if(apple) {

            apples.splice(apples.indexOf(apple), 1);
            cows.splice(cows.indexOf(cow), 1);
            grabbed++;

        };
    
    });

    count += grabbed;
    if(grabbed == 0) return console.log(count);
    else grabApples();

};

grabApples();