10
My daughter had the following assignment for her math homework. Imagine six friends living on a line, named E, F, G, H, J and K. Their positions on the line are as indicated (not to scale) below:
Thus, F lives five units from E, and two units from G, and so forth.
Your assignment: craft a program that identifies a path that visits each friend exactly once with a total length of n units, taking the locations of the friends and n as inputs. It should report the path if it finds it (for example, for length 17 it might report "E,F,G,H,J,K", and it should exit gracefully if no solution exists. For what it's worth, I completed an ungolfed solution in Mathematica in 271 bytes. I suspect it's possible much more concisely than that.
3This might be better as a program that takes inputs (ex.
[0, 5, 7, 13, 16, 17]
and62
) so that you can make sure it's not specifically hard-coded to this case. – Doorknob – 2015-02-19T01:42:17.410@Doorknob, good point. I have adjusted the assignment accordingly. – Michael Stern – 2015-02-19T01:56:53.213
I think @Doorknob 's idea was that you make the specification for the general case (position of friends may vary) and use the specific case as an example (to avoid answers hardcoded to the specific case). Maybe you agree with him, but it's not entirely clear from the current edit. Also, should the path found be exactly
n
units as specified by the user? Or the maximum possible? What was the intention? – Level River St – 2015-02-19T02:05:06.1531Does the path start at any friend? – xnor – 2015-02-19T02:23:04.157
1Can I define the format of the input and output strings? Is an input like
"[0, 5, 7, 13, 16, 17], 62"
and an output"(7, 16, 0, 17, 5, 13)"
ok? – Logic Knight – 2015-02-19T06:15:16.173answering various questions above: (1) you can start at any friend. Given the six friends above counting reflected paths, there are 720 possible paths. (2) assume the locations of the friends and the target distance are provided at runtime. For every length where a solution is possible, at least two solutions are possible (because of reflection). You need to produce only one. If the requested length is impossible, exit gracefully. – Michael Stern – 2015-02-19T11:23:00.967
1@Geobits just sloppiness on my part. Corrected. – Michael Stern – 2015-02-19T16:07:39.087