吉米会掉下平台吗?


29

背景故事

认识我的朋友吉米:

/o\

吉米(Jimmy)是一个小人物,喜欢站在平台上。这是吉米安全地站在平台上的地方:

         /o\
  -------------

现在,吉米已经有了很好的平衡感,因此他可以放下一只脚,安全地站起来,就像这样:

   /o\
    -------------------

尽管如果他站着两个或更多身体部位离开平台,他将跌倒。这两个都是吉米将倒下的例子:

/o\                                       /o\
  ----------        ----------------------   

挑战

您面临的挑战是编写一个程序,在给出吉米平台和位置的字符串的情况下,确定吉米是否可以站在平台上而不脱落。

  • 输入:两行显示Jimmy的位置和他下面平台的位置。这可以来自两个单独的输入,单个输入或某种数组。

    1. 您可以通过任何合理的形式接受输入,包括功能和标准输入。如果您的语言不支持其他输入法,则仅求助于硬编码。
  • 输出:布尔值true和false,或者整数1或0分别表示true / false。

    1. 布尔值取决于Jimmy是否可以留在平台上-如果Jimmy可以留在平台上,则为true;如果他会跌落,则为false。
  • 平台大小是任意的,可以随意更改。您的程序应说明这一点。

    1. 平台的长度不能为零,并且平台必须完整(平台上没有孔)。

    2. 请记住,吉米的两个身体部位悬在平台上时会掉下。身体部位是他身体的一个ASCII字符。

    3. 在平台末尾不需要尾随空格,但是您的程序应考虑两种情况,即平台后面有空格而没有空格。

  • 注意标准漏洞禁止。

测试用例

         /o\               ✔️ TRUE
  -------------

/o\                        ✔️ TRUE
 ----------

                    /o\    ❌ FALSE
  ------------------

               /o\         ❌ FALSE
  -------

    /o\                    ❌ FALSE
     -

计分

这是,因此最低字节数为准。

排行榜

您可以通过展开下面的小部件/片段来查看此帖子的页首横幅。为了使您的帖子包含在排名中,您需要一个# header text带有以下信息的标题():

  • 语言名称(以逗号,或破折号结尾-),后跟...。

  • 字节数,作为出现在标题中的最后一个数字。

例如,JavaScript (ES6), 72 bytes有效但Fortran, 143 bytes (8-bit)无效,因为字节数不是标头中的最后一个数字(您的答案将被识别为8个字节-请勿利用此字节)。

<!-- Run the snippet to see the leaderboard. Report any bugs to @xMikee1 on Github -->    <iframe src="https://xmikee1.github.io/ppcg-leaderboard/?id=187586" width="100%" height="100%" style="border:none;">Oops, your browser is too old to view this content! Please upgrade to a newer version of your browser that supports HTML5.</iframe><style>html,body{margin:0;padding:0;height:100%;overflow:hidden}</style>


我们可以在吉米之后假设尾随空格吗?另外,如果您允许使用字符数组,则在某些语言中,这些字符必须用空格填充。
尼克·肯尼迪

@NickKennedy您需要考虑尾随空格或无尾随空格。我没有对此设置严格的规则。
connectyourcharger

12
标题为“将吉米从平台上掉下来 ”,您需要输出“将吉米平台上停留 ”。这是预期的行为吗?
tsh

6
您可以重新格式化测试用例,使其更易于复制和粘贴吗?
毛茸茸的

2
是否允许交换真假值?(即吉米跌倒时输出为真,而吉米跌倒时为假?)
Xcoder先生

Answers:


20

果冻,6个字节

n⁶Sċ2Ẓ

在线尝试!

说明:

n⁶Sċ2Ẓ  args: z (e.g. [['/', 'o', '\\'], [' ', '-']] => 0)
        implicit return value: z ([['/', 'o', '\\'], [' ', '-']])
n⁶      dyad-nilad pair ([[1, 1, 1], [0, 1]])
 ⁶       4th command-line argument or space [4th CLA assumed absent] (' ')
n        vectorized inequality ([[1, 1, 1], [0, 1]])
  S     reduction by addition with base case 0 ([1, 2, 1])
   ċ2   dyad-nilad pair (1)
    2    literal (2)
   ċ     number of occurrences of right in left (1)
     Ẓ  primality (0)

26
有时候,我想知道人类已经进步了多大,能以6个字节来检查某人是否会从平台掉到他们的厄运。
IMustBeSomeone

4
@IMustBeSomeone,如果我要摆脱厄运的平台,我希望有人能尽快告诉我!
毛茸茸的

18
@Shaggy我总是觉得当人们在介绍中遇到挑战时很有趣:“因为我们不想花太多时间在X上,所以代码必须尽可能短。”即使代码和性能很高在大多数情况下是相反的。如果我们可以保存一个从到关注性能的 O n n!; pO(log(n))O(nn)
Kevin Cruijssen

1
我不认识果冻,但是为什么要S对列而不是行求和呢?:S如果我正确看了Wiki,代码就会执行:n⁶检查每个字符是否不等于空格(即['/o\\', ' -'] -> [[1,1,1],[0,1]]);S求和列表(但是出于某种原因,它求和而不是行.. so [[1,1,1],[0,1]] -> [1,2,1]);ċ2数2s; 在隐式输出结果之后,检查这是素数(2还是3)。但我希望[[1,1,1],[0,1]]总结为[3,1]..
凯文·克鲁伊森

1
@KevinCruijssen S等价于0;+/Ɗ,即,通过与初始值相加(向量化)来减少0§做了您期望S做的事。
暴民埃里克

19

JavaScript(ES6),38个字节

将输入作为(a)(b)。返回01

a=>b=>b[a.search`o`]=='-'&/--/.test(b)

在线尝试!

怎么样?

我们"o"在第一个琴弦中寻找吉米身体中部的位置,并测试第二个琴弦在同一位置是否有破折号。

b[a.search`o`] == '-'

在这种情况下,吉米唯一不安全的情况是使用单破折号平台:

/o\
 -

因此,我们还要确保平台的宽度至少为2

/--/.test(b)

JavaScript(ES6),36个字节

替代版本,如果我们假设Jimmy下面始终有破折号或空格(即输入为矩形)。

a=>b=>b[a.search`o`]!=0&/--/.test(b)

在线尝试!

利用以下事实:对空格的数字强制为0,对于破折号的强制为NaN


哇。您能解释一下它是如何工作的吗?
connectyourcharger

@connectyourcharger我添加了一个解释。
Arnauld

3
天才!JS的答案通常并不短。
connectyourcharger


@Oliver将失败"/o\\\n__"
tsh

10

Excel,67 45 44字节

=(MID(A2,FIND("o",A1),1)="-")*(TRIM(A2)>"-")

将Jimmy放进位于A1的平台上A2

检查2个条件:

  • o平台上有吉米的躯干()吗?
  • 平台不只是-

1
编辑历史记录中不会显示前五分钟内的@Keeta编辑。
基金莫妮卡的诉讼

从我所做的有限测试中,我认为您可以更改<>>
Taylor Scott

9

Python 3, 3,88 43字节

输入以包含两个字符串的列表的形式给出:第一个字符串是第一行;第二个字符串是第一行。第二个字符串是第二行。

lambda a:sum(1-(" "in i)for i in zip(*a))>1

在线尝试!

另一个版本,绑定了43个字节(我无法使其短于43个字节):

lambda a,b:b[a.find("/"):][:3].count("-")>1

在线尝试!

由于Jo King的提示,减少了42个字节。

旧版:

lambda s:sum((s.split("\n")[1]+" "*len(s))[i]=="-"and s[i]!=" "for i in range(len(s)))>1

-2个字节,感谢Sriotchilism O'Zaic。

这是通过获取两个单独的输入,然后将相应的字母配对来实现的。它计算两个字符都不为空格的对的数目,如果该数目大于1,则返回True。


8

Perl 6、18个字节

{?/''B|Bq/}o&[~^]

在线尝试!

接受两个参数并返回一个布尔值,该布尔值表示Jimmy是否将保留在平台上。通过将两行异或并检查Jimmy的任何一部分是否仍在平台上来工作。

说明:

             &[~^]   # String XOR operator
{          }o        # Combined with the anonymous function
 ?/       /          # That checks for the regex match
   ''B              # Unprintable, B, which is "/o" ~^ "--"
       |Bq           # Or B, q, which is "o\" ~^ "--"


6

Haskell,34个字节

a#b=[1|(p,'-')<-zip a b,p>' ']>[1]

在线尝试!

我将以下技术与 另一种haskell答案

Haskell,45个字节

x#'-'|x/=' '=1
x#y=0
(((>1).sum).).zipWith(#)

在线尝试!

这将计算平台顶部的身体部位(非空格字符)的数量,然后检查其是否大于1。我们在平台上计算身体部位而不是身体部位的原因是zipWith会砍掉顶行是底部的长度,因此可以切掉吉米的身体部位。这避免了我们不得不做一些事情cycle" "来填补名单。


1
您不能通过转换为infix减少2个字节吗?
科尔

1
@cole是的,当您发表评论时,我只是在进行编辑:)
Wheat Wizard

4

MathGolf6个 14字节

`^@╞^αmÆû-oñ╧╙

在线尝试!

考虑到尼克·肯尼迪(Nick Kennedy)提出的极端情况,必须添加8个字节。

检查是否"-o-"是两行压缩字符串的子字符串,以及第一行输入的第一个字符已删除的压缩字符串。将输入作为两个单独的字符串,唯一的变化是将字符输入为/o\\,因为这\\是在MathGolf中的字符串中输入反斜杠的正确方法。

说明

`                duplicate the top two items
 ^               zip top two elements on stack
  @              rrot3
   ╞             discard from left of string/array
    ^            zip top two elements on stack
     α           wrap last two elements in array
      mÆ         explicit map using 5 operators
        û-oñ     push "-o" and palindromize to make "-o-"
            ╧    pop a, b, a.contains(b)
                 map block ends here
             ╙   max of list

哦,这是比我的MathGolf答案更好的方法..交织后,我再次将其拆分为大小2的部分,而不是直接检查“ -o-”。
凯文·克鲁伊森


@NickKennedy好收获!我将看到如何修复代码,并在帖子通过后对其进行更新。不过可能会增加一些字节,这是不幸的。
maxb

3

05AB1E(旧版)9 8 7 字节

ζðм2ùgp

-1字节感谢@ Mr.Xcoderðм2ù

输入两个字符串的列表。

仅可用于旧版05AB1E,因为它ζ可以转置字符串列表和2D字符列表,而ζ在新05AB1E版本中仅可用于2D字符列表。

在线尝试验证所有测试用例

说明:

ζ        # Zip/transpose; swapping rows/columns, with space as default filler
 ðм      # Remove all spaces from each string
   2ù    # Only leave strings of size 2
     g   # Count how many there are left
      p  # Check if this is a prime (2 or 3)
         # (after which the result is output implicitly)

1
您的头衔打破了排行榜,
SMH

@connectyourcharger嗯,可能是因为我总是将链接bytes到代码页,所以您知道它不是用UTF-8编码的,而是使用自定义编码的。;)如果您愿意,我可以编辑答案以将编码放在标题下方,但是说实话,排行榜代码应该可以处理它。
凯文·克鲁伊森

我觉得我需要为05AB1E添加一个特殊情况-它是唯一的代码页有问题的语言之一。解决即将到来的错误。
connectyourcharger

1
努力提出修改后的正则表达式。现在,这将是一个持久的错误。如果您想提供帮助,请使用以下脚本:github.com/xMikee1/ppcg-leaderboard/edit/master/docs/script.js。我可能必须完全重构解析的字节。
connectyourcharger

您不能https://github.com/Adriandmen/05AB1E/wiki/Codepage?7在不使URL无效的情况下将URL 修改为以7为最后一个数字吗?
LF


3

Dyalog APL扩展,11 10 8字节

21⊥∧⌿⍤<

在线尝试!

说明:

21⊥∧⌿⍤<  a monadic train
       <  Compare the input with the implicit prototype element - a space.
          Returns a boolean matrix of characters that are greater than 0x20
    ∧⌿⍤   and-reduce that, i.e. places where both Jimmy and a platform is
  1      base 1 decode, aka sum - the amount of body parts over the platform
2        is that greater-or-equal to 2?

-2感谢Adám。



3

Excel,36个字节

=LEN(TRIM(MID(A2,FIND("/",A1),3)))>1

Jimmy在A1的平台上A2

查找Jimmy的位置,并占用平台的3个字节并修剪空间。如果最终的平台长度足够长,则Jimmy站立。


3

EXCEL,94 71字节。VBA(Excel),87个字节

A1=吉米(Jimmy)A2=平台

-23个字节。谢谢@Wernisch。

=(FIND("-",A2)-FIND("/",A1)<2)*(FIND("\",A1)-LEN(A2)<2)*(TRIM(A2)<>"-")

[(FIND("-",A2)-FIND("/",A1)<2)*(FIND("\",A1)-LEN(A2)<2)]*(len(replace([A2]," ",""))>1)


1
您不能使用trim代替len(replace吗?
韦尔尼施

哦!永远不会沉入我的脑海。哈哈谢谢@Wernisch :)
消除

3

///85 93 87字节

/~/\/\///\/o\\/(o)~ 
/
~
~/ (o) /(o)~ (o)-/(o)~- -/--~(o) - ~/) ~/)-~/o~/(-/1~-~/(~/)~ 

在线尝试!

如果Jimmy安全,则输出为1。否则不输出任何内容。(1和0的整数。)由于没有其他方法可以///获取输入,因此需要对其进行硬编码:

/~/\/\///\/o\\/(o)~ 
/
~
~/ (o) /(o)~ (o)-/(o)~- -/--~(o) - ~/) ~/)-~/o~/(-/1~-~/(~/)~ //<INPUT HERE> 

例如:

/\/o\\/(o)// 
/
//
/// (o) /(o)// (o)-/(o)//- -/--//(o) - ///) ///)-///o///(-/1//-///(///)//         /o\
  ------------- 

在线尝试!

注意<INPUT HERE>。后面的空格。

说明:

注意!由于有注释,因此无法运行说明代码。注释用大括号括起来。另外,原始代码使用的//是替换为的高尔夫球~。该代码从说明中省略。

/\/o\\/(o)/            {replace Jimmy with a Jimmy with curvy arms, because slashes are hard to manipulate in this language}
/ 
/
/                      {remove unneeded spaces after Jimmy, but before the floor}

/
//                     {get rid of the line break

/ (o) /(o)/            {remove all the spaces before both Jimmy and the floor}
/ (o)-/(o)/            {for each floor tile, remove it and one space before Jimmy. This detects whether Jimmy lines up with the floor.}
                       {If Jimmy is before the floor, then there will be extra floor.}
                       {If Jimmy is behind the floor, then there will be extra spaces before Jimmy.}
/- -/--/               {Handle the case where there is a hole beneath Jimmy but he is still well-supported}

/(o) - //              {Handle the case where only Jimmy's head is on the floor. The space at the end of the code is necessary for this.}
/) //                  {The rest of the substitutions clean up the result and handle each of the possible results that could exist at this point}
/)-//
/o//
/(-/1/
/-//
/(//
/)//


              /o\   
               --
 {there is a space right before this comment. The comment is only here to make the space visible and explain itself.}

  • +8个字节来修复错误
  • 通过应用标准的///高尔夫技巧获得-6个字节。

1
源代码看起来像一些奇怪的表情符号。\\(o)-(o)//
tsh


2

Haskell,59个字节

f a b=sum[1|(p,q)<-zip a$b++cycle" ",elem p"/o\\",q==' ']<2

在线尝试!

该函数的调用方式如下: f "/o\\ " " -- "

工作原理(适用于f "/o\\" " -"):

b++cycle" "-之后添加无限数量的空格,b以确保Jimmy始终位于a -" -"" - ..."

zip a$b++cycle" "-将两个字符串一起压缩([('/',' '), ('o','-'), ('\\',' ')]

(p,q)<-zip a$b++cycle -对于压缩列表中的每对

[1|(p,q)<-zip a$b++cycle" ",elem p"/o\\",q==' ']-生成一个1s 列表,其长度是满足条件的对数:

elem p"/o\\"-顶部字符串中的字符是吉米的身体部位之一。(在此示例中,所有三对均满意)

q==' '-底部字符串中的字符是一个空格。(由('/', ' ')('\\', ' ')

因此,这对必须是吉米身体部位之一位于空间上方的一对。

因为在此示例中,两对都满足两个条件,所以列表为 [1,1]

sum[1|(p,q)<-zip a$b++cycle" ",elem p"/o\\",q==' ']-取这些1s 的总和(即列表的长度),在此示例中为2

sum[1|(p,q)<-zip a$b++cycle" ",elem p"/o\\",q==' ']<2-检查空间上方的身体部位的数量是否少于2。在此示例中,不是,因此Jimmy会掉下来。:(


我只是想告诉您,您的答案可以帮助我缩短自己的答案。那谢谢啦!
小麦巫师


2

Kotlin, 60 bytes

fun String.c(b:String)=zip(b){i,j->i>' '&&j>' '}.count{it}>1

Explanation:

fun String.c  # Define an extension function on string, so we don't have to provide a first argument (and we also have string method calls for free)
(b:String)    # Pass the second string as argument
=             # Shorthand syntax for fun body
zip(b)        # Essentially a.zip(b). Creates a List<Pair> by joining both arrays. 
              # Takes care of trailing whitespace, because it will be the size of the smaller array
{i,j->        # Declare a transformer lambda as second function argument
i>' '&&j>' '} # This essentially translates to: If i!=' ' and j=='-'
.count{it}    # Count the true values
>1

Welcome to Code Golf! That's certainly a very nice first answer.
connectyourcharger

2

///, 57 bytes

/|/\/\///\/o\\/J| J/J*|
/|* /|  -/  | /|*-/|--/!|-/|*/|J|

Try it online!

Append the input to the end of the program in order to run. Returns the empty string if Jimmy falls off the platform, a string of exclamation points otherwise.

  • /|/\/\// replaces | with //, which makes the code both shorter and more readable (| is used to demarcate each replacement)
  • /\/o\\/J| J/J*/ replaces Jimmy with J for brevity and changes the space to the left of him to * to the right of him
  • The next replacement gets rid of newlines.
  • /* /| -/ | // cancels out *s and with the space to the left of the platform. If there are two or more spaces left, Jimmy is falling off to the left, and the platform is deleted. This part also removes any whitespace to the right of the platform.
  • /*-/|--/!/ cancels out *s and with length of the platform. If there are at least two - left, Jimmy isn't falling off to the right, so they are replaced with a !.
  • /-/|*/|J// deletes every remaining character that isn't !


1

Retina 0.8.2, 16 bytes

 (.*¶).
$1
¶ ?--

Try it online! Link includes test suite. Explanation:

+`^ (.*¶).
$1

While there is still a space on the first line, and both lines still have more than one character, delete the space and the first character of the next line. Note: This assumes that there is no trailing space after Jimmy. +1 byte needed if trailing space needs to be allowed.

¶ ?--

Check that there are at least two pieces of platform under Jimmy.



1

Ruby 2.5.3, 44 bytes

->a,b{a.zip(b).map(&:join).grep(/\S-/).size>1}

Input taken as two arrays. Definitely not the most golf-friendly approach (see G B's answer), but I like any excuse to use the zip function.


1

PowerShell, 63..55 53 bytes

-1 byte thanks to mazzy

param($j,$f)''+($f|% t*y|?{$j[$i++]-gt32})-match'- -'

Try it online!

Takes input as two lines.

Unrolled:

param($j,$f)            #Take $jimmy and $floor
''+                     #Implicitly converts next part to string
($f |% ToCharArray      #Convert $f to a char[] and...
    |?{                 #Only take the chars where...
        $j[$i++]-gt32   #The same indexed char in $j's ASCII # is > ' ' i.e. only get /o\
     }
)-match'- -'            #Arrays.ToString are joined with a space and we need 2 -'s

Huh. I never considered PowerShell a great golfing language, but I guess it's actually not that bad.
connectyourcharger

@connectyourcharger It can do some pretty neat stuff with the pipeline but there's definitely a few areas where it's a huge pain in the ass.
Veskah

I never bothered to learn it because of those pain-in-the-ass reasons.
connectyourcharger



1

Python 3.7, 71 56 Bytes

lambda s:sum(j in'/o\\'and p=='-'for j,p in zip(*s))>1

Very simple version and the shortest I could think of using this approach. Input s is a list of two strings, the first one for the Jimmy-row, the second one for the platform. Zip the characters which are above one another and then check whether - is below a part of Jimmy in at least two instances.

EDIT: Reduced by quite a few Bytes thanks to Blue!


1
Hi, welcome to Code Golf! Couple things: you might want to specify the input format in your answer (looks like s should be two lists, one for jimmy and one for the platform?). Also, there are a few places you can save bytes: instead of summing a list, you can sum the raw iterator (remove the brackets); instead of checking >=2, check >1; you can sum True and False like 1 and 0, no need for the if-else. Finally, before posting a new answer in the same language, you should look at the previous ones to see if you can improve.
Blue

@Blue Hi, thanks a lot! And yeah I will keep that in mind in the future. :)
Michael

1

Chevron, 84 87 bytes

>^__>^j
^i<<0
^i<<^i+1
^h<^i>^j
->+2??^h=/
->-3
>^__>^p
^q<^i|2>^p
->+2??^q<--
><0
><1

This is a fairly new language of my own creation - prototype interpreter, documentation, and example programs can be found at https://github.com/superloach/chevron.

Explanation:

  • >^__>^j - take jimmy as TXT input
  • ^i<<0 - initialise index to 0
  • ^i<<^i+1 - increment index
  • ^h<^i>^j - get character of jimmy at index
  • ->+2??^h=/ - jump out of loop at jimmy's left leg
  • ->-3 - hop back to beginning of loop
  • >^__>^p - take platform as TXT input
  • ^q<^i|2>^p - cut out 3 characters under jimmy
  • ->+2??^q<-- - if 2 dashes under jimmy, jump to truthy exit
  • ><0 - falsy exit
  • ><1 - truthy exit

I am aware that my code fails to handle single-character platform cases - updated version will be edited soon.
Superloach

0

C (gcc), 103 bytes

i,j,l;f(char*s){for(i=0;*s-10;j=*s++-47?j:i)++i;l=strlen(s)>j+1;s+=j;return l&&*s+s[1]+(s[2]?:32)>109;}

Try it online!

Would be much shorter (75 bytes), if it could be assumed that there is trailing whitespace after the platform.




0

V, 18 bytes

0vt/"_dj.d3lVkp0#x

Try it online!

Output whitespace only if jimmy fall of the platform. Output something non-whitespace if jimmy stay on the platform.


Not using Ctrl or Esc in Vim is funny.
tsh
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.