DEV Community

Discussion on: Daily Challenge #274 - Aerial Firefighting

Collapse
 
aminnairi profile image
Amin • Edited

C

Assuming the input for the fire area is always correct.

#include <math.h>

int getWaterBombsNeeded(const char* area, unsigned int tank) {
    const char BUILDING = 'Y';
    unsigned int fire = 0;
    double bombs = 0;

    for (const char* current = area; *current; current++) {
        if (BUILDING == *current) {
            bombs += ceil(fire / (float) tank);
            fire = 0;
        } else {
            fire++;
        }
    }

    if (fire) {
        bombs += ceil(fire / (float) tank);
    }

    return bombs;
}

Test