Manabase building can be modeled as a linear program with integer variables, where all color requirements must be met while maximizing the number of untapped sources. There are solvers for these type of problems, like the amazing jsLPSolver, which I used here.