#!/usr/bin/env ysh source --builtin list.ysh # sum() const INPUT = $1 const DIGITS = {one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9} const Digit = / digit | 'one' | 'two' | 'three' | 'four' | 'five' | 'six' | 'seven' | 'eight' | 'nine' / const DigitRev = / digit | 'eno' | 'owt' | 'eerht' | 'ruof' | 'evif' | 'xis' | 'neves' | 'thgie' | 'enin' / const SingleDigit = / %start digit %end / func toDigit(number) { ### Convert number to digit if name and return digit. if (number !~ / SingleDigit /) { return (DIGITS[number]) } else { return (number) } } proc main { var components = [] while read -r line { var first = toDigit(split($(grep -E -o ${Digit} <<< ${line}))[0]) var last = toDigit(split($(grep -E -o ${Digit} <<< ${line}))[-1]) call components->append(str(first) ++ str(last)) } < ${INPUT} echo $[sum(components)] } main