火车穿过一座标有商标的桥


9

考虑由标有级联的正整数的数字的图块形成的长度为B的桥。例如,如果B为41,则它将如下所示:

-----------------------------------------
12345678910111213141516171819202122232425

现在想象一列长度为T的火车过桥。火车的最左端开始于位置X(1索引)。为了更好地理解问题,让我们为事件制定一个方案,其中B = 41,T = 10,X = 10。使用等号(=)和线绘制火车:

         __________
         | ======== |
         | ======== |
-----------------------------------------
12345678910111213141516171819202122232425

火车在每一步中都可以通过其所在的唯一图块的总和来前进。例如,火车在上面站立的磁贴为:[1, 0, 1, 1, 1, 2, 1, 3, 1, 4],唯一(重复数据消除)磁贴为:[1, 0, 2, 3, 4],其总和为10。因此,火车可以前进10。我们应该再次绘制它并重复该过程,直到火车的最左边的点通过最后一个图块:

                   __________
                   | ======== |
                   | ======== |
-----------------------------------------
12345678910111213141516171819202122232425

唯一区块的总和:1 + 5 + 6 + 7 + 8 + 9 =36。火车前进了36区块...

                                                       __________
                                                       | ======== |
                                                       | ======== |
-----------------------------------------
12345678910111213141516171819202122232425

火车显然完全过了桥,所以我们现在应该停下来。

由于里面的人很无聊,他们每次都计算火车前进的瓷砖。在这种情况下,1036。总结一下一切,火车46在通过桥之前就已经移动了。


任务

给定三个正整数,B(桥长),T(火车长)和X(起始位置,1索引),您的任务是确定火车在遵循规则穿越桥之前已移动了多少块瓷砖以上。

  • 您可以假设:
    • 高于Ť
    • X是低于
    • T至少为2
    • 火车最终过桥了。
  • 我们所有的标准规则均适用。
  • 这是 ,因此以字节为单位的最短代码胜出!

测试用例

输入([B,T,X])->输出

[41,10,10]-> 46
[40,10,10]-> 46
[30,4,16]-> 24
[50,6,11]-> 50

最后一个测试用例的另一个可行示例:

桥的长度为50,火车的长度为6,起始位置为11。

          ______
          | ==== |
          | ==== |
--------------------------------------------------
12345678910111213141516171819202122232425262728293

唯一图块:[0、1、2]。总数:3。

             ______
             | ==== |
             | ==== |
--------------------------------------------------
12345678910111213141516171819202122232425262728293

唯一图块:[1、2、3、4]。总计:10。

                       ______
                       | ==== |
                       | ==== |
--------------------------------------------------
12345678910111213141516171819202122232425262728293

唯一图块:[1、7、8、9]。总计:25。

                                                ______
                                                | ==== |
                                                | ==== |
--------------------------------------------------
12345678910111213141516171819202122232425262728293

唯一图块:[9,3]。总计:12。
                                                            ______
                                                            | ==== |
                                                            | ==== |
--------------------------------------------------
12345678910111213141516171819202122232425262728293

火车存在桥梁。总和:3 + 10 + 25 + 12 = 50。

6
我们可以假设火车没有最终过桥?对于输入,比如(200, 2, 169),火车卡在00…9899100101102…
林恩

@Lynn有点晚了,是的,可以。
Xcoder先生17年

Answers:


3

外壳,20字节

ṁ←U¡S↓←moΣuX_⁰↓Θ↑ṁdN

在线尝试!

依次接受三个参数TBX

说明

ṁ←U¡S↓←moΣuX_⁰↓Θ↑ṁdN
                 ṁdN    Build the list of digits of natural numbers
              ↓Θ↑       Take the first B digits, add a 0 in front
                        then drop the first X digits
           X_⁰          Get all sublists of length T
       moΣu             Map the sum of unique values of each sublist

   ¡S↓←                 Repeatedly drop as many elements from the start of the list as the
                        first element of the list says;
                        keep all partial results in an infinite list.

  U                     Take elements until the first repeated one
                        (drops tail of infinite empty lists)

ṁ←                      Sum the first elements of each remaining sublist

6

Python 2中110 105 104 103 100 97 96个字节

  • 多亏了Xcoder先生,节省了五个字节;删除了不必要的分配,将否定移到了可用的空格中。
  • 多亏了Xcoder先生,节省了一个字节;golfed [~-x:x+~-t][~-x:][:t]
  • 保存一个字节;golfed ...range(1,-~b)))[:b]...range(b)))[1:-~b]
  • 保存了三个字节;golfed [1:-~b][~-x:][:-~b][x:]
  • 保存了三个字节;golfed [:-~b][x:][x:-~b]
  • 感谢Lynn节省了一个字节;将while循环打成一条exec语句。
b,t,x=input();S=x;exec"x+=sum(set(map(int,''.join(map(str,range(b)))[x:-~b][:t])));"*b;print-S+x

在线尝试!


备用105字节长的解决方案。
乔纳森·弗雷希

104个字节[~-x:x+~-t]可以由[x-1:][:t]
Xcoder先生于

exec"x+=sum(set(map(int,''.join(map(str,range(b)))[x:-~b][:t])));"*b可以工作96分钟。(火车b走出桥的步伐绝不会超过一步,x+=0一旦离开桥头,整个操作将一遍又一遍。)
Lynn

4

Haskell,106102字节

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)

在线尝试!

(b#t)x
   |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

3

R,123字节

function(B,T,X){s=substring
while(X<B){F=F+(S=sum(unique(strtoi(s(s(paste(1:B,collapse=''),1,B),K<-X+1:T-1,K)))))
X=X+S}
F}

仅实现所描述的算法。

R在字符串上非常糟糕。

function(B,T,X){
 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
}

在线尝试!


2

果冻 22  21 字节

ḣ⁵QS
RDẎḣ⁸ṫṫÇ‘$$ÐĿÇ€S

一个带有三个参数的完整程序-顺序为BXT-打印结果。

在线尝试!

怎么样?

ḣ⁵QS - Link 1, calculate next jump: list of digits, bridge under and beyond train's left
 ⁵   - program's fifth command line argument (3rd input) = T (train length)
ḣ    - head to index (get the digits of the tiles under the train)
  Q  - de-duplicate
   S - sum

RDẎḣ⁸ṫṫÇ‘$$ÐĿÇ€S - Main link: number, B (bridge length); number, X (starting position)
R                - range(B) = [1,2,3,...,B-1,B]
 D               - to decimal list (vectorises) = [[1],[2],[3],...,[digits of B-1],[digits of B]]
  Ẏ              - tighten (flatten by one) = [1,2,3,...,digits of B-1,digits of B]
    ⁸            - chain's left argument, B
   ḣ             - head to index (truncate to only the bridge's digits)
     ṫ           - tail from index (implicit X) (truncate from the train's left)
           ÐĿ    - loop, collecting results, until no more change occurs:
          $      -   last two links as a monad:
         $       -     last two links as a monad:
       Ç         -       call last link (1) as a monad (get next jump)
        ‘        -       increment
      ṫ          -     tail from that index (remove the track to the left after train jumps)
             Ç€  - call last link (1) as a monad for €ach (gets the jump sizes taken again)
               S - sum
                 - implicit print

1

JavaScript(ES6),117个字节

f=(B,T,X,g=b=>b?g(b-1)+b:'',o=0)=>X<B?[...g(B).substr(X-1,T)].map((e,i,a)=>o+=i+X>B|a[-e]?0:a[-e]=+e)&&o+f(B,T,X+o):0

一对递归函数:

  1. f() 总结火车的动作。
  2. g() 创建数字字符串。

少打高尔夫球:

f=
(B,T,X,
 g=b=>b?g(b-1)+b:'',                       //creates the string of numbers
 o=0                                       //sum of tiles the train sits on
)=>
  X<B?                                     //if we're not past the bridge:
      [...g(B).substr(X - 1,T)].map(       //  grab the tiles we're sitting on
        (e,i,a)=>o += i + X > B |          //  if we've passed the bridge,
                      a[-e] ? 0 :          //  ... or we've seen this tile before, add 0 to o
                              a[-e] = +e   //  else store this tile and add its value to o
      ) &&
      o + f(B,T,X+o) :                     //recurse
  0


0

PHP> = 7.1,153个字节

<?$s=substr;[,$p,$q,$r]=$argv;while($i<$p)$a.=++$i;$a=$s($a,0,$p);;while($r<$p){$x+=$n=array_sum(array_unique(str_split($s($a,$r-1,$q))));$r+=$n;}echo$x;

在线尝试!

为了使其与较低版本的PHP兼容,请更改[,$p,$q,$r]=list(,$p,$q,$r)=(+4字节)。

<?
[,$bridgelen,$trainlen,$position] = $argv;                  // grab input
while($i<$bridgelen)                                        // until the bridge is long enough...
  $bridgestr .= ++$i;                                       // add to the bridge
$bridgestr = substr($bridgestr,0,$bridgelen);               // cut the bridge down to size (if it splits mid-number)
while($position<$bridgelen){                                // while we are still on the bridge...
  $currtiles =                                              // set current tiles crossed to the...
    array_sum(                                              // sum of tiles...
      array_unique(                                         // uniquely...
        str_split(substr($bridgestr,$position-1,$trainlen)) // under the train
      )
    )
  ;
  $totaltiles += $currtiles;                                // increment total tiles crossed
  $position += $currtiles;                                  // set new position
}
echo $totaltiles;                                           // echo total tiles crossed
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.