A train crosses a labeled bridge

Python 2, 110 105 104 103 100 97 96 bytes

  • Saved five bytes thanks to Mr. Xcoder; removed unnecessary assignment, moved negation into available whitespace.
  • Saved a byte thanks to Mr. Xcoder; golfed [~-x:x+~-t] to [~-x:][:t].
  • Saved a byte; golfed ...range(1,-~b)))[:b] to ...range(b)))[1:-~b].
  • Saved three bytes; golfed [1:-~b][~-x:] to [:-~b][x:].
  • Saved three bytes; golfed [:-~b][x:] to [x:-~b].
  • Saved a byte thanks to Lynn; golfing the while loop to an exec statement.

Try it online!

Haskell, 106 102 bytes

import Data.List
(b#t)x|x>b=0|y<-sum[read[c]|c<-nub$take t$drop(x-1)$take b$show=<<[1..]]=y+(b#t)(x+y)

Try it online!

   |x>b=0                 -- if the train has left the bridge, return 0
   |y<-sum[   ]           -- else let y be the sum of
      read[c]|c<-         -- the digits c where c comes from
        nub               -- the uniquified list of
            show=<<[1..]] -- starting with the digits of all integers concatenated
          take b          -- taking b digits (length of bridge)
         drop(x-1)        -- dropping the part before the train
        take t            -- take the digits under the train
     =y+(b#t)(x+y)        -- return y plus a recursive call with the train advanced

R, 123 bytes


Just implements the described algorithm.

R is quite terrible at strings.

 s <- substring                         # alias
 b <- s(paste(1:B,collapse=''),1,B)     # bridge characters
 while(X<B){                            # until we crossed the bridge
  K <- X+1:T-1                          # indices of the characters
  S <- s(b,K,K)                         # the characters from b
  S <- sum(unique(strtoi(S)))           # sum
  F <- F + S                            # F defaults to 0 at the beginning
  X <- X + S                            # advance the train
 F                                      # number of steps, returned

Try it online!