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.
#<tag>
\$\endgroup\$