LOGO100 logo

Stars in their eyes

Your challenge is to return the amount of green pixels in the following image:

stars with dimensions

He’re is a JSON dataset with the dimensions of each star.

[
    { "width": 200, "height": 300 },
    { "width": 200, "height": 150 },
    { "width": 100, "height": 100 }
]

Solution

OK, this one was slightly evil. The trick is to not get distracted by the complex shape of the star, but to realise that the black parts of each of them make up an oval or circle as shown in this animation:

slicing the star and moving each part so it becomes an oval

In JavaScript, using the dataset, this could be:

let pixels = 0;
stardata.forEach(star => {
    pixels += Math.floor(
        star.width * star.height - 
        (Math.PI * star.width/2 * star.height/2)
    );
});

Which results in 21460 pixels.

Back to all puzzles