Answers:
l
另一种内置解决方案,但该解决方案的名称最短(除非有人找到一种以零字节为单位执行此任务的语言)。这在Brachylog 1和Brachylog 2中都应该起作用。
这是一个函数提交(TIO链接包含一个命令行参数,使解释器运行单个函数而不是整个程序),部分是因为否则我们将不得不在输出上花费字节,部分是因为Brachylog的负数语法数字有点不寻常,并且使该程序成为函数可以解决有关输入语法的任何潜在参数。
经常让我感到困扰的是,Bracylog的大多数内建函数都将负数视为正数,但是事实最终在这里派上了用场。我想每种高尔夫语言都需要权衡取舍。
1 is waiting at Starchild Numerology.Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Crime Lab.'-' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 1 r 2 r 2 l.Switch to plan "n" if no one is waiting.-1 is waiting at Starchild Numerology.[n]0 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 1 l 1 l 2 l.Pickup a passenger going to Cyclone.Pickup a passenger going to Addition Alley.Go to Cyclone:e 1 l 2 r.[r]Pickup a passenger going to Cyclone.Pickup a passenger going to Addition Alley.Go to Zoom Zoom:n.Go to Addition Alley:w 1 l 1 r.Pickup a passenger going to Addition Alley.Go to Chop Suey:n 1 r 2 r.Switch to plan "f" if no one is waiting.Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:n 1 l 3 l 1 l.Go to Cyclone:n 1 l.Switch to plan "r".[f]Go to Addition Alley:n 1 l 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:n 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.
取消高尔夫:
1 is waiting at Starchild Numerology.
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
Pickup a passenger going to Crime Lab.
'-' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 3rd left.
Pickup a passenger going to Crime Lab.
Go to Crime Lab: north 1st right 2nd right 2nd left.
Switch to plan "n" if no one is waiting.
-1 is waiting at Starchild Numerology.
[n]
0 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 1st left 1st left 2nd left.
Pickup a passenger going to Cyclone.
Pickup a passenger going to Addition Alley.
Go to Cyclone: east 1st left 2nd right.
[r]
Pickup a passenger going to Cyclone.
Pickup a passenger going to Addition Alley.
Go to Zoom Zoom: north.
Go to Addition Alley: west 1st left 1st right.
Pickup a passenger going to Addition Alley.
Go to Chop Suey: north 1st right 2nd right.
Switch to plan "f" if no one is waiting.
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park: north 1st left 3rd left 1st left.
Go to Cyclone: north 1st left.
Switch to plan "r".
[f]
Go to Addition Alley: north 1st left 2nd left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: north 1st right 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st left 1st right.
说明:
Pickup the input and split it into individual characters
Pickup the value 1.
If the first character a hyphen, add -1. Otherwise, add 0.
Keep picking up characters and adding 1 until you're out.
Convert the running total to a string and print to stdout.
Zoom Zoom
的每个循环都停在plan "r"
。我只是对其进行了高达100,000位数的测试,而且它从来没有用完。我没有计算出来,但我想它所收取的票价足以支付所用的汽油,因此它在每个回路中都充满了油箱。
IntegerLength
有一个内置的...回报0
的0
。
There's a built-in
:什么时候不在那里?
//; 'q<)e
o!@i -
为此找到一个像样的布局非常棘手。由于空格,<
和和;
,我仍然不太满意,但这是我目前能做的最好的事情。
字符串长度是Alice中不存在的那些非常常见的内置函数之一,因为它的输入是字符串,而输出是整数(并且所有Alice命令严格来说都是整数到整数或字符串到字符串)。我们可以通过以下方式测量字符串的长度:在“序数”模式下将其写入磁带,然后在“基数”模式下找到其末端。
/ Reflect to SE. Switch to Ordinal. While in Ordinal mode, the IP will bounce
diagonally up and down through the code.
! Store an implicit empty string on the tape, does nothing.
; Discard an implicit empty string, does nothing.
i Read all input as a string.
'- Push "-".
< Set the horizontal component of the IP's direction to west, so we're bouncing
back now.
- Remove substring. This deletes the minus sign if it exists.
'i Push "i".
; Discard it again.
! Store the input, minus a potential minus sign, on the tape.
/ Reflect to W. Switch to Cardinal. The IP immediately wraps to the
last column.
e) Search the tape to the right for a -1, which will be found at the end
of the string we stored there.
< Does nothing.
q Push the tape head's position, which is equal to the string length.
'<sp> Push " ".
; Discard it again.
/ Reflect to NW. Switch to Ordinal. The IP immediately bounces off
the top boundary to move SW instead.
o Implicitly convert the string length to a string and print it.
IP bounces off the bottom left corner, moves back NE.
/ Reflect to S. Switch to Cardinal.
! Store an implicit 0 on the tape, irrelevant.
The IP wraps back to the first line.
/ Reflect to NE. Switch to Ordinal. The IP immediately bounces off
the top boundary to move SE instead.
@ Terminate the program.
我也尝试使用H
(绝对值)来照顾基数模式下的减号,但是额外的模式切换总是使我的尝试更加昂贵。
<?=-~log10(abs($argn));
绝对值以10为底的对数加上一个强制转换为int的对数
输入log10为零时返回INF
为false
更好的方法是$argn
用$argn?:1
+3字节替换
<?=strlen($argn)-($argn<0);
字符串长度减去布尔值小于零
+2字节用于字符串比较 $argn<"0"
<?=preg_match_all("#\d#",$argn);
正则表达式计算所有数字
<?=strlen($argn)-strspn($argn,"-");
字符串长度减去计数 -
10
,因为的^
优先级较低。您可以使用修复它-~
。
<?=strlen(abs($argn));
?
$argn?:1
?使用log10()
和将为26个字节abs()
。
-~$x
等同于((int)$x)+1
。<?=-~log10(abs($argn?:1));
似乎有效。
program c
character b
call get_command_argument(1,b,length=i)
print*,i-index(b,'-')
end program
说明:
从参数的长度中减去“-”号的索引。
在Fortran中,数组从1开始,如果找不到符号,index()返回0。
编辑:切换到隐式整数“ i”,也合并参数getter。
编辑:-1字节感谢@Tsathoggua
-[+>+[+<]>+]>->,[-<->]<[>+>],[<+>,]<.
输出是按字节值。
-[+>+[+<]>+]>-> Constant for 45 (from esolangs wiki)
, Read a byte of input
[-<->] Subtract that byte from 45
<[>+>] If the result is nonzero then increment a cell and move to the right
(0 means it was a minus; so not counted)
,[<+>,] Read a byte and increment the cell to its left until EOF is reached
<. Print the cell that was being incremented
DL
这确实符合要求:
DL - Main link number n e.g. -45
D - convert to a decimal list [-4,-5]
L - get the length 2
D
对小数有效吗?会-1.2
输出[-1,-0.2]
吗?我自己尝试过,但事实并非如此。
654.321D
会产生[6,5,4.321]
(实际上是[6.0,5.0,4.321000000000026]
)
[-6.0, -5.0, -4.321000000000026]
,实际上,显然。
s=>s.length-(s<0)
保存6个字节
i->(""+i.abs()).length()
假设i
是一个BigInteger
。而且,该类型是上下文相关的,因此不需要导入,如测试代码所示。
// No imports
class Pcg120897 {
public static void main(String[] args) {
java.util.function.ToIntFunction<java.math.BigInteger> f =
// No full class declaration past here
i->(""+i.abs()).length()
// No full class declaration before here
;
System.out.println(f.applyAsInt(new java.math.BigInteger("-1267650600228229401496703205376"))); // -(2^100)
System.out.println(f.applyAsInt(new java.math.BigInteger("1267650600228229401496703205376"))); // (2^100)
}
}
+""
代替.toString()
?
i
答案的类型。我认为更多的lambda答案应该做到这一点。
([({})]((((()()()()())){}{})){}{})((){[()](<{}>)}{})([{}][]<>)
这是62个字节的代码,而+1个字节用于-a
标志。
我尝试了另外两种方法,但是不幸的是,它们都更长:
([]<({}[((((()()()()())){}{})){}{}]<>)((){[()](<{}>)}{})>)({}[{}])
([]<>)<>({}<>)((((([][]())){}{})){}{}[{}])((){[()](<{}>)}{})([{}]{})
这应该是一个简短的答案。实际上,如果我们不必支持负数,我们可以这样做:
([]<>)
但是我们必须将第一个输入与第45个(ASCII -
)首先进行比较,这是此答案的大部分字节数。
算术解决方案可能会更短。
([{}]((((()()()()())){}{})){}{})({(<()>)}{}[]<>)
->a{a.abs.to_s.size}
->a{a.abs.to_s.size}[-95]
->a{a.abs.to_s.size}.call(-92)
nchar(abs(scan()))
/d/
O@IHc
这是一个非竞争性的解决方案,因为在发布此挑战时,命令c
已在官方(且仅是:D)解释器中进行了错误处理。同时,Martin Ender对其进行了修复,因此现在可以使用。
指令指针/
多次通过两个镜像(),因此可能很难遵循。我将尝试使用基本方向尽可能清楚地解释它(例如N向上,SW向下向左...)。我称/1
最左边的镜子/2
为最右边的镜子。
Command Direction Comment
E Execution starts from the upper-left corner going right
/1 E → SE Passing through the mirror changes direction and switches
to ordinal mode (string operations)
I SE → NE Push the input string to the stack, then bounce against
the bottom of the code
/2 NE → S Back to cardinal mode (numeric operations)
H S Pop n, push abs(n). Execution wraps from bottom to top
/2 S → SE Ordinal mode again
c SE → NW Pop s, push each char of s separatedly. Bounce against
the bottom right corner
/2 NW → W Cardinal mode
d W Push the depth of the stack (which is now equal to
the number of characters in abs(input))
/1 W → NW → SW Pass through the mirror, then bounce agains the top
O SW → NE Output the result, then bounce on the bottom left corner
/1 NE → S Last mirror, I promise
@ S Terminate execution
命令式:
$(eval T=$1)$(foreach D,$(shell seq 9),$(eval T=$(subst $D,? ,$T)))$(words $T)
功能样式,113字节:
$(eval 2?=$(shell seq 9))$(if $2,$(call $0,$(subst $(word 1,$2),? ,$1),$(wordlist 2,$(words $2),$2)),$(words $1))
Pure Make,83个字节:
$(eval T=$1)$(foreach D,0 1 2 3 4 5 6 7 8 9,$(eval T=$(subst $D,? ,$T)))$(words $T)
#include<string>
int main(int,char**c){printf("%d",strlen(c[1])-(*c[1]<46));}
打印参数的长度,如果第一个字符为负号bool
,1
则减1,因为可以保证转换为if true
或0
iffalse
<46
代替=='-'
,并尊重数组而不是[]
c[1][0]=='-'
为来减少4个字节,*c[1]<46
因为我们可以假定输入将始终是有效整数。(除非允许使用'-'以外的前缀...)
length(toString(abs(Ans
TI-Basic是一种标记化语言;length(
和toString(
每个是两个字节。
Ans
用作隐式输入;最后(唯一)行的值隐式返回。
很简单,将绝对值除掉负号,转换为字符串,返回字符串的长度。
6字节的数学方法不适用于0:
1+log(abs(Ans
toString(
?