brainfuck - 617个 616 604字节
+>>>>,[>++++[<-------->-]<[>>>>],]<<<<[>+<<+<+>>-]<[>+<-]+<+<<[>+>-<<-]>[<+>-]>[,+++++[>+++++<-]>>[<->-]<[>>>>>[>>>>]+[<<<<]<-]>>[<+>-]<<<]>[>>[,<]<<+++++++++<]<<<[-[+>>-<]>[>>[<+<+>>-]<<<<[>+>-<<-]>[<+>-]>[<<[<<<<]>>>>[[<<<<+>>>>-]>>>>]<<<<+>>-]>[>+<-]]<<<[-[+>]+<<<<]>>>>-<<<<<]>>>>>+++++[>----<-]>->[<+>>+<-]<[<<<[<<<<]+[>>>>]<-]>>[<+>-]<[<<<<]>>>++++[<-------->-]>[-[,+++>]+>>>[<<<->>]>]<<<<<[>-]>[>>]>>+[<++++[<++++++++>-]<]>>[+++++++++++++>>>>]<<<<----<+++[<<+<<[<<+<<]+[>>>>]<<<<-]<<<<[-<<<<]>[.,>>]<<<<<[<<<<]<++++++++++<<.<+<<+<<+<<+<<+<[.,>>]<<<<<[<<]>++++++++++<+<<+<<+<..<+<[.,>>]<[<<]<...<<.
这花了我两天的大部分时间。我认为这是值得的。通过改变存储在什么单元格中的东西,也许有些零件可以打更多的高尔夫球,但是现在我很高兴自己能工作。
如果问题未指定对输入进行排序,则该程序必须完全不同。这种工作方式是在输入的引脚周围构造10个引脚的列表。这有点令人困惑,但是也许这可以更好地解释它:
If you input these pins: [2, 3, 6, 8, 9]
First, the program does this: [2, 3, 6, 8, 9] + [10]
Then this: [2, 3, 6] + [7] + [8, 9, 10]
Then this: [2, 3] + [4, 5] + [6, 7, 8, 9, 10]
Finally, this: [1] + [2, 3, 4, 5, 6, 7, 8, 9, 10]
To build this: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
在执行此操作时,它会记住用户在其中放置了哪些销钉以及在其中放置了哪些销钉。如果未对输入进行排序,则此策略将很难使用。
排序变得更容易的另一件事是数字10的检测。由于Brainfuck本身处理的是单个字节,而不是“数字”,所以这可能会很麻烦,但是排序后的输入使我更容易处理用。原因与我在程序中存储数据的方式有关。我一次输入一个字符,然后从结果中减去32。如果此后该单元格不为零,则我向前移动4个单元格。在重复之前。这意味着我每4个单元获得一个非空格字节的输入,并且我将引脚有效地存储为它们的数字+16。但是,10键入两个字节,因此我必须对其进行特殊处理。如果未对输入进行排序,则必须仔细检查引脚,但是由于已对引脚进行排序,因此如果出现,它将始终是最后一个引脚。我检查(input的最后一个字节+ 1)==(input的第二个最后一个字节),如果是,则必须为10。我摆脱了最后一个字节,并将第二个最后一个设置为系统所理解的“ 10”。那些角色'1'
并且'0'
不能放在单个字节中,但是数字26肯定可以!
我想使用一些技巧来使某件事完全起作用,这是我使用这种语言的最喜欢的部分。:)
如果您对这个程序的更详细的工作方式感兴趣,可以查看该程序及其编写时所用的注释,以确保我记得所有操作。由于没有注释语法,因此即使用Brainfuck编写注释也很困难。取而代之的是,除其中的所有字符外,其他所有字符<[+.,-]>
都是禁忌。通过不小心包含.
或,
在您的注释中引入错误很容易!这就是为什么语法如此古怪而分号无处不在的原因。
编辑:作为这是多么容易搞砸的示例:我在其中之一中使用了“非空格”!当我从源代码中剥离所有非bf字符时,我曾经执行过的程序保存在-
。幸运的是它没有破坏任何东西,但是现在我删除了它以节省一个字节。:)
编辑二:自从我碰到这已经有一段时间了,哈哈。在该站点上的另一个令人不安的答案中,我注意到我在注释版本中不小心使用了逗号。由于输入已经用尽,因此将当前单元格设置为0(这取决于实现,但是根据我的经验,这是最常见的行为)。我修复了该错误,但它使我产生了思考。将单元格设置为0的惯用方式是[-]
(大约while (*p) { *p--; }
),这要长两个字节。任何时候读取所有输入后,我都可以使用,
。这为我节省了2个字节,而在这个答案中却节省了12个字节!
one flag at the very left; will be important later
+>>>>
all nonspace bytes of input separated by 3 empty cells; pin number `n` stored with value `n` plus 16
,[>++++[<-------->-]<[>>>>],]<<<<
test if last pin is 10
[>+<<+<+>>-]<[>+<-]+<+<<[>+>-<<-]>[<+>-]>
[
if not: find 10 minus the number it is; put that many placeholder pins (cells with value 1) at the end
,+++++[>+++++<-]>>[<->-]<[>>>>>[>>>>]+[<<<<]<-]>>[<+>-]<<<
]>
[
if so: get rid of '0' byte; convert '1' byte to 26 (10 plus 16)
>>[,<]<<+++++++++<
]<<<
pointer now sitting on the cell with the second greatest pin that was inputted (ie not a placeholder)
;;;;;;;
[
check for flag placed at the very beginning of the program; if present: break
-[+>>-<]>
[
find ((pin to our right) minus 1) minus pin to our left
move all pins left of us 4*(that value) cells and insert placeholder pins
>>[<+<+>>-]<<<<[>+>-<<-]>[<+>-]>[<<[<<<<]>>>>[[<<<<+>>>>-]>>>>]<<<<+>>-]>[>+<-]
]
find first non placeholder pin to our left
there has to be one because we haven't hit the flag yet
<<<[-[+>]+<<<<]>>>>-<<<<<
]>>>>>+
we have now added placeholder pins at the end and in the middle; all that's left is the beginning
subtract 17 from lowest pin and put that many placeholders to the left
++++[>----<-]>->[<+>>+<-]<[<<<[<<<<]+[>>>>]<-]>>[<+>-]
subtract 32 from an empty cell 2 to the left of the lowest pin; will be useful later
<[<<<<]>>>++++[<-------->-]>
placeholder pins have the value 1; real pins have a value somewhere between 17 and 26
normalize it by stepping through and setting every pin with value != 1 to 3 (0's ascii code is 2 higher than period so this will make it easier to print later)
[-[,+++>]+>>>[<<<->>]>]<<<<<[>-]>[>>]>>
start writing 32s across the board; hitting every second cell
that's every pin and the cell 2 to the right of each pin
this is done in such a way that it will only halt if adding 32 to a cell sets it to 0; which is why we subtracted 0 from an empty cell earlier
it will catch us and prevent an infinite loop
+[<++++[<++++++++>-]<]
now write 13 to each pin; this adds up to 46 or 48; which are exactly the ascii values we want
>>[+++++++++++++>>>>]
we happen to have made a 14; turn it into a 10 for a newline
<<<<----
we're so close now; i can taste it
we have a list of 10 pins; each one with the ascii value that needs to be written
we have 32 everywhere because we'll need spaces
we even have a newline
the only problem now is that our list looks like this:
;;;;;;;;;;;;;;;;;;;;;;;;
;;1 2 3 4 5 6 7 8 9 10;;
;;;;;;;;;;;;;;;;;;;;;;;;
and we need to print in this order:
;;;;;;;;;;;;;;;;;;;;;;;;
;;7 8 9 10 4 5 6 2 3 1;;
;;;;;;;;;;;;;;;;;;;;;;;;
it's a pretty simple fix
once we print a pin we obviously don't need to remember it any more
so we simply print the last 4 pins on the list; destroying them on the way
then we print the last 3; which have become the ones we want
then two; then one
<+++[<<+<<[<<+<<]+[>>>>]<<<<-]<<<<[-<<<<]
print pins 7 8 9 10
>[.,>>]
print pins 4 5 6
<<<<<[<<<<]<++++++++++<<.<+<<+<<+<<+<<+<[.,>>]
print pins 3 2
<<<<<[<<]>++++++++++<+<<+<<+<..<+<[.,>>]
print the final pin!! :)
<[<<]<...<<.