#JavaScript (ES6), 178
JavaScript (ES6), 178
#JavaScript (ES6), 178
JavaScript (ES6), 178
F=
r=>r.replace(/#/g,(c,i)=>([d=r.search`\n`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
Yellow=` ### ####
# # #
# # #
# # #
# # #
# # #
## # # #####
## # #
## `
Nile=` #
#
#
#
#
#
#
#
# #
# # #
# #
#
##
#
#
#
#
#
#
#
#
# #
# ##
#
#
#
#
#
#
#
#
#
#
# `
Missi=` ###
# #
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
###
#
#
#
#
#
#
#
#
#
##
#
#
##
##
##
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
####
#
# `
console.log('Yellow River',F(Yellow))
console.log('Nile River',F(Nile))
console.log('Mississippi River',F(Missi))
F=
r=>r.replace(/#/g,(c,i)=>([d=r.search`\n`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
Yellow=` ### ####
# # #
# # #
# # #
# # #
# # #
## # # #####
## # #
## `
Nile=` #
#
#
#
#
#
#
#
# #
# # #
# #
#
##
#
#
#
#
#
#
#
#
# #
# ##
#
#
#
#
#
#
#
#
#
#
# `
console.log('Yellow River',F(Yellow))
console.log('Nile River',F(Nile))
F=
r=>r.replace(/#/g,(c,i)=>([d=r.search`\n`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
Yellow=` ### ####
# # #
# # #
# # #
# # #
# # #
## # # #####
## # #
## `
Nile=` #
#
#
#
#
#
#
#
# #
# # #
# #
#
##
#
#
#
#
#
#
#
#
# #
# ##
#
#
#
#
#
#
#
#
#
#
# `
Missi=` ###
# #
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
###
#
#
#
#
#
#
#
#
#
##
#
#
##
##
##
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
####
#
# `
console.log('Yellow River',F(Yellow))
console.log('Nile River',F(Nile))
console.log('Mississippi River',F(Missi))
r=>r.replace(/#/g,(c,i)=>([d=r.search`
`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
r=>r.replace(/#/g,(c,i)=>([d=r.search`
`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
r=>(
r.replace(/#/g, // exec the following for each '#' in the string
(c,i) => c: current char (=#), i: current position
( // check in 8 directions
// note: d starts as the offset to next row, prev x position
// and is incremented up to offset to next row, succ x position
// note 2: there are 2 diagonal offsets, then 2 orthogonal offsets
// then other 2 diagonal, then 2 more orthogonal
[d=r.search`\n`,-d, ++d,-d, ++d,-d, 1,-1].map( // for each offset
(d,j) => // d: current offset, j: array position (0 to 7)
r[i+d] == c && // if find a '#' at current offset ...
(
--n, // decrement n to check for 2 neighbors or just 1
s += j & 2 ? 1 : Math.SQRT2 // add the right distance to s
),
n = 1), // n starts at 1, will be -1 if 2 neighbors found, else 0
// if n==0 we have found a start or end position, record it in v and w
n || (v=w, w=i)
),
w=s=0), // init s and w, no need to init v
// at the end
// d is the length of a line + 1
// s is twice the total length of the river
// v and w can be used to find the x,y position of start and end
s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
)
r=>(
r.replace(/#/g, // exec the following for each '#' in the string
(c,i) => // c: current char (=#), i: current position
( // check in 8 directions
// note: d starts as the offset to next row, prev x position
// and is incremented up to offset to next row, succ x position
// note 2: there are 2 diagonal offsets, then 2 orthogonal offsets
// then other 2 diagonal, then 2 more orthogonal
[d=r.search`\n`,-d, ++d,-d, ++d,-d, 1,-1].map( // for each offset
(d,j) => // d: current offset, j: array position (0 to 7)
r[i+d] == c && // if find a '#' at current offset ...
(
--n, // decrement n to check for 2 neighbors or just 1
s += j & 2 ? 1 : Math.SQRT2 // add the right distance to s
),
n = 1), // n starts at 1, will be -1 if 2 neighbors found, else 0
// if n==0 we have found a start or end position, record it in v and w
n || (v=w, w=i)
),
w=s=0), // init s and w, no need to init v
// at the end
// d is the length of a line + 1
// s is twice the total length of the river
// v and w can be used to find the x,y position of start and end
s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
)
Test
F=
r=>r.replace(/#/g,(c,i)=>([d=r.search`\n`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
Yellow=` ### ####
# # #
# # #
# # #
# # #
# # #
## # # #####
## # #
## `
Nile=` #
#
#
#
#
#
#
#
# #
# # #
# #
#
##
#
#
#
#
#
#
#
#
# #
# ##
#
#
#
#
#
#
#
#
#
#
# `
console.log('Yellow River',F(Yellow))
console.log('Nile River',F(Nile))r=>r.replace(/#/g,(c,i)=>([d=r.search`
`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
r=>(
r.replace(/#/g, // exec the following for each '#' in the string
(c,i) => c: current char (=#), i: current position
( // check in 8 directions
// note: d starts as the offset to next row, prev x position
// and is incremented up to offset to next row, succ x position
// note 2: there are 2 diagonal offsets, then 2 orthogonal offsets
// then other 2 diagonal, then 2 more orthogonal
[d=r.search`\n`,-d, ++d,-d, ++d,-d, 1,-1].map( // for each offset
(d,j) => // d: current offset, j: array position (0 to 7)
r[i+d] == c && // if find a '#' at current offset ...
(
--n, // decrement n to check for 2 neighbors or just 1
s += j & 2 ? 1 : Math.SQRT2 // add the right distance to s
),
n = 1), // n starts at 1, will be -1 if 2 neighbors found, else 0
// if n==0 we have found a start or end position, record it in v and w
n || (v=w, w=i)
),
w=s=0), // init s and w, no need to init v
// at the end
// d is the length of a line + 1
// s is twice the total length of the river
// v and w can be used to find the x,y position of start and end
s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
)
r=>r.replace(/#/g,(c,i)=>([d=r.search`
`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
r=>(
r.replace(/#/g, // exec the following for each '#' in the string
(c,i) => // c: current char (=#), i: current position
( // check in 8 directions
// note: d starts as the offset to next row, prev x position
// and is incremented up to offset to next row, succ x position
// note 2: there are 2 diagonal offsets, then 2 orthogonal offsets
// then other 2 diagonal, then 2 more orthogonal
[d=r.search`\n`,-d, ++d,-d, ++d,-d, 1,-1].map( // for each offset
(d,j) => // d: current offset, j: array position (0 to 7)
r[i+d] == c && // if find a '#' at current offset ...
(
--n, // decrement n to check for 2 neighbors or just 1
s += j & 2 ? 1 : Math.SQRT2 // add the right distance to s
),
n = 1), // n starts at 1, will be -1 if 2 neighbors found, else 0
// if n==0 we have found a start or end position, record it in v and w
n || (v=w, w=i)
),
w=s=0), // init s and w, no need to init v
// at the end
// d is the length of a line + 1
// s is twice the total length of the river
// v and w can be used to find the x,y position of start and end
s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
)
Test
F=
r=>r.replace(/#/g,(c,i)=>([d=r.search`\n`,-d,++d,-d,++d,-d,1,-1].map((d,j)=>r[i+d]==c&&(--n,s+=j&2?1:Math.SQRT2),n=1),n||(v=w,w=i)),w=s=0)&&s/2/Math.hypot(v%--d-w%d,~(v/d)-~(w/d))
Yellow=` ### ####
# # #
# # #
# # #
# # #
# # #
## # # #####
## # #
## `
Nile=` #
#
#
#
#
#
#
#
# #
# # #
# #
#
##
#
#
#
#
#
#
#
#
# #
# ##
#
#
#
#
#
#
#
#
#
#
# `
console.log('Yellow River',F(Yellow))
console.log('Nile River',F(Nile))