9
4
Consider a bridge of length B formed by tiles labeled with the digits of the positive integers concatenated. For example, if B was 41, then it would look like this:
----------------------------------------- 12345678910111213141516171819202122232425
Now imagine a train of length T crossing the bridge. The leftmost point of the train starts at position X (1-indexed). To get a better understanding of the problem, let's make a scheme of the event, with B = 41, T = 10, X = 10. The train is drawn using equal signs (=
) and lines:
__________ |========| |========| ----------------------------------------- 12345678910111213141516171819202122232425
The train can advance, at each step, by sum of the unique tiles it is located on. For example, the tiles the train stands on above are: [1, 0, 1, 1, 1, 2, 1, 3, 1, 4]
, the unique (deduplicated) tiles are: [1, 0, 2, 3, 4]
, and their sum is 10
. Hence, the train can advance by 10
tiles. We should draw it again and repeat the process until the leftmost point of the train has passed the last tile:
__________ |========| |========| ----------------------------------------- 12345678910111213141516171819202122232425 Sum of unique tiles: 1 + 5 + 6 + 7 + 8 + 9 = 36. The train advances by 36 tiles... __________ |========| |========| ----------------------------------------- 12345678910111213141516171819202122232425 The train obviously crossed the bridge completely, so we should stop now.
Since the people inside are bored, they count the tiles the train has advanced each time. In this specific case, 10
and 36
. Summing everything up, the train has moved 46
before it passed the bridge.
Task
Given three positive integers, B (the bridge length), T (the train length) and X (the starting position, 1-indexed), your task is to determine how many tiles the train has moved until it crossed the bridge following the rules above.
- You can assume that:
- B is higher than T.
- X is lower than B.
- T is at least 2.
- The train eventually crosses the bridge.
- All our standard rules apply.
- This is code-golf, so the shortest code in bytes wins!
Test cases
Input ([B, T, X]) -> Output [41, 10, 10] -> 46 [40, 10, 10] -> 46 [30, 4, 16] -> 24 [50, 6, 11] -> 50
Another worked example for the last test case:
The bridge is of length 50, the train 6, and the starting position is 11. ______ |====| |====| -------------------------------------------------- 12345678910111213141516171819202122232425262728293 Unique tiles: [0, 1, 2]. Sum: 3. ______ |====| |====| -------------------------------------------------- 12345678910111213141516171819202122232425262728293 Unique tiles: [1, 2, 3, 4]. Sum: 10. ______ |====| |====| -------------------------------------------------- 12345678910111213141516171819202122232425262728293 Unique tiles: [1, 7, 8, 9]. Sum: 25. ______ |====| |====| -------------------------------------------------- 12345678910111213141516171819202122232425262728293 Unique tiles: [9, 3]. Sum: 12. ______ |====| |====| -------------------------------------------------- 12345678910111213141516171819202122232425262728293 Train exists the bridge. Total sum: 3 + 10 + 25 + 12 = 50.
6Can we assume the train does cross the bridge eventually? For inputs like
(200, 2, 169)
, the train gets stuck on the00
in…9899100101102…
. – Lynn – 2017-10-09T22:47:56.310@Lynn A little late, yes, you can. – Mr. Xcoder – 2017-10-10T04:16:15.297