I am by no means an "expert" at spreadsheets, .... also, I use Libreoffice. I have made some very long formulas for optimum engine lifespan and expanding into "predicted" mileage to find a "predicted" running cost. These are not ideal, but they do work when I limit to check for say 25 years. The way I did it, the formula is setup with 25 instances to check. To change the date range requires adjusting the length of the entire formula.
Because your data sets are of an unpredictable length, I don't think it would be practical to try to cram everything in one cell (I think you will have trouble fitting more than 50 cities into one cell). I would consider making a table that will auto-fill from a pasted set of cities,x,y. Make it say 200x200 (which map has more than 200 cities?). Then you can have an auto-generated list for the output. Can be done in an hour or less (without formatting).
As I said, I'm not an "expert" at sheets. I am also not familiar with other code types. If you know how to use basic, I would do that. Otherwise, perhaps you can try something like this
http://postgis.net/windows_downloads/ ? Disclaimer: I never used it, but it's for real-world mapping. Personally, if I wanted such a thing spending an hour to make a sheet is probably the best use of my time, rather than learning something else from scratch.