Introduction
I want to build a ladder. For this, I have scavenged from the junkyard two long boards with holes in them, and I want to place the steps into these holes. However, the holes are not evenly placed, so the steps will be a little wonky, and I find it hard to estimate the amount of rod I need for them. Your job is to do the calculations for me.
Input
Your input is two bit vectors, given as arrays of integers, which represent the two boards.
A 0
represents a segment of one aud (arbitrary unit of distance) without a hole, and a 1
represents a segment of one aud with a single hole.
The arrays may be of different lengths and contain a different number of 1
s, but they will not be empty.
I will construct my ladder as follows.
First, I place the two boards exactly one aud apart, and align their left ends.
For each index i
, I measure the distance between the i
th hole of the first board with the i
th hole of the second board, cut a piece of rod, and attach it between the two holes.
I stop once I run out of holes in one of the boards.
Output
Your output is the total amount of rod I'll need for the steps, measured in auds. The output should be correct to at least six significant digits.
Example
Consider the inputs [0,1,1,0,1,1,1,1,0,0]
and [1,0,0,1,1,1,0,0,1]
.
The resulting ladder looks like this:
The total length of the rod in this ladder is 7.06449510224598
auds.
Rules
You can write either a function or a full program. The lowest byte count wins, and standard loopholes are disallowed.
Test Cases
[0] [0] -> 0.0
[0] [1,0] -> 0.0
[1,0,0] [1,1,1,1,1] -> 1.0
[0,1,0,1] [1,0,0,1] -> 2.414213562373095
[0,1,1,0,1,1,1,1,0,0] [1,0,0,1,1,1,0,0,1] -> 7.06449510224598
[1,1,1,1,1] [0,0,1,1,0,1,0,0,1] -> 12.733433128760744
[0,0,0,1,0,1,1,0,0,0,1,1,1,0,0,1,0,1,1,0,0,0,1,0] [0,0,1,1,0,1,1,1,0,0,0,0,0,1,1,0,1,1,0,0,0,1] -> 20.38177416534678