dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
119 lines
4.0 KiB
Markdown
119 lines
4.0 KiB
Markdown
---
|
|
id: 69d03e549613fbdbe21d11e0
|
|
title: "Challenge 271: Medication Reminder"
|
|
challengeType: 28
|
|
dashedName: challenge-271
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an array of medications and a string representing the current time, return the next medication you need to take and how long until you need to take it.
|
|
|
|
- Each medication is in the format `[name, lastTaken]`, where `name` is the name of the medication and `lastTaken` is the time it was last taken.
|
|
- All given times will be in `"HH:MM"` (24-hour) format.
|
|
|
|
Use the following medication schedule:
|
|
|
|
| Name | Schedule |
|
|
| - | - |
|
|
| Deployxitrin | 08:00, 16:00 |
|
|
| Debuggamanizole | 07:00, 13:00, 21:00 |
|
|
| Mergeflictamine | every 4 hours |
|
|
|
|
Return a string in the format `"{name} in Hh Mm"`. For example, `"Debuggamanizole in 2h 0m"` or `"Deployxitrin in 1h 5m"`.
|
|
|
|
# --hints--
|
|
|
|
`medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "10:00"]], "11:00")` should return `"Debuggamanizole in 2h 0m"`.
|
|
|
|
```js
|
|
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "10:00"]], "11:00"), "Debuggamanizole in 2h 0m");
|
|
```
|
|
|
|
`medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "14:55")` should return `"Deployxitrin in 1h 5m"`.
|
|
|
|
```js
|
|
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "14:55"), "Deployxitrin in 1h 5m");
|
|
```
|
|
|
|
`medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "17:15")` should return `"Mergeflictamine in 0h 45m"`.
|
|
|
|
```js
|
|
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "17:15"), "Mergeflictamine in 0h 45m");
|
|
```
|
|
|
|
`medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "09:00"]], "12:59")` should return `"Debuggamanizole in 0h 1m"`.
|
|
|
|
```js
|
|
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "09:00"]], "12:59"), "Debuggamanizole in 0h 1m");
|
|
```
|
|
|
|
`medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "21:00"], ["Mergeflictamine", "03:00"]], "06:55")` should return `"Debuggamanizole in 0h 5m"`.
|
|
|
|
```js
|
|
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "21:00"], ["Mergeflictamine", "03:00"]], "06:55"), "Debuggamanizole in 0h 5m");
|
|
```
|
|
|
|
`medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "07:30"]], "08:00")` should return `"Mergeflictamine in 3h 30m"`.
|
|
|
|
```js
|
|
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "07:30"]], "08:00"), "Mergeflictamine in 3h 30m");
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function medicationReminder(medications, currentTime) {
|
|
|
|
return medications;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function medicationReminder(medications, currentTime) {
|
|
const schedule = {
|
|
Deployxitrin: { type: 'fixed', times: ['08:00', '16:00'] },
|
|
Debuggamanizole: { type: 'fixed', times: ['07:00', '13:00', '21:00'] },
|
|
Mergeflictamine: { type: 'interval', hours: 4 }
|
|
};
|
|
|
|
const toMinutes = t => {
|
|
const [h, m] = t.split(':').map(Number);
|
|
return h * 60 + m;
|
|
};
|
|
|
|
const current = toMinutes(currentTime);
|
|
let earliest = Infinity;
|
|
let nextMed = '';
|
|
|
|
for (const [name, lastTaken] of medications) {
|
|
const med = schedule[name];
|
|
let nextDue;
|
|
|
|
if (med.type === 'fixed') {
|
|
const futureTimes = med.times
|
|
.map(toMinutes)
|
|
.filter(t => t > current);
|
|
if (futureTimes.length === 0) continue;
|
|
nextDue = Math.min(...futureTimes);
|
|
} else {
|
|
nextDue = toMinutes(lastTaken) + med.hours * 60;
|
|
}
|
|
|
|
const diff = nextDue - current;
|
|
if (diff > 0 && diff < earliest) {
|
|
earliest = diff;
|
|
nextMed = name;
|
|
}
|
|
}
|
|
|
|
const h = Math.floor(earliest / 60);
|
|
const m = earliest % 60;
|
|
return `${nextMed} in ${h}h ${m}m`;
|
|
}
|
|
```
|