按照奇怪的厨房用具的主题,让我们谈谈我的微波炉。
我的微波炉有一个键盘,布局如下:
30 > 15
1 2 3
4 5 6
7 8 9
+/- 0 S/M如果我按下0 3 +/- 15 S/M 4 >,我会用微波炉加热我的食物12分钟41秒:
其他测试用例:
0 3 +/- 15 S/M 4 S/M 1 S/M +/- 0 S/M 7 -/+ 30 > = 1101挑战
给我写一个程序,以按钮按下的字符串作为输入(分离器不重要),并输出我的微波炉运行多长时间(以秒计)。这是代码-高尔夫,所以最短的字节代码获胜!
这些是您的程序应该接受的按钮代码:(我应该能够准确地输入这些字符串):
0 1 2 3 4 5 6 7 8 9 (0等于10)3015+/-S/M>发布于 2021-12-06 16:03:22
期望命令列表。返回数秒。假设最后一个命令是>,如注释中所述。
a=>a.map(c=>1/c?t+=s*(+c||10)*(c>9|a||60):c<','?s=-s:a^=1,t=0,s=1)|ta => // a[] = input array, reused as a flag to figure out
// if we are in 'seconds' mode
a.map(c => // for each command c in a[]:
1 / c ? // if c is numerical:
t += // add to t:
s * // the sign multiplied by
(+c || 10) * // either c, or 10 if c is 0
( // multiplied by
c > 9 // 1 if c is 15 or 30
| a // or we are in seconds mode
|| 60 // otherwise 60
) //
: // else:
c < ',' ? // if c is '+/-':
s = -s // invert the sign
: // else:
a ^= 1, // switch between 'seconds' and 'minutes' mode
// (c is either 'S/M' or '>')
t = 0, // start with t = 0
s = 1 // start in 'add' mode
) | t // end of map(); return t发布于 2021-12-06 20:49:38
@set/at=0,s=1,m=60
:l
@set/pb=
@if "%b%"==">" echo %t%&exit/b
@if %b%==+/- set/as=-s
@if %b%==S/M set/am=60/m
@if %b%==0 set b=10
@if %b% gtr 10 (set/at+=s*b)else set/at+=s*m*b
@goto l在STDIN上接受以新行结尾的输入,因为在命令行上使用>是很棘手的.解释:
@set/at=0,s=1,m=60初始化总数并签名和分钟标志。
:l开始循环。
@set/pb=读入下一个按钮。
@if "%b%"==">" echo %t%&exit/b如果这是>按钮,则输出总计和完成。
@if %b%==+/- set/as=-s
@if %b%==S/M set/am=60/m更新+/-和S/M按钮的标志。
@if %b%==0 set b=100算作10。
@if %b% gtr 10 (set/at+=s*b)else set/at+=s*m*b
@goto l更新秒数并返回循环。请注意,非数字字符串静默地计算为0,从而使整个字符串不受干扰。
https://codegolf.stackexchange.com/questions/238134
复制相似问题