Determining vertical slices
Jelly, 2 bytes
PS
Try it here!
If I encode an image like so:
0000111111111100000
0000000111111111000
0000000001111100000
0000000011111000000
0001111111111111100
0000001111110000000
0000000111111110000
0000111111111100000
Into a nested array like this:
[[0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0],...]
Then P
takes the element-wise product of all of the row vectors, and S
sums all of the ones in the result, yielding the length of the vertical slice. (This works only because there’s guaranteed to be only one contiguous slice.) In our case, the answer is 3
.
APL, 4 bytes
+/×⌿
Try it here.
This is my first APL answer!
Thanks to @jimmy23013 and @NBZ for saving bytes!
Perl, 21 22 bytes
Fixed version
Includes +2 for -lp
(-l
can be omitted and would still be a valid solution, but it's ugly without the final newline)
Give sequences of 1's and 0's on 0 or more lines on STDIN. You can add spaces or commas or whatever between the digits if you want as long as the usage is.consistent on all lines.
$a|=~$_}{$_=$a=~y;\xce;
This works as shown, but replace \xce
by the literal byte value to get the claimed score
If there are multiple vertical sections this returns the sum of all section widths. If you want the width of a vertical section use
$a|=~$_}{$a=~/\xce+/;$_="@+"-"@-"
Old version
I originally misunderstood the challenge and implemented a program that gives true or false based on if a vertical line exists at all. Code and explanation here are for this old version
$a|=~$_}{$_|=~$a=~1
If only I could add 1=~ at the left for almost perfect symmetry... I suppose the closest would be
1=>$a|=~$_}{$_|=~$a=~1
Explanation
$a|=~$_ The bitwise operators in perl (&, |, ^, ~) also work on strings by
working on the sequence of byte values. The digits "0" and "1" happen
to have the same ASCII value differing only in the last bit which is
0 for "0" and 1 for "1". So I would really like to do an "&" here.
Unfortunately "&" of two different length strings shortens the result
to the shortest of the strings and my accumulator starts as an empty
string. The "|" of two strings however extends to the longest string.
So instead I will apply De Morgan's law and use "|" on the
complemented byte string
}{ Standard perl golf trick. "-p code" transforms to (simplified)
"while (<>) { code; print }". So if code is "code1 } { code2" this
becomes "while (<>) { code1 } {code2; print }". So you can use code1
for the loop operation, use code2 for the final calculation and get a
free print by assigning to $_
$_|=~$a=~1 I would like to match the accumulator with the bit complement of "1",
but $a=~~1 doesn't work because the 1 is not a string but a number.
$a=~~"1" would work but is too long. Next up is complementing $a back
and matching with 1, so $_=~$a=~1. That also doesn't work since the
first =~ will be interpreted as a string match insteads of equals
followed by complement. Easily solved by writing it as $_= ~a=~1. But
if I am going to give up a byte I can at least have some fun with it.
Using $_|= also makes the parse work and has the advantage that the
failure case will give 0 instead of an empty string, which looks
nicer. It also makes the code look very symmetric. I can also bring
out the symmetry more by putting 1=> in front (which evaluates 1
before the assignment and then immediately discards it)