[Community Puzzle] By train or by car?

To illustrate my solution process I’ll use Montpellier Perpignan.

Montpellier Perpignan
Cholet Clisson 35.9
Beziers Narbonne 34.2
Orleans Blois 63
Montpellier Beziers 73.1
Tours Saumur 78
Angouleme Jarnac 33.2
Jarnac Cognac 11
Cognac Saintes 28.1
Saintes Saujon 27.7
Saujon Royan 13.3
Clisson Nantes 33.7
Blois Tours 65
Angers Cholet 65
Saumur Angers 66
Narbonne Roquefort-des-Corbieres 27
Salses-le-Château Perpignan 17.6
Paris Chartres 91
Roquefort-des-Corbieres Salses-le-Château 28.9

First I put all the distances in a list (I’m using python btw):

distances = [35.9, 34.2, 63.0, 73.1, 78.0, 33.2, 11.0, 28.1, 27.7, 13.3, 33.7, 65.0, 65.0, 66.0, 27.0, 17.6, 91.0, 28.9]
# sum(distances) = 791.7

==== Train Part ====

Next up I put the information train information from the Goal section of the puzzle into variables:

time_to_start_station = 35 # minutes
train_fast_speed = 60/284 # min/km
train_slow_speed = 60/50 # min/km
train_slow_distance = 3 # km
train_slow_time = train_slow_distance * train_slow_speed # minutes
train_stop_time = 8 # minutes
time_from_end_station = 30 # minutes

Now I add:

time_to_start_station + time_from_end_station + # Start + Enf
train_stop_time * distances_length +  # Total stop time
distances_length * train_slow_distance * 2 * train_slow_speed + # Total slow time
(sum(distances) - train_slow_distance * 2 * distances_length) * train_fast_speed
# Total fast time

And we get train_time = 475.043661971831 minutes or 7:55 hours.

==== Car Part ====

For the car I again create and assign variables for the information we know about traveling by car:

car_fast_speed = 60/105 # min/km
car_slow_speed = 60/50 # min/km
car_slow_distance = 7 # km
car_slow_time = car_slow_distance * car_slow_speed # minutes

I split the distances to separate values < 14

big_distances = [35.9, 34.2, 63.0, 73.1, 78.0, 33.2, 28.1, 27.7, 33.7, 65.0, 65.0, 66.0, 27.0, 17.6, 91.0, 28.9]
little_distances = [11.0, 13.3]

Now I add:

sum(little_distances) * car_slow_speed + # Little Distances slow time
big_distances_length * car_slow_distance * 2 * car_slow_speed + # Big Distances slow time
(sum(big_distances) - car_slow_distance * 2 * big_distances_length) * car_fast_speed
# Total fast time

And we get car_time = 608.4742857142859 minutes or 10:08 hours.

Since 7:55 hours is significantly less than 10:08 hours the train is faster.

[EDIT/Summary: My approach here is to sum up all the time travelled for all the distances provided - the original wall of text can be found in the previous version of the message.]

Didn’t expect you to write that long :sweat_smile: I thought it would be better for you to simplify the explanation like the section “Details by train” in the puzzle statement. Easier to catch your own error that way too…

The issue with your answers: You did not really travel from start to end. You travelled to irrelevant places too - I mean in your calculation.

You did not really travel from start to end.

I’m sorry, I don’t quite understand.

In case 8, you want to go from Montpellier to Perpignan. Are Cholet and Clisson, for example, really required to be part of that journey?

No idea, didn’t look at the map… :stuck_out_tongue_closed_eyes:

I shouldn’t have assumed that the places were all connected. That said the Goal section didn’t exactly mention or hint that not all inputs would be relevant.

All cities will appear only one time as starting point and/or end point. (no loop possible)

I guess that didn’t mean that all cities will appear only one time as starting point and/or end point on the path we need to take.

That said I still think this needs fixing:

The author wrote the goal section in evaluated form and forgot to show his work in some places:

35 + 3*60/50 + 127*60/284 + 3*60/50 + 8 + 3*60/50 + 212*60/284 + 3*60/50 + 30 = 159.01 minutes (2:39)

It should have been written as:

35 + 3*60/50 + (133-3*2)*60/284 + 3*60/50 + 8 + 3*60/50 + (218-3*2)*60/284 + 3*60/50 + 30 = 159.01 minutes (2:39)

He did the operation for his example but didn’t show that he subtracted slow_distance*2 from the distances between Paris ---- 133 km ----> Orléans and Orléans ---- 218 km -----> Tours.

Well the names of the places are provided as inputs for a reason :wink:

As for the calculation, technically the author is not wrong. But I agree that it may not be clear/detailed enough.

Well thanks for all the help… I’m off to write a state machine… :sweat:

Should I delete my big wall of text?

1 Like

Maybe simplify a lot of it instead of just deleting it :stuck_out_tongue:


How about replacing all the text with [EDIT: My initial approach is to sum up all the time travelled - the original details can be found in the previous version of the message.]? You may still just delete the message if you want. It’s your choice.

Good idea, I think I’ll leave a bit of detail to summarize.

1 Like

Maybe the example used in the Goal section should have an unnecessary line in it as well
and the puzzle should be labeled with filtering or something. Just a thought.

How about changing the original statement in the Goal section from

You must go from one city to another one. You want to choose between the train and the car, whichever is faster to reach your destination. According to the following benefits and constraints, make the right decision.


You are planning a journey from your current city to a destination city. This involves selecting which routes to take (some routes may be irrelevant to you), as well as choosing between the train and the car, whichever is faster to reach your destination. Make the right decision according to the following benefits and constraints.

1 Like

That sounds perfect.

Thanks. I have updated the statement.

1 Like

Hi ! Nice puzzle
My code passed all tests without much difficulty but the 8th validator fails.
Any suggestion ?

Does your code handle correctly the case where distance between 2 locations is less than 14?

Yes. I handle less than 14 for the car and less than 6 for the train.
Here are the times I found for the 7th test (where we have 11km between Jarnac and Cognac

train: 150.59859154929578
car: 106.41714285714286

How did you calculate the figures? Can you show the calculations?