Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Problems/Sleep-Time/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Sleep Time

You're setting an alarm for the next morning and know how long you want to sleep. The job is to figure out what time you need to go to bed to get that amount of sleep.

Return the time when you should go to bed in the 24-hour-format ("HH:MM")

## Documentation

### Solution Idea

The Idea is to subtract the duration you want to sleep from the time you want to wake up. If you now don't have any negativ values, this is the solution.
If any of the values are negativ, start with the minutes. We now add the negativ minute value from 60 and have with that the minute when we need to go to bed. If we did this we also need to subtract one from the hour value.
Now we check if the hour value is negativ. If yes, we add this value to 24. These values are now our solution.

---

### [Implementation](./solver.ts)

---

### Information's %Informations where the problem comes from. Most of them came from Sloth Byte%

This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com).
[Post](https://slothbytes.beehiiv.com/p/two-factor-codes) from August 26, 2025.
38 changes: 38 additions & 0 deletions Problems/Sleep-Time/sleep-time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import bedTime from "./solver";

describe("Returns always the correct time to go to sleep", () => {
it("should return '00:00' for start time '07:50' and duration '07:50'", () => {
expect(bedTime("07:50", "07:50")).toEqual("00:00");
});

it("should return '20:15' for start time '06:15' and duration '10:00'", () => {
expect(bedTime("06:15", "10:00")).toEqual("20:15");
});

it("should return '22:00' for start time '08:00' and duration '10:00'", () => {
expect(bedTime("08:00", "10:00")).toEqual("22:00");
});

it("should return '23:30' for start time '09:30' and duration '10:00'", () => {
expect(bedTime("09:30", "10:00")).toEqual("23:30");
});

it("should return '01:45' for start time '05:45' and duration '04:00'", () => {
expect(bedTime("05:45", "04:00")).toEqual("01:45");
});

it("should return '02:40' for start time '07:10' and duration '04:30'", () => {
expect(bedTime("07:10", "04:30")).toEqual("02:40");
});
});

describe("Throws the correct error when having the wrong time format", () => {
it("should throw an Error because the hour is bigger than allowed", () => {
expect(() => bedTime("26:23", "00:00")).toThrowError("The given times haven't the correct format!");
});

it("should throw an Error because the minute is bigger than allowed", () => {
expect(() => bedTime("00:59", "00:80")).toThrowError("The given times haven't the correct format!");
});
});
33 changes: 33 additions & 0 deletions Problems/Sleep-Time/solver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const checkBoundaries = (time: string) => {
const hour = Number.parseInt(time.split(":")[0], 10);
const minutes = Number.parseInt(time.split(":")[1], 10);

if (hour > 24 || minutes > 59 || hour < 0 || minutes < 0)
throw new Error("The given times haven't the correct format!");
};

export default function bedTime(wakeTime: string, sleepTime: string): string {
checkBoundaries(wakeTime);
checkBoundaries(sleepTime);

const endTimeHour = Number.parseInt(wakeTime.split(":")[0], 10);
const endTimeMinutes = Number.parseInt(wakeTime.split(":")[1], 10);

const durationTimeHour = Number.parseInt(sleepTime.split(":")[0], 10);
const durationTimeMinutes = Number.parseInt(sleepTime.split(":")[1], 10);

let startTimeHours = endTimeHour - durationTimeHour;
let startTimeMinutes = endTimeMinutes - durationTimeMinutes;

if (startTimeMinutes < 0) {
startTimeMinutes = 60 + startTimeMinutes;

startTimeHours--;
}

if (startTimeHours < 0) {
startTimeHours = 24 + startTimeHours;
}

return `${startTimeHours < 9 ? `0${startTimeHours}` : startTimeHours}:${startTimeMinutes < 9 ? `0${startTimeMinutes}` : startTimeMinutes}`;
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This repository is about solving coding-problems. It contains famous (or not tha
- [Dependable-Jobs-Schedule](./Problems/Dependable-Jobs-Schedule/README.md) (<span style="color:green">Easy</span>)
- [Nom-Nom-Numbers](./Problems/Nom-Nom-Numbers/README.md) (<span style="color:green">Easy</span>)
- [Shortes-Path-Dijkstra](./Problems/Shortes-Path-Dijkstra/README.md) <span style="color:orange">Medium</span>
- [Sleep-Time](./Problems/Sleep-Time/README.md) (<span style="color:green">Easy</span>)

<!-- PROBLEMS_END -->

Expand Down