此按钮有什​​么作用?


11

目前,在我的工作中,我们搬到了新建筑物。它应该是最先进的,并具有自动照明,自动卷帘百叶窗,现在被称为该镇最环保的建筑。
但是,它并不是真的工作得很好。.阴天有时会卷起百叶窗,当太阳开始发光时会向上卷起,并且有时每隔5-10次交替打开和关闭,灯光有时会导致迪斯科效果分钟。另外,我们没有任何手动方式来控制这些下拉式百叶窗,也没有温度,但是我们确实有一个遥控器来控制灯光。
但是,这些遥控器没有说明手册,并且包含20多个按钮,用于执行除您似乎需要的操作之外的所有操作。

PS:我在1.5个月前在沙盒中编写了此挑战。目前,我们有点了解遥控器的工作原理。.
这栋建筑的一大优势是,室外温度为摄氏30度以上,而内部温度始终保持在21摄氏度。

因此,这就是介绍,灯光的遥控器激发了这一挑战。

挑战:

假设我们最初关闭了灯:

L

然后我们按下所有按钮,然后输出灯的状态。

对于灯泡遥控器的不同功能,我们使用以下数字。当该数字前面有减号时,我们做相反的事情。

  • 1=开; -1=关。
  • 2=增加力量25%;-2=降低(昏暗)强度25%。
  • 3=点差增加50%;-3=减少点差50%。
  • 4 =切换灯光类型。

所以,这一切都非常模糊,因此让我们更深入地了解每件事的含义:

因此,-1(OFF)很明显,1它将进入初始的ON状态(表示为50%,表示为250%3):

   //
  //
L ====
  \\
   \\

2以及-2光的传播距离(==每25%附加一个):

0% (equal to a light bulb that is OFF)
L

25%
L ==

50%
L ====

75%
L ======

100%
L ========

3并且-3是光差多远:

0%:
L ====

50%:
   //
  //
L ====
  \\
   \\

100%:
|| //
||//
L ====
||\\
|| \\

(注意:当23都为100%时,您将处于以下状态:

||   //
||  //
|| //
||//
L ========
||\\
|| \\
||  \\
||   \\

4 是切换灯光类型。

Default:
  //
L ==
  \\

After toggling (note the single space before '/' instead of two):
 /
L --
 \

输入:

您将收到包含可能的按钮按下的输入。例如:

12-34

输出:

依次按输入的所有按钮后,指示灯的状态。因此,使用上面的示例输入,我们得到以下输出:

L ------

挑战规则:

  • 输入仅包含1234-(且-之前不包含4)。
  • 您永远不能低于0%或高于100%。如果数字会超出这些界限而增加/减少,则可以忽略它。
  • 灯泡熄灭时,您可以忽略任何操作,再次打开灯泡时,它将重置为初始ON状态(2和均为50%3,默认为4)。例如:12-1-341将仅打印上述初始ON状态。(提示:您可以忽略最后1输入之前的所有内容-除外-1。)
  • 尾随空格等于灯的长度或一条尾随新行不受限制。但是,没有添加多余的新行。
  • 您可以将输入作为整数列表而不是单个字符串。因此12-34,可以代替[1,2,-3,4]输入。

一般规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您使用非代码高尔夫球语言发布答案。尝试针对“任何”编程语言提出尽可能简短的答案。
  • 标准规则适用于您的答案,因此允许您使用STDIN / STDOUT,具有适当参数的函数/方法,完整程序。你的来电。
  • 默认漏洞是禁止的。
  • 如果可能的话,请添加一个带有测试代码的链接。
  • 另外,如有必要,请添加说明。

测试用例:

12-34
L ------

12-1-341
   //
  //
L ====
  \\
   \\

14-3224333-2
||  //
|| //
||//
L ======
||\\
|| \\
||  \\

142-1-314-3-322
L --------

1324-2-3
  /
 /
L ----
 \
  \

134
| /
|/
L ----
|\
| \

1-2-2-214-3-3-3
L ----

1
输出是134什么?
PurkkaKoodari

@ Pietu1998我已经添加了测试用例(并修复了关于toggle后空格的一些问题4)。感谢您的询问。
凯文·克鲁伊森

1
我认为第三个测试用例的传播不正确。
PurkkaKoodari

1
如果打开并1按下了指示灯,是否重置参数?
PurkkaKoodari

2
在这种天气下,您的办公室21度吗?我想让你不羡慕。
尼尔

Answers:


6

Python 2,221字节

for b in[-1]+input():exec["p=b>0;d=2;s=1;t=2","d+=b/2*(-2<d+b<6)","s+=b/3*(-3<s+b<5)","t=3-t"][abs(b)-1]
i=c=(s>0)*d*p
q='print" |"[s/2]*t+" "*i+t*%r;'
exec('i-=1;'+q%'/')*c
print"L "+" -="[t]*2*d*p
exec(q%'\\'+'i+=1;')*c

这比我预期的更长。第一行计算灯的状态,其余行执行打印。

输入是通过STDIN以数组形式给出的。

查看有关ideone的测试案例


3

R,323320字节

    z=scan();a=c=1;b=d=2;for(i in 1:sum(1|z)){s=sign(y<-z[i]);switch(y/s,b<-d<-2*(c<-a<-y),b<-b+s,c<-c+s,d<-2-d);b=min(b,4);c=min(c,2);b=b*(b>0);c=c*(c>0)}
    s="/";v=if(c>1)"|"else" ";for(i in a*b:-b){if(i)cat(v,if(d)v,rep(" ",abs(i)-1),s,if(d)s,"\n",sep="")else{cat("L ",if(d)rep("==",b)else rep("--",b),"\n",sep="");s="\\"}}

取消高尔夫:

z=scan()

读取输入行(用空格分隔的整数)

a=c=1;b=d=2

初始化变量a(打开),b(亮度),c(宽度),d(光束类型)。d为零或二,这意味着我们可以稍后调用if(d),而不是更长的if(d> 1)或类似名称,并节省几个字节。

while(any(z|1))

golf-y的书写方式,while(length(z))其中z是整数向量。

第一行的其余部分通过switch语句处理输入。第二行打印出来。

可能有一些<-可以替换为=,但是我认为您会被词汇作用域所吞噬...

另请注意,在R中,需要转义反斜杠。

c*(c>0)是一种max(c,0)保存角色的高尔夫式写作方式。

如果灯不亮,则由于*优先级低于:,因此for(i in a*b:-b)循环仅遍历0:0

更新;通过用for(而不是while)替换第一行中的循环,节省了3个字节。请注意,1:sum(1|z)字符数少于1:length(z)seq_along(z)seq(z)在大多数情况下都可以使用,但z长度为1则不行。给出的解决方案不适用于长度为零的输入,但我希望这不在竞争范围之内。


2

Kotlin,445字节

我第一次参加Kotlin高尔夫,比Java少38个字节:)

fun f(z:IntArray)={var a=1<0;var b=2;var c=1;var d=a
z.map{when(it){1->{a=1>0;b=2;c=1;d=!a}-1->a=1<0;2->if(b<4)b+=1;-2->if(b>0)b-=1;3->if(c<2)c+=1;-3->if(c>0)c-=1;4->d=!d}}
var r="";val l=if(c>1)if(d)"|" else "||" else if(d)" " else "  "
if(c>0)for(i in b downTo 1)r+="${l+" ".repeat(i-1)+if(d)"/" else "//"}\n"
r+="L ${(if(d)"--" else "==").repeat(b)}\n"
if(c>0)for(i in 1..b)r+=l+" ".repeat(i-1)+"${if(d)"\\" else "\\\\"}\n"
if(a)r else "L"}()

使用空格和测试:

fun f(z: IntArray) = {
    var a = false // ON / OFF
    var b = 2 // Strength [0,4]
    var c = 1 // Spread [0,2]
    var d = a // Type

    // Find state to print
    z.map {
        when (it) {
            1 -> {
                a = true
                b = 2
                c = 1
                d = !a
            }
            -1 -> a = false
            2 -> if (b < 4) b += 1
            -2 -> if (b > 0) b -= 1
            3 -> if (c < 2) c += 1
            -3 -> if (c > 0) c -= 1
            4 -> d = !d
        }
    }

    var r = ""
    val l = if (c > 1) if (d) "|" else "||"
    else if (d) " " else "  "

    // Print state
    if (c > 0) for (i in b downTo 1) {
        r += "${l + " ".repeat(i - 1) + if (d) "/" else "//"}\n"
    }
    r += "L ${(if (d) "--" else "==").repeat(b)}\n"
    if (c > 0) for (i in 1..b) {
        r += "${l + " ".repeat(i - 1) + if (d) "\\" else "\\\\"}\n"
    }

    /* return */ if (a) r else "L"
}()

fun main(args: Array<String>) {
    println(f(intArrayOf(1, 2, -3, 4)))
    println(f(intArrayOf(1, 2, -1, -3, 4, 1)))
    println(f(intArrayOf(1, 4, -3, 2, 2, 4, 3, 3, 3, -2)))
    println(f(intArrayOf(1, 4, 2, -1, -3, 1, 4, -3, -3, 2, 2)))
    println(f(intArrayOf(1, 3, 2, 4, -2, -3)))
    println(f(intArrayOf(1, 3, 4)))
    println(f(intArrayOf(1, -2, -2, -2, 1, 4, -3, -3, -3)))
}

有趣的是,使用函数赋值(计算的lambda的值)要短一些,而不是正常地定义函数和/ printreturn创建的字符串fun f() =。(这种描述有意义吗?)

我只是希望SE能够正确显示Kotlin语法


2

Java的8,484个 483 452 446 440字节

z->{int a=1,b=2,c=1,d=0,j,k;for(int i:z){d=i==1?0:i>3?1-d:d;a=i*i==1?i:a;b+=i==1?2-b:i==2&b<4?1:i==-2&b>0?-1:0;c+=i==1?1-c:i==3&c<2?1:i==-3&c>0?-1:0;}String t=d<1?"=":"-",q=d<1?"//":"/",x=d<1?"\\\\":"\\",n="\n",y=" ",w=d<1?y+y:y,g=c>1?d<1?"||":"|":w,r="";if(c>0)for(r+=g,j=b;j-->0;r+=q+n+(j>0?c>1?g:w:""))for(k=j;k-->0;r+=y);for(r+="L ",j=b;j-->0;r+=t+t);r+=n;if(c>0)for(r+=g;++j<b;r+=x+n+(j<b-1?g:""))for(k=j;k-->0;r+=y);return a>0?r:"L";}

终于,好吧,我自己的挑战比我预期的要难一些。

毫无疑问,这可以通过使用完全不同的方法来解决。.现在,我首先确定要执行的操作,然后打印。印刷实际上是这项挑战中最困难的,恕我直言。

-6个字节,感谢@ceilingcat

说明:

在这里尝试。

z->{                          // Method with integer-array parameter and String return-type
  int a=1,                    //  ON/OFF flag, initially ON
      b=2,                    //  Strength, initially 50%
      c=1,                    //  Spread, initially 50%
      d=0,                    //  Type of light, initially two lines
      j,k;                    //  Index-integers
  for(int i:z){               //  Loop over the input-array
    d=i==1?0:i>3?1-d:d;       //   Determine the new type of light
    a=i*i==1?i:a;             //   Determine if the light is ON/OFF
    b+=i==1?2-b:i==2&b<4?1:i==-2&b>0?-1:0;
                              //   Determine the new strength
    c+=i==1?1-c:i==3&c<2?1:i==-3&c>0?-1:0;}
                              //   Determine the new spread
  String t=d<1?"=":"-",       //  Horizontal light symbol
         q=d<1?"//":"/",      //  Upper diagonal light symbol
         x=d<1?"\\\\":"\\",   //  Bottom diagonal light symbol
         n="\n",              //  New-line
         y=" ",               //  Space
         w=d<1?y+y:y,         //  One or two spaces?
         g=c>1?d<1?"||":"|":w,//  Space(s) or vertical light symbol(s)?
         r="";                //  Result String, starting empty
  if(c>0)                     //  Do we have spread >0%?
    for(r+=g,j=b;j-->0;r+=q+n+(j>0?c>1?g:w:""))for(k=j;k-->0;r+=y);
                              //   Create upper light part
  r+="L ";                    //  Light-bulb
  for(j=b;j-->0;r+=t+t);      //  Horizontal light next to the light-bulb
  r+=n;
  if(c>0)                     //  Do we have spread >0%?
    for(r+=g;++j<b;r+=x+n+(j<b-1?g:""))for(k=j;k-->0;r+=y);
                              //   Create bottom light part
  return a>0?                 //  Is the light turned ON?
             r                //   Return the created result-String
            :                 //  Else:
             "L";}            //   Return just "L"

1

批次,552字节

@echo off
for %%a in (1 %*) do call:l %%a
set/aw*=o,l*=o
if %w% gtr 0 for /l %%a in (%l%,-1,1)do call:w %%a /
set s=----
if %t%==1 set s=====
call echo L %%s:~-%l%%%%%s:~-%l%%%
if %w% gtr 0 for /l %%a in (1,1,%l%)do call:w %%a \
exit/b
:w
set v= 
set u=%2
if %w%==2 set v=l
if %t%==1 set u=%2%2&set v=%v%%v%
set s=    
call set s=%%s:~-%1%%
echo %v:l=^|%%s:~1%%u%
exit/b
:l
if %1==1 set/ao=w=t=1,l=2
if %1==-1 set/ao=0
if %1==2 set/al+=1-l/4
if %1==-2 set/al-=!!l
if %1==3 set/aw+=1-w/2
if %1==-3 set/aw-=!!w
if %1==4 set/at^=1

注意:set v=包含一个尾随空格,并且set s=包含三个尾随空格。这确实很尴尬,因为您不能|在Batch中轻松打印可变数量的s,因此必须使用占位符并将其替换为echo语句。


0

05AB1E,106 个字节

“/|= 
L“•Wθ¨S9ƒTª»þúÙ•6вèJ¶¡sŽ8ÃS«1¡θΣÄ}.γÄ}ODd*©н8‚ß8αF樚NÈi¨]R®θ8Öi"||//="2ô…|/-S:}®Ås3/©_iθ}®i'|ð:}».∊

输入为整数列表。

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

说明:

“/|= 
L               # Push string "/|=\nL"
  Wθ¨S9ƒTª»þúÙ• # Push compressed integer 9569494169631511496055972036
   6в            # Converted to Base-6 as list: [5,3,2,2,2,2,2,2,2,2,4,1,1,0,0,4,1,1,3,0,0,4,1,1,3,3,0,0,4,1,1,3,3,3,0,0]
     è           # Index each into the string
      J          # Join everything together

现在,我们有了字符串:

L ========
||//
|| //
||  //
||   //

然后,我们将:

¶¡               # Split it by newlines: ["L ========","||//","|| //","||  //","||   //"]
s                # Swap to take the (implicit) input-list
 Ž8Ã             # Push compressed integer 2234
    S            # Converted to a list of digits: [2,2,3,4]
     «           # Append it at the end of the input-list
                 #  i.e. [4,3,3,2,1,3,2,4,-2,-3] → [4,3,3,2,1,3,2,4,-2,-3,2,2,3,4]
 1¡              # Then split on 1
                 #  i.e. [4,3,3,2,1,3,2,4,-2,-3,2,2,3,4]
                 #   → [[4,3,3,2],[3,2,4,-2,-3,2,2,3,4]]
   θ             # Only leave the last inner list
                 #  i.e. [[4,3,3,2],[3,2,4,-2,-3,2,2,3,4]] → [3,2,4,-2,-3,2,2,3,4]
    ΣÄ}          # Sort on the absolute value
                 #  i.e. [3,2,4,-2,-3,2,2,3,4] → [2,-2,2,2,3,-3,3,4,4]
       .γÄ}      # Then group by absolute value
                 #  i.e. [2,-2,2,2,3,-3,3,4,4] → [[2,-2,2,2],[3,-3,3],[4,4]]
           O     # Then take the sum of each group
                 #  i.e. [[2,-2,2,2],[3,-3,3],[4,4]] → [4,3,8]
            Dd   # Duplicate it, and check for each if it's non-negative (>= 0)
                 #  i.e. [4,3,8] → [1,1,1]
              *  # Multiply the two lists
                 #  i.e. [4,3,8] and [1,1,1] → [4,3,8]
               © # And store the result in the register (without popping)
н                # Now take the first value (the strength)
                 #  i.e. [4,3,8] → 4
 8              # Pair it with 8
                 #  i.e. 4 → [4,8]
   ß             # Pop and push the minimum of the two
                 #  i.e. [4,8] → 4
    8α           # And then calculate the absolute difference with 8
                 #  i.e. 4 → 4
      F          # Loop that many times:
       ć         #  Extract the head of the string-list
                 #   i.e. ["L ========","||//","|| //","||  //","||   //"] → "L ========"
        ¨        #  Remove the last character
                 #   i.e. "L ========" → "L ======="
         š       #  And prepend it back to the list again
                 #   i.e. ["||//","|| //","||  //","||   //"] and "L ======="
                 #    → ["L =======","||//","|| //","||  //","||   //"]
       NÈi       #  And if the loop-index is even:
          ¨      #   Also remove the last item of the string-list
                 #    i.e. ["L =======","||//","|| //","||  //","||   //"]
                 #     → ["L =======","||//","|| //","||  //"]
      ]          # Close both the if and loop
                 #  i.e. ["L ========","||//","|| //","||  //","||   //"] and 4
                 #   → ["L ====","||//","|| //"]
       R         # Then reverse the list
                 #  i.e. ["L ====","||//","|| //"] → ["|| //","||//","L ===="]
®                # Push the list from the register again
 θ               # Now take the last value (the toggle)
                 #  i.e. [4,3,8] → 8
  8Öi         }  # If it's divisible by 8:
                 #  i.e. 8 → 1 (truthy)
     "||//="     # Push string "||//="
            2ô   # Split into parts of size 2: ["||","//","="]
     …|/-        # Push string "|/-"
         S       # Split into characters: ["|","/","-"]
     :           # And replace all of them in the string-list
                 #  i.e. ["|| //","||//","L ===="] → ["| /","|/","L ----"]
®                # Push the list from the register again
 Ås              # Now take the middle value (the spread)
                 #  i.e. [4,3,8] → 3
   3/            # Divide it by 3
                 #  i.e. 3 → 1
     ©           # Store it in the register (without popping)
      _i }       # If it's exactly 0:
                 #   i.e. 1 → 0 (falsey)
        θ        #  Only leave the last value of the string-list
     ®i    }     # If it's exactly 1 instead:
                 #   i.e. 1 → 1 (truthy)
       '|ð:     '#  Replace all "|" with spaces " "
                 #   i.e. ["| /","|/","L ----"] → ["  /"," /","L ----"]
»                # Then join the string-list by newlines
                 #  i.e. ["  /"," /","L ----"] → "  /\n /\nL ----"
 .∊              # And finally intersect mirror everything vertically
                 # (which automatically converts the slashes)
                 #  i.e. "  /\n /\nL ----" → "  /\n /\nL ----\n \\n  \"
                 # (And output the result implicitly)

看到这个05AB1E尖矿(部分如何压缩大的整数?以及如何压缩整数列表?理解为什么•Wθ¨S9ƒTª»þúÙ•6в[5,3,2,2,2,2,2,2,2,2,4,1,1,0,0,4,1,1,3,0,0,4,1,1,3,3,0,0,4,1,1,3,3,3,0,0]; 并且Ž8Ã2234

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.