12
Goal
Given an string with a train of hashes, calculate its total length and divide by the distance from start to finish.
Simulation
What are we simulating? According to this paper, the ratio of the length of a river to the distance between start and end is approximately Pi! (This may have been disproved empirically, but I could find the data and for this challenge we'll assume it is true).
How are we simulating this?
- Take a string input of whitespace and hashes
- Each hash will have two others adjacent to it
- With the exception of the first and last hash which will have only 1
- Each character lies on a lattice point
(x, y)
x
is the character's index in its line- eg
c
is the 4th character in0123c567
- eg
y
is the character's line number- eg
c
is on the 3rd line:
- eg
0line
1line
2line
3c...
- Sum the distances between adjacent hashes, call it
S
- Take the distance between the first and last hashes, call it
D
- Return
S/D
Specification
- Input
- Flexible, take input in any of the standard ways (eg function parameter,STDIN) and in any standard format (eg String, Binary)
- Output
- Flexible, give output in any of the standard ways (eg return, print)
- White space, trailing and leading white space is acceptable
- Accuracy, please provide at least 4 decimal places of accuracy (ie
3.1416
)
- Scoring
- Shortest code wins!
Test Cases
These are my approximations of the rivers. My approximations might be poor or these my be poor sample of the river population. Also, I did this calculations by hand; I could have miss calculated.
### ####
# # #
# # #
# # #
# # #
# # #
## # # #####
## # #
##
1.6519
#
#
#
#
#
#
#
#
# #
# # #
# #
#
##
#
#
#
#
#
#
#
#
# #
# ##
#
#
#
#
#
#
#
#
#
#
#
1.5498
###
# #
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
###
#
#
#
#
#
#
#
#
#
##
#
#
##
##
##
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
####
#
#
1.5257
TL;DR
These challenges are simulations of algorithms that only require nature and your brain (and maybe some re-usable resources) to approximate Pi. If you really need Pi during the zombie apocalypse, these methods don't waste ammo! There are nine challenges total.
3They're called hashes on their own. "Hashtag" is just the term for an inline tag signified with
#<tag>
– FlipTack – 2016-12-26T15:42:37.4831I assume that the distance should be calculated using the Pythagorean theorem. Is this correct? – Loovjo – 2016-12-26T15:47:33.263
Also, can we take the input as a list of lines? – Loovjo – 2016-12-26T15:48:22.680
@Loovjo ^^ It can be, it is Euclidean geometry so however you want to calculate it is fine. ^ Yes, input is flexible. – NonlinearFruit – 2016-12-26T15:49:31.443
Can the input be a matrix of zeros and ones? – Luis Mendo – 2016-12-26T16:55:42.677
@LuisMendo Yes, that would be fine – NonlinearFruit – 2016-12-26T19:13:53.120
Can we return a rational number? – LegionMammal978 – 2016-12-26T21:25:02.323
Does the paper really say
pi
? The outputs look more similar topi/2
– Luis Mendo – 2016-12-26T22:58:39.173@LuisMendo I believe it does say pi (but I don't have access to it anymore). Several articles about this paper say that the average sinuosity is 3.14. This particular article suggests that, empirically, the average sinuosity of rivers is around 1.94.
– NonlinearFruit – 2016-12-27T00:24:39.1871@NonlinearFruit Thanks. Then it's probably that the ASCII versions are not sinuous enough :) – Luis Mendo – 2016-12-27T00:25:48.740
@LegionMammal978 Accuracy, please provide at least 4 decimal places of accuracy – NonlinearFruit – 2016-12-27T00:36:29.267