Given an ASCII representation of a piece of string, determine its length.
Input
An multi-line ASCII rendering of a piece of string, which runs from top to bottom, with one 'node' (corner) on each line of input. The delimiter may be assumed to be CR
, LF
, CRLF
, or LFCR
. Input may be accepted from STDIN, Command Line Argument, Function Parameter, or whatever daft method your language of choice necessitates if none of the prior are applicable.
#
#
#
####
##
##
#
##
#
Each line in the context of the previous tells you the position of one of the nodes on the string. The first line always has exactly one hash #
, so the position is trivially found. Each following line has a hash '#' on the node itself, and on all calls which must be included to make the string 'complete' (attached to itself). All positions will be between 0 and 9 inclusive, but the difference is what realy matters.
For example, the string defined by the sequence 1, 3, 6, 5, 1, 3, 4
has the following nodes:
#
#
#
#
#
#
#
#
#
Which we must join up on a line by line basis starting from the top (nodes marked with X
for clarity, they will be hashes in actual imputs). This means, the first line has no hashes, and each following line has exactly as many hashes as are rquired to 'connect' the previous node to the current with hashes. This can be thought of as putting a hash in every space between the two nodes (on the lower line) and then removing any that appear on the line above. Example:
X
#X
##X
X
X###
X
X#
##X
X
There will be no fewer than 2 lines, and no more than 100. The input will be padded with spaces to form a rectangle of width 10 (excluding the line delimiters). You may require a trailing new-line be present or absent, but declare any such requirements in your answer.
Computing the Arc Length
You must output the arc length of this string. You compute the arc length by taking the distances between each consequtive nodes. The distance between one node and the next is sqrt(d^2 + 1)
where d
is the difference in position (that is, the current position minus the previous). For example, the sequence 4, 8, 2, 3, 4, 4
has differences 4, -6, 1, 1, 0
, which gives distances (rounded to 2 dp) 4.12, 6.08, 1.41, 1.41, 1
by squaring, adding one, and square-rooting. The arc length is then the sum of these distances, 14.02
.
You must reduce the result of the square-root to 2 decimal places before summing (this is to allow sqrt tables and make answers consistent/comparable across systems; see below).
Using the input example at the top`
# 2 -1 1.41
# 1 +0 1.00
# 1 +4 4.12
#### 5 -5 5.10
## 0 +3 3.16
## 3 -2 2.24
# 1 +2 2.24
## 3 -1 1.41
# 2
20.68 (total)
Output
You must output the arc length as exactly one number, or a decimal string represenation with 2 decimal places of precision. Numerical output must be rounded, but I'm not going to quibble about minute floating point inaccuracies.
SQRT Table
This is a table of the rounded squareroots you must conform to. You can either use this table itself, or verify that your system conforms to the results.
1 1.00
2 1.41
5 2.24
10 3.16
17 4.12
26 5.10
37 6.08
50 7.07
65 8.06
82 9.06
Powershell to produce this table:
0..9 | % {"" + ($_ * $_ + 1) + " " + [System.Math]::Sqrt($_ * $_ +1).ToString("0.00")}
Test Cases
Input:
#
#
#
####
##
##
#
##
#
Output: 20.68
Input:
#
###
####
#
###
#
###
####
#
#
##
########
#
###
######
Output: 49.24
Input:
#
##
######
#
####
#
######
#
########
#
####
####
Output: 44.34
Test Code
A larger test case and a C# solver and example generator can be found as a Gist.
Victory Criterion
This is code-golf, the shortest code will win.
d
is the difference in position" - Can you elaborate on that? Maybe it's just too early but, reading through your examples, I can't see how you're arriving at thed
for each value in the sequences. \$\endgroup\$