Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shricodev/b1f8ba826ae933672f016079750ab81a to your computer and use it in GitHub Desktop.

Select an option

Save shricodev/b1f8ba826ae933672f016079750ab81a to your computer and use it in GitHub Desktop.

Alright, let's tackle this problem step by step. We have four people who need to cross a town in just 17 minutes using a car that can only hold two people at a time. Each person takes a different amount of time to "pass" or cross the town:

  • Person A: 1 minute
  • Person B: 2 minutes
  • Person C: 5 minutes
  • Person D: 10 minutes

When two people are in the car together, they move at the speed of the slower person. So, if Person A (1 min) and Person D (10 min) are in the car together, they'll take 10 minutes to cross.

Our goal is to get all four people across in no more than 17 minutes.

Initial Thoughts

This resembles the classic "bridge crossing" puzzle where people have to cross a bridge with similar constraints. The key is to minimize the time taken by optimizing who crosses and who returns with the car.

Generally, the strategy involves:

  1. Having the fastest individuals (those who take less time) be the ones who shuttle the car back and forth because they waste less time in returning trips.
  2. Pairing the slowest individuals together to avoid multiple slow crossings.

Let me try to outline a sequence of crossings.

Attempting a Sequence

Initial State:

  • Start Side: A (1), B (2), C (5), D (10)
  • Destination Side: None
  • Time Elapsed: 0 minutes

First Crossing: Who should go first? It might make sense to send the two fastest first because they can return quickly.

  • A and B go to the destination side.
  • Time taken: 2 minutes (since B is slower).
  • Total time: 2 minutes.

Now:

  • Start Side: C (5), D (10)
  • Destination Side: A (1), B (2)
  • Time: 2

Return Trip: Now, someone needs to bring the car back. It's best if the faster one returns to minimize time.

  • A returns.
  • Time taken: 1 minute.
  • Total time: 3 minutes.

Now:

  • Start Side: A (1), C (5), D (10)
  • Destination Side: B (2)
  • Time: 3

Second Crossing: Now, send the two slowest people across. This is crucial because we don't want them making multiple trips.

  • C and D go to the destination side.
  • Time taken: 10 minutes (since D is slowest).
  • Total time: 13 minutes.

Now:

  • Start Side: A (1)
  • Destination Side: B (2), C (5), D (10)
  • Time: 13

Return Trip: Now, someone needs to bring the car back so A can cross again. It's better if the faster person on the destination side returns.

  • B returns.
  • Time taken: 2 minutes.
  • Total time: 15 minutes.

Now:

  • Start Side: A (1), B (2)
  • Destination Side: C (5), D (10)
  • Time: 15

Final Crossing: Now, send A and B to the destination side.

  • A and B go to the destination side.
  • Time taken: 2 minutes.
  • Total time: 17 minutes.

Now:

  • Start Side: None
  • Destination Side: A (1), B (2), C (5), D (10)
  • Time: 17

Verifying the Steps

Let's list out the steps with times:

  1. A and B cross to the destination: 2 minutes. (Total: 2)
  2. A returns: 1 minute. (Total: 3)
  3. C and D cross to the destination: 10 minutes. (Total: 13)
  4. B returns: 2 minutes. (Total: 15)
  5. A and B cross to the destination: 2 minutes. (Total: 17)

Adding these up: 2 + 1 + 10 + 2 + 2 = 17 minutes. Perfect!

Checking for Optimality

Is there a way to do it in less than 17 minutes? Let me think.

Alternative idea: maybe send the fastest and the slowest first.

  1. A and D cross: 10 minutes. (Total: 10)
  2. A returns: 1 minute. (Total: 11)
  3. A and C cross: 5 minutes. (Total: 16)
  4. A returns: 1 minute. (Total: 17)
  5. A and B cross: 2 minutes. (Total: 19)

This totals 19 minutes, which is over the limit. So, the initial strategy is better.

Another alternative:

  1. A and B cross: 2. (Total: 2)
  2. B returns: 2. (Total: 4)
  3. C and D cross: 10. (Total: 14)
  4. A returns: 1. (Total: 15)
  5. A and B cross: 2. (Total: 17)

This also totals 17 minutes, same as the first method.

Both sequences take 17 minutes, but the first one seems more straightforward where the faster person does more returns.

Conclusion

The optimal strategy is:

  1. A and B cross to the destination: 2 minutes.
  2. A returns to the start: 1 minute.
  3. C and D cross to the destination: 10 minutes.
  4. B returns to the start: 2 minutes.
  5. A and B cross to the destination: 2 minutes.

This sequence totals exactly 17 minutes, ensuring all four people cross the town on time.

@shricodev
Copy link
Copy Markdown
Author

Prompt: Four people need to pass a town in 17 minutes using a single car that holds two people. One takes 1 minute to pass, another takes 2 minutes, the third takes 5 minutes, and the fourth takes 10 minutes. How do they all pass in time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment