Pyth, 12 11 10 bytes
ssM:z"\D"3
z autoinitialized to input()
: "\D"3 split on non-digits
sM convert all elements of resulting array to ints
s sum
Fortunately, s
(convert to int) returns 0
when applied to the empty string, so I don't have to worry about the fact that split("a1b", "\D+")
returns ["", "1", ""]
. Similarly, split("a", "\D+")
returns ["", ""]
.
This even allows me to split on every non-digit individually, since 1 + 0 + 0 + 0 + 0 + 2
is the same thing as 1 + 2
.
Thanks to Thomas KwaThomas Kwa for a byte!