Skip to content

Instantly share code, notes, and snippets.

@ourai
Last active December 2, 2019 08:57
Show Gist options
  • Save ourai/efd6e38a8e7fc59bbd1a5acfafdc0bd8 to your computer and use it in GitHub Desktop.
Save ourai/efd6e38a8e7fc59bbd1a5acfafdc0bd8 to your computer and use it in GitHub Desktop.
Get water from water dispenser
type WaterVolume = number;
interface IPerson {
id: number; // 人员标识
competent: boolean; // 是否有能力换水
altruistic: boolean; // 是否乐于助人
}
// 所有员工
const staffs: IPerson[] = [];
/**
* 饮水机剩余水量
*/
function getRemainWaterOfDispenser(): WaterVolume {
return 0;
}
/**
* 重装水桶
*
* @param person 换水人
*/
function reloadWaterBucket(person: IPerson): boolean {
return true;
}
/**
* 从饮水机接水
*
* @param person 接水人
* @param need 需要接的水量
*/
function getWaterFromDispenser(person: IPerson, need: WaterVolume): boolean {
const remainNeed = need - getRemainWaterOfDispenser();
if (remainNeed < 0 || (remainNeed === 0 && (!person.competent || !person.altruistic))) {
return true;
}
if (remainNeed === 0 || person.competent) {
return reloadWaterBucket(person);
}
const helper = staffs.find((s: IPerson): boolean => s.id !== person.id && s.altruistic && s.competent);
if (!helper) {
throw new Error('没水喝,渴着吧!');
}
return reloadWaterBucket(helper);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment