使用线条图字符绘制数独板


20

这是代码高尔夫。对于这个挑战,我将接受一个方法(您不需要完整的程序),但是方法签名确实计入字节数,并且我想查看完整的签名(不是lamdba)。该方法的输入是具有81个元素的整数数组。该方法的输出/返回值是一个字符串,该字符串将数组表示为ASCII数独板。

如果您使用的是深奥的语言或某种绝对没有方法的语言,则可以进行调整,但是如果该语言完全支持此功能,我希望看到实际上可以将某种东西插入“真正的”非高尔夫程序中,即使方法身体本身​​就是一种痛苦。该要求不是要阻止Jelly或05AB1E之类的语言,而是要使Java之类的语言更容易构建对该平台有意义的内容。

对于输入,整数值1-9应该具有明显的含义。应始终将0解释为空白单元格。您也可以将1-9范围以外的任何内容解释为空白单元格,但这不是必需的。从数组到拼图的位置从左上角开始,并从左到右填充每一行,然后再移动到下一行。

对于盒子,我希望在外部和每个3x3区域之间使用双线,在其他单元格之间使用单线。这些字符应使用线描字符绘制(如果您的I / O格式将字符串表示为字节序列而不是字符序列,则应以众所周知的编码(例如UTF-8或代码页347)表示它们。

对于这个挑战,我不是要您生成数独难题。这是功能的输入。我不是要你解决难题。我只是想让您产生一个字符串来“绘制”您所得到的(尽可能少的字节)。

输入示例:

数组的值:

{ 8, 5, 0, 0, 0, 2, 4, 0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 9, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 2, 3, 0, 5, 0, 0, 0, 9, 0, 0 ,0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 7, 0, 0, 1, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 0, 4, 0}

这些值可以使用您的语言所固有的任何机制:int [],ArrayList,序列,元组,数字串等,只要您在每个单元格的输入中都有一个值即可(没有映射仅将填充的单元格映射到位置)。请记住,提供了输入...这不是字节数的一部分。但是输入可能代表任何数独难题,并​​且难题甚至可能没有有效的解决方案。您必须假设拼图可打印的。例如,您将不会得到82个元素。

您还可以假定使用合理的固定宽度字体。

对应输出:

╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗
║8│5│║││2║4││║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║7│2│║││║│││9║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║││4║││║││║
╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣
║││║1││7║││2║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║3││5║││║9││║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║│4│║││║││║
╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣
║││║│8│║│7│║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║│1│7║││║││║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║││║│3│6║│4│║
╚═══╧═══╧═══牛皮═══╧═══╧═══牛皮═══╧═══╧═══╝

4
您确定方法部分吗?对于许多语言(即没有方法的语言),这没有意义。
Cyoce

1
对于没有方法的语言,您可以进行调整。但是,如果这样做的话,我正在寻找对插入“真实”程序实际上可能有用的东西。我将其添加到问题中。
Joel Coehoorn

2
不允许使用lambda是什么原因?当然可以将这些程序以及命名的函数/方法插入“真实”程序中
朱利安·沃尔夫

2
挑剔但很重要:没有“ ASCII盒子绘图字符”之类的东西。ASCII涵盖代码0到127,它们都不是箱形图字符。近年来,Unicode是标准,但是它有几种不同的编码:UTF-8,UTF-16等,每个boxdrawing字符都使用超过1个字节。较旧的编码,例如Codepage 437,支持单字节框图字符。当您使用ASCII范围以外的字符时,应指定哪些编码有效。en.wikipedia.org/wiki/Box-drawing_character en.wikipedia.org/wiki/Unicode
Level River St

2
“方法”可能应该是“命名函数”,以获取与非面向对象语言中的方法最接近的等效方法。(例如,C是一种广泛使用的语言,它没有方法,但确实具有命名函数。)在我所知的大多数语言中,这些方法都具有方法,它们等同于命名函数。(我知道的最著名的异常是C ++,在这种情况下,使用命名函数比使用方法更容易实现此任务,因为目前尚不清楚将方法与哪种对象相关联。)

Answers:


9

Python 3,232字节

感谢那些帮助打高尔夫球的人。

加密内的加密...

q=lambda x,y:x+y+x+y+x
r=lambda a,b,c,d,e:a+q(q(b*3,c),d)+e+"\n"
print(((r(*"╔═╤╦╗")+q(q("║ %d │ %d │ %d "*3+"║\n",r(*"╟─┼╫╢")),r(*"╠═╪╬╣"))+r(*"╚═╧╩╝"))%eval(input())).replace(*"0 "))

在线尝试!

要打高尔夫球。


我怎么没有注意到这一点……这就像我使用Python 2的全部原因,但是谢谢。
Leaky Nun

1
实际上,您最好使用Python 3,因为那样您就不需要第一行了。
暴民埃里克(Erik the Outgolfer)

1
您可以在最后一行删除更多的括号:tio
Conor O'Brien

删除f的定义并将i定义为i=["╔"+(g+"╦")*2+g+"╗"]+d+2*(["╠"+(e+"╬")*2+e+"╣"]+d)+["╚"+(h+"╩")*2+h+"╝"]保存4个字节
Officialaimm

7

C(GCC) 398个 395 291字节

通过反向处理字符串节省了3个字节,感谢Leaky Nun节省了104(!)个字节。

#include<locale.h>
#define q(x,y) x y x y x
#define D L"╝"q(q("═══","╧"),"╩")"╚"q(q("\n║"q(q(" & ","│"),"║")"║","\n╢"q(q("───","┼"),"╫")"╟"),"\n╣"q(q("═══","╪"),"╬")"╠")"\n╗"q(q("═══","╤"),"╦")"╔"
i;f(int*t){setlocale(LC_ALL,"");for(i=721;i--;)wprintf(L"%lc",D[i]%19?D[i]:*t++?48+t[-1]:32);}

在线尝试!

C(gcc),395字节

我将其保留在此处,以便更清楚地了解程序的工作方式。

#include<locale.h>
#define A L"\n╢───┼───┼───╫───┼───┼───╫───┼───┼───╟"
#define B L"\n║ & │ & │ & ║ & │ & │ & ║ & │ & │ & ║"
#define C L"\n╣═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╠"
#define E B A B A B
#define D L"╝═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╚"E C E C E L"\n╗═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╔"
i;f(int*t){setlocale(LC_ALL,"");for(i=721;i--;)wprintf(L"%lc",D[i]%19?D[i]:*t++?48+t[-1]:32);}

在线尝试!

在C中使用unicode成本很高。如int*链接和规范中所示,将输入作为输入。

我将看看是否可以使用一些数字魔术来保存字节,而不是对字符串进行硬编码。



@LeakyNun Huh,谢谢!根据TIO,它为291个字节
Conor O'Brien

TIO正在使用SBCS进行计数。
Leaky Nun

6

PHP,297字节

<?for(;$l<19;$l++)echo$l&1?strtr(vsprintf(str_pad("",67,"║ %d │ %d │ %d "),array_slice($_GET,9*($l/2^0)-9,9)),0," "):str_pad([╔,╟,╠,╚][$b=$l?$l<18?$l%6<1?2:1:3:0],108,strtr("11101110111".[╦,╫,╬,╩][$b],[[╤,═],[┼,─],[╪,═],[╧,═]][$b])).[╗,╢,╣,╝][$b],"
";

在线尝试!

展开式

for(;$l<19;$l++)  # loop thrpugh lines
  echo$l&1 # Output
    ?strtr(
        vsprintf(str_pad("",67,"║ %d │ %d │ %d ") # formated string for lines with numbers
        ,array_slice($_GET,9*($l/2^0)-9,9)) # nine items of the input array
      ,0," ") #replace zeros with space
    :str_pad([╔,╟,╠,╚][$b=$l?$l<18?$l%6<1?2:1:3:0] # start character non number lines and switch number four cases
      ,108 # fill too 108 bytes
      ,strtr("11101110111".[╦,╫,╬,╩][$b] # with string 
        ,[[╤,═],[┼,─],[╪,═],[╧,═]][$b]))  #replace ones and zero with the two character in array chosed 
    .[╗,╢,╣,╝][$b] # end row with chosen character
  ,"
    "; # end line with new line

两个版本使用的功能

vsprintfstrtrstr_padarray_slicearray_chunk

PHP,313字节

<?$r=($s=str_pad)(╔,108,($t=strtr)(($p=11101110111).╦,[╤,═])).╗;foreach(array_chunk($_GET,9)as$v)$r.=$t(vsprintf($s("
",68,"║ %d │ %d │ %d "),$v),0," ").(++$k%9?$k%3?$s("
╟",109,$t($p.╫,[┼,─])).╢:$s("
╠",109,$t($p.╬,[╪,═])).╣:"");echo$r.$s("
╚",109,$t($p.╩,[╧,═])).╝;

在线尝试!


这是如何运作的?
Cyoce

@Cyoce我增加了一个解释为我的新版本
约尔格Hülsermann

5

T-SQL,445个 437字节(381个字符)

DECLARE @r INT=0,@ NVARCHAR(999)=N'╔=╤=╤=╦=╤=╤=╦=╤=╤=╗P'p:SELECT @+=FORMAT(CAST(SUBSTRING(a,@r*9+1,9)AS INT),N'║ 0 │ 0 │ 0 ║ 0 │ 0 │ 0 ║ 0 │ 0 │ 0 ║P')FROM t
SET @r+=1IF @r=9SET @+=N'╚=╧=╧=╩=╧=╧=╩=╧=╧=╝P'ELSE IF @r%3=0SET @+=N'╠=╪=╪=╬=╪=╪=╬=╪=╪=╣P'ELSE SET @+=N'╟-┼-┼-╫-┼-┼-╫-┼-┼-╢P'IF @r<9GOTO p
PRINT REPLACE(REPLACE(REPLACE(REPLACE(@,'=',N'═══'),'-',N'───'),'0',' '),'P',CHAR(13))

根据批准的方法,通过存储在预先存在的表t的a列中的一串数字进行输入。

格式和说明

DECLARE @r INT=0, @ NVARCHAR(999)= N'╔=╤=╤=╦=╤=╤=╦=╤=╤=╗P'
p:
    SELECT @+= FORMAT(CAST(SUBSTRING(a, @r*9+1, 9) AS INT),
        N'║ 0 │ 0 │ 0 ║ 0 │ 0 │ 0 ║ 0 │ 0 │ 0 ║P') FROM t
    SET @r+=1
    IF @r=9 SET @+= N'╚=╧=╧=╩=╧=╧=╩=╧=╧=╝P'
    ELSE IF @r%3=0 SET @+= N'╠=╪=╪=╬=╪=╪=╬=╪=╪=╣P'
    ELSE SET @+= N'╟-┼-┼-╫-┼-┼-╫-┼-┼-╢P'
IF @r<9 GOTO p
PRINT REPLACE(REPLACE(REPLACE(REPLACE(@, '=',N'═══'), '-',N'───'), '0',' '), 'P',CHAR(13))

在循环的第一行中,我从预先存在的表t的a列中获取输入字符串的下9个数字。

我将该数字字符串转换为整数,然后使用.Net FORMAT函数通过自定义文本模板显示它们'║ 0 │ 0 │ 0 ║ 0 │ 0 │ 0 ║ 0 │ 0 │ 0 ║P'

之后,我只需附加适当的分隔线,并在输出之前进行一些节省字节的替换。

输出显示在结果窗格中:

╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗
║ 8 │ 5 │   ║   │   │ 2 ║ 4 │   │   ║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║ 7 │ 2 │   ║   │   │   ║   │   │ 9 ║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║   │   │ 4 ║   │   │   ║   │   │   ║
╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣
║   │   │   ║ 1 │   │ 7 ║   │   │ 2 ║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║ 3 │   │ 5 ║   │   │   ║ 9 │   │   ║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║   │ 4 │   ║   │   │   ║   │   │   ║
╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣
║   │   │   ║   │ 8 │   ║   │ 7 │   ║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║   │ 1 │ 7 ║   │   │   ║   │   │   ║
╟───┼───┼───╫───┼───┼───╫───┼───┼───╢
║   │   │   ║   │ 3 │ 6 ║   │ 4 │   ║
╚═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╝

以前,我还用其他一些图形字符替换了其他字符,但最终还是没有为我节省字节。

编辑1@r从零开始而不是1,并删除了一些不必要的空格,从而节省了8个字节。


4

视网膜196167字节

.{27}
¶N#=XZ#Q¶|$&
\d{9}\B
$&¶M#─┼Y#P¶|
\d{3}
 $& |
\B\d
 │ $&
^¶.*
B#=RT#E
$
¶H#=UW#K
+`#([^#¶]+)([^#¶])#
#$1#$2#$1#$2#$1#
#(.)#
$1$1$1
T`0=|#L` ═-╬

在线尝试!将输入作为长度为81的字符串。说明:由于箱形图字符花费3个字节,因此unicode代码点═-╬在代码中使用表示=|#A-Z(并非使用所有字符,但坚持使用范围可以节省字节)。此外,使用#符号将行压缩:a#bcd#e扩展为abbbcbbbcbbbdbbbcbbbcbbbdbbbcbbbcbbbe

.{27}
¶N#=XZ#Q¶|$&

╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣每三行插入一次,在每27 行的开头添加一个。

\d{9}\B
$&¶M#─┼Y#P¶|

╟───┼───┼───╫───┼───┼───╫───┼───┼───╢在其他行之间插入,在这些行的开头加s。

\d{3}
 $& |

每三位数插入s。所有现在全被插入。

\B\d
 │ $&

|在所有剩余的数字对之间插入。(此字符是实际的方框图字符,而不是管道。不幸的是,这些字符的─│┼代码彼此之间以及与双框字符之间的距离太远,因此在使用占位符时值得使用。)

^¶.*
B#=RT#E

将第一行更改为╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗(与不将第一行添加到第一位相比,这节省了一个字节)。

$
¶H#=UW#K

╚═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╝在最后一行之后添加。

+`#([^#¶]+)([^#¶])#
#$1#$2#$1#$2#$1#

先扩展a#bcd#ea#bc#d#bc#d#bc#e,然后扩展到a#b#c#b#c#b#d#b#c#b#c#b#d#b#c#b#c#b#e

#(.)#
$1$1$1

更改#b#bbb。这样就完成了减压。

T`0=|#L` ═-╬

删除所有零条目,并用方框图字符替换占位符。


您忘记了用0空格代替。
Leaky Nun

每行周围还有双线,而不仅仅是边框和3x3区域。
Joel Coehoorn

@JoelCoehoorn修复,对不起。
尼尔

@LeakyNun谢谢,我忽略了这一点。(此外,我想避免以空格结尾或仅包含空格的行,这就是为什么我要寻找另一种节省2字节的原因。)
Neil

3

SOGL V0.12174个 172 164 160 158 字节

«ž#>]Wž²6√±_ΕΨ╥╬]v←ē⅓ZΗ⌡z∫◄‽q   §↑╗∑Ολ[Μ↕z↓/∆Yn⁄:Ο║χ≥¾▓g*≈]═+π℮─6⁽SE/⁷,0+►Ƨ⌡u\^⁄-▼0cΦ“╤─┼╬│║═╔╗╚╝”Φ⅜nΡ¡ΞΨīŗ(`½│uģ“ ╬ζ─{ζ} 6Δ¹∑A'⁄─{IaW}¹∑#¶ŗ3 ¶ŗ ”+Ƨøp+!!┌d0@ŗčŗ

太长的解释:

...“                          push a big base-43 encoded number; will be used later. It's pushed here to save a byte on a quote
    ...”                      push "╤─┼╬│║═╔╗╚╝" - the chars in SOGLs encoding
        ...“                  push 679301851737965572513837476350078477
             ╬                push "╬"
              ζ               convert it to its codepoint (9580)
               ─              convert that number to an array of base-9580 numbers
                {ζ}           convert each number to a character (pushing each on the stack)
                    6Δ        push all ascii chars up to 6 (" !"#$%&'()*+,-./0123456")
                      ¹∑      join all the strings on the stack together ("╤─┼╬│║═╔╗╚╝╦╟╫╢╠╪╣╧╩ !"#$%&'()*+,-./0123456")
                        A     save on variable `A`. Now ontop of the stack is the 1st big number
                         '⁄─  onvert from base 43

{   }                           for each number do
 I                                increase
  aW                              get its position in the variable A
     ¹∑                         join all the strings ontop of the stack (the loop above pushed each char separately)
       #¶ŗ                      replace quote (") characters with newlines
          3 ¶ŗ                  replace 3s with "¶"
               ”+               append "”"
                 Ƨøp+           append "øp"
                     !!         execute the created code as SOGL
                       ┌        push "-"
                        d       push variable d - defaults to string input. In a full program could be set as an input
                         0@ŗ    replace zeroes with spaces
                            č   chop into an array
                             ŗ  replace ["-" with input chopped - so each iteratively]

执行的程序:

───!#
 - $
¶%&¶'(
)╪)╪)+
)╤)╤),
)╧)╧).
┼#
+╬+/
0!╫0!1
,╦,2
.╩.4
5│$║&
#0
═══)
$│$5
║&&%
╠/╬+╣6'*
╟1╫0!╢(
(6
╔2╦,╗6'**╚4╩.╝”øp

除了最后一行以外的都是in the entire program replace occurrences of the last char of this line with the rest of this line。这就是为什么可以将一半的字符设为随机ascii的原因(但要花点时间找出空格,破折号和引号是很有用的)

...”    push the whole sudoku grid
    øp  print nothing (prevents bug that this code would already print and pop the result)

在这里尝试!
在线解释器代码更正确,因为选项卡不适用于SE

-8个字节:暴力替换压缩整个电路板,然后用其代码点替换外来字符(至代码页)。这样做比旧程序要花一个小时的时间...
-4字节:压缩压缩的字符串...
-2字节:使用变量+字符串而不是数组


2

JavaScript(ES6),246个字节/ 198个字符

(n,r=(x,y)=>x+y+x+y+x,q=s=>([u,w,x,y,z]=[...s[0]],u+r(r(w+w+w,x),y)+z+`
`))=>q`╔═╤╦╗`+n.reduce((o,v,i)=>o+"║││"[i++%3]+` ${v||" "} `+(i%9?e:`║
`+(i-27&&i-54?i<81?q`╟─┼╫╢`:e:q`╠═╪╬╣`)),e="")+q`╚═╧╩╝`

输入是一个整数数组。最终使用与以下相同的两个帮助器功能 Leaky Nun的Python answer,因此功不可没。

如果function需要,则为263个字节/ 215个字符

function g(n,r=(x,y)=>x+y+x+y+x,q=s=>([u,w,x,y,z]=[...s[0]],u+r(r(w+w+w,x),y)+z+`
`)){return q`╔═╤╦╗`+n.reduce((o,v,i)=>o+"║││"[i++%3]+` ${v||" "} `+(i%9?e:`║
`+(i-27&&i-54?i<81?q`╟─┼╫╢`:e:q`╠═╪╬╣`)),e="")+q`╚═╧╩╝`}

测试片段

支持输入81个数字(12341, 2, 3, 4[1 2 3 4]等)。最好以整页形式查看。

f=
(n,r=(x,y)=>x+y+x+y+x,q=s=>([u,w,x,y,z]=[...s[0]],u+r(r(w+w+w,x),y)+z+`
`))=>q`╔═╤╦╗`+n.reduce((o,v,i)=>o+"║││"[i++%3]+` ${v||" "} `+(i%9?e:`║
`+(i-27&&i-54?i<81?q`╟─┼╫╢`:e:q`╠═╪╬╣`)),e="")+q`╚═╧╩╝`

onload=I.oninput=_=>O.innerHTML=(m=I.value.match(/\d/g))&&m.length==81?f(m.map(x=>+x)):''
<textarea id=I rows=3 style="width:95%">8, 5, 0, 0, 0, 2, 4, 0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 9, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 2, 3, 0, 5, 0, 0, 0, 9, 0, 0 ,0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 7, 0, 0, 1, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 0, 4, 0</textarea>
<pre id=O>


2

批处理,332字节

@echo off
set/ps=
set s=%s:0= %
call:l É Í Ñ Ë »
set t="Ç Ä Å × ¶"
for %%i in (%t% %t% "Ì Í Ø Î ¹" %t% %t% "Ì Í Ø Î ¹" %t% %t% "È Í Ï Ê ¼")do call:c %%~i
exit/b
:c
set t=º
for %%j in (³ ³ º ³ ³ º ³ ³ º)do call set t=%%t%% %%s:~,1%% %%j&call set s=%%s:~1%%
echo %t%
:l
set t=%2%2%2%3%2%2%2%3%2%2%2
echo %1%t%%4%t%%4%t%%5

需要控制台在CP437中。如果这不是您的默认设置,那么CHCP 437如果您的控制台设置为TrueType字体,则可以使用命令更改它。(如果CP437已经是您的默认代码页,则仅适用于光栅字体。)这是CP437中的代码:

@echo off
set/ps=
set s=%s:0= %
call:l ╔ ═ ╤ ╦ ╗
set t="╟ ─ ┼ ╫ ╢"
for %%i in (%t% %t% "╠ ═ ╪ ╬ ╣" %t% %t% "╠ ═ ╪ ╬ ╣" %t% %t% "╚ ═ ╧ ╩ ╝")do call:c %%~i
exit/b
:c
set t=║
for %%j in (│ │ ║ │ │ ║ │ │ ║)do call set t=%%t%% %%s:~,1%% %%j&call set s=%%s:~1%%
echo %t%
:l
set t=%2%2%2%3%2%2%2%3%2%2%2
echo %1%t%%4%t%%4%t%%5

2

从其他答案中收获的想法:

C#(.NET Core),401字节,349个字符

string s(string x){Func<string,string,string>q=(m,n)=>m+n+m+n+m;var a="╔"+q(q("=","╤"),"╦")+"╗";for(var i=0;i<9;) {a+=int.Parse(x.Substring(i*9,9)).ToString("\n║"+q(q(" 0 ","│"),"║")+"║\n")+(i++<8?(i%3>0?"╟"+q(q("-","┼"),"╫")+"╢":"╠"+q(q("=","╪"),"╬")+"╣"):"╚"+q(q("=","╧"),"╩")+"╝");}return a.Replace("=","═══").Replace("-","───").Replace("0"," ");}

取消高尔夫:

static public string s(string x)
{
    Func<string,string,string>q=(m,n)=>m+n+m+n+m;
    var a="╔"+q(q("=","╤"),"╦")+"╗";
    for (var i=0;i<9;) //once per row
    {
        //parse that row to an int, then spit out a formatted string
        a += int.Parse(x.Substring(i*9,9)).ToString("\n║"+q(q(" 0 ","│"),"║")+"║\n") 
          // as well as a trailing row for the box
          + (i++<8?(i%3>0?"╟"+q(q("-","┼"),"╫")+"╢":"╠"+q(q("=","╪"),"╬")+"╣"):"╚"+q(q("=","╧"),"╩")+"╝");
    }
    //expand placeholder characters before returning
    return a.Replace("=","═══").Replace("-","───").Replace("0"," ");
}

在线尝试!

我的答案:

C#(.NET Core)509430418字节,328个字符

string b(string x){var a="╔=╤=╤=╦=╤=╤=╦=╤=╤=╗\n║";for(int i=0,j=0,k,l,m;j<3;j++)for(k=0;k<3;k++){for(l=0;l<3;l++)for(m=0;m<3;)a+=" "+x[i++]+(m++<2?" │":" ║");a+=i<80?(k<2?"\n╟-┼-┼-╫-┼-┼-╫-┼-┼-╢\n║":"\n╠=╪=╪=╬=╪=╪=╬=╪=╪=╣\n║"):"\n╚=╧=╧=╩=╧=╧=╩=╧=╧=╝";}return a.Replace("=","═══").Replace("-","───").Replace("0"," ");}

取消高尔夫:

public string s(string x)
{
    var a = "╔=╤=╤=╦=╤=╤=╦=╤=╤=╗\n║";
    for (int i=0,j=0,k,l,m;j<3;j++)
    {
        for (k = 0; k < 3;k++)
        {
            for (l = 0; l < 3; l++)
            {
                for (m = 0; m < 3;)
                    a += " " + x[i++] + (m++ < 2 ? " │" : " ║");
            }
            a += i < 80 ? (k < 2 ? "\n╟-┼-┼-╫-┼-┼-╫-┼-┼-╢\n║": "\n╠=╪=╪=╬=╪=╪=╬=╪=╪=╣\n║") 
                        : "\n╚=╧=╧=╩=╧=╧=╩=╧=╧=╝";
        }
    }
    return a.Replace("=", "═══").Replace("-","───").Replace("0"," ");
}

I also looked at using a lambda for the `for` loops here, but it actually cost me one byte (saved 10 bytes per loop, with 41 bytes of overhead).

在线尝试!


您是否需要将每个绘图字符算作两个字节?
BradC

我做到了,现在修复。由于这些字符,我本来打算解决该问题并计算字符而不是字节,但是我想现在为时已晚。
Joel Coehoorn

是的,字节比较难,某些ASCII替换可以节省字节,但不影响字符(甚至不伤害字符)。我正在使用T-SQL,而char vs nchar则有很大的不同。
BradC

1

芯片,3645字节

这不是错字

ooooooZZ-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-).
|`xxxx-x--(x---x---x---x---x-v-x---x---x---x---x---x-.`K-)-K-)-K-)-K-).
|b|`xx-x--(x-v-x---x-v-x---x-x-x---x-v-x---x-v-x---x-x-x---x-v-x---x-.`K-).
|>xd`x-x(v-x-x-x-v-x-x-x-v-x-x-x-v-x-x-x-v-x-x-x-v-x-x-x-v-x-x-x-v-x-x-x-.|
||`--x-x-x(x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x/.
|`--z',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\',/\','
`-. |,< >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.| >.|
*-x-/xZ/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ^/xZ'
Z~' |`'|`' |`' |`' |`' |`' |`' |`' |`' |`' |`' |`' |`' |`' |`' |`' |`','`'
    `)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)-K-)'
=
oooooo).h
`)))--^M^Zh
=
oooooo
|    `(--------------------------------------------------------------------------------------------------------va
KZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ^cg
)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx-xKZvvZ
xxxxxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxxx-Kxxxx}e
)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)x))xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)x))xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)x)b`feac
  c
=
oooooo
,'   `(--------------------------------------------------------------------------------------------------------.cba
KZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ^x^^)v--.
xx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxxx-xK-'f e`.
)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx-x-K-+Z+Z}e
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxK^}b gac
xxxxxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxxx-K^d
=
oooooo
,-'
KZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZtabgfv------.
)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx-xK^^x-Zv-vZ}e
xxxxxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxxxK^---^}cade,]b
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)-K----^-^^~'
,v'
db
=
oooooo
,--' `(--------------------------------------------------------------------------------------------------------v-.,-v-ZZZZZZZZZZZZf
KZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'a{x.df
)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xx)xxKx-xxv+Zc
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)x-KZx+bge
xx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxx)xxxxxxxxxxxx---K\--^c
 a^b
=
oooooo
,---'`(--------------------------------------------------.
KZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'gf
)xxxxx)xxxxx)xxxxx)xxxxx)xxxxx)xxxxx)xxxxx)xxxxx)xxxxx)xx-^KZc
)xxxxx)x)xxx)x)xxx)xxxxx)x)xxx)x)xxx)xxxxx)x)xxx)x)xxx)xxbahKZ\ZZZ
x))xxxxxxxxxxxxxxxx))xxxxxxxxxxxxxxxx))xxxxxxxxxxxxxxxx))-K-eh|fff
 ,--K-v-v-K--v-vK-v---K-----K-----K-----K-----K-----K `--Z-Z--'
A/ab/B/e/C/cd/D/-e/A
*}s

在线尝试!, 有点。TIO版本包含大约三分之一的截止时间(t在4th之后oooooo),因此它应在60秒内终止。完整版本在我的计算机上耗时约1分25秒,而TIO似乎快一半。这也意味着TIO仅显示输出的前7行。

我的初稿重达19758字节,花了我大约8m30s来运行。最终的解决方案是预高尔夫球,仅占用2m07s的时间仅占用5980个字节。

那么,如何运作呢?

这需要一个82字节,81位数字的字符串,后跟一个终止符。\0\n什至另一个数字也可以。(此实现实际上只查看前81个,但至少需要再看一个,因为Chip会在耗尽其输入后终止。如果这是不可接受的,则-z可以使用该标志,该标志将无限\0个字节有效地附加到缩短的TIO代码实际上并没有全部到达81个字节,因此问题出在那儿。

我实现这种方式的方式是,仅查看输入的低4位,因此,从原始的二进制数据到莎士比亚的鲜为人知的作品,任何东西都可以算是数独的“难题”。低4位全为零的任何字符都将显示为空格(特殊情况),所有其他字符映射为123456789:;<=>?。(因此,最后几个不是数字,但在普通数独中,10也不是有效数字)。

对于绘制框的字符,它将产生UTF-8,每个相等于3个字节。

实际执行情况如何?

芯片是一种受集成电路启发的3D语言。它具有导线,逻辑门和存储单元。大多数工作是在2D平面上完成的,但是这些平面可以彼此堆叠。这就是程序的构建方式。

开头的行=是层分隔符。然后,将各层堆叠,顶部和左侧对齐。的o的作为销,从而允许信号通过从一个层到另一个。

这里的每一层都有一个目的,您可以将它们视为功能。第一层控制所有内容;第二层控制所有内容。它依次“调用”其他每个层。这里有一个重复的从左到右的模式。该模式跟踪我们当前正在打印的19行输出中的哪一行。

第二层很小,工作量很小。它0x80为除包含数字的行之外的所有输出行设置位。h是与该0x80位相对应的Chip元素。(字母的低端h通过a定义所有八个输出位。)

第三层是我们真正涉足印刷领域的地方。这一层负责第一行。非高尔夫版本具有八行x“和)”,每个字节的八位中的每一个映射到“ 0”和“ 1”。但是,我们可以利用位中的模式在更少的行中完成相同的任务。

第四层很像第三层。它处理水平双线。

第五层处理最后一行。请注意,它缺少其他层沿顶部的导线。这是因为我们不需要将控制权返回给定序器。相反,我们可以在此处终止执行t

第六层处理水平单线。

第七层是打印数字的位置。九个数字行均被“调用”。在执行过程中,它消耗9字节的输入。


1

JavaScript(ES6),222个字节

为ES6功能使用简短的语法-以utf8编码的174个字符,222个字节(https://mothereff.in/byte-counter)。使用还function ...需要16个字节。

F=v=>[1,...v].map((x,i)=>'│║│'[i%3]+` ${x||' '} `+(i%9?'':`║
${[h,r,s,u,t]=i%27?'─╟╫┼╢':i>80?'═╚╩╧╝':i?'═╠╬╪╣':'═╔╦╤╗',r+(s=(u=(h+=h+h)+u+h+u+h)+s)+s+u+t}
`)).join``.slice(6)

少打高尔夫球

F=v=>{
   // horizontal lines are appended after each 9th element
   // so I need to prepend a dummy first element to draw the top horizontal line
   v = [1, ...v];
   return v.map( (x,i) => 
     '│║│'[i % 3] + ` ${x||' '} ` // left bar and cell value
     + ( i % 9 ? '' // add horizontal line after each 9th element
       // the line drawing characters are chosen according to the value of i
       : `║\n${ [h, r, s, u, t] = 
         i % 27 != 0
         ? '─╟╫┼╢'
         : i > 80 
           ? '═╚╩╧╝' // i==81, bottom row
           : i != 0
             ? '═╠╬╪╣'
             : '═╔╦╤╗', // i==0, top row
         r + (s = (u = (h += h + h) + u + h + u + h) + s) + s + u + t
         }\n`
       )
   ).join``
   .slice(6) // cut the first cell (the dummy element)
}

F=v=>[1,...v].map((x,i)=>'│║│'[i%3]+` ${x||' '} `+(i%9?'':`║
${[h,r,s,u,t]=i%27?'─╟╫┼╢':i>80?'═╚╩╧╝':i?'═╠╬╪╣':'═╔╦╤╗',r+(s=(u=(h+=h+h)+u+h+u+h)+s)+s+u+t}
`)).join``.slice(6)

function go() {
  var i=I.value
  i = i.match(/\d+/g).map(x => +x); // convert strings to numbers
  O.textContent = F(i)
}
#I { width: 90% }
<input id=I value='8 5 0 0 0 2 4 0 0 7 2 0 0 0 0 0 0 9 0 0 4 0 0 0 0 0 0 0 0 0 1 0 7 0 0 2 3 0 5 0 0 0 9 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 8 0 0 7 0 0 1 7 0 0 0 0 0 0 0 0 0 0 3 6 0 4 0'>
<button onclick='go()'>go</button>
<pre id=O></pre>


1

Java(OpenJDK 8),279字节

String f(int[]a){String P="0121213121213121214",R[]={"╔═╤╦╗","║ │║║x","╟─┼╫╢","╠═╪╬╣","╚═╧╩╝"},r="";for(int X:P.getBytes()){for(int x:P.replace("1",R[X-=48].length()>5?"151":"111").getBytes())r+=R[X].charAt(x-48);r+="\n";}for(int i:a)r=r.replaceFirst("x",i>0?""+i:" ");return r;}

在线尝试!

对于字节数,请使用CP-437,Java本身支持 CP-437 作为IBM437(最近的API)或Cp437(较旧的API)。因此,请使用具有此字符集的系统,并将该字符集作为默认字符集。

该代码从Java 5开始兼容,但仅在Java 8上进行了测试。

说明

String f(int[]a){
  String P="0121213121213121214",                         // Both lines and rows are repeated according to this pattern.
         R[]={"╔═╤╦╗","║ │║║x","╟─┼╫╢","╠═╪╬╣","╚═╧╩╝"},  // Characters found on each line.
                                                          //   (note the 'x')
         r="";                                            // The string under construction
  for (int X: P.getBytes()) {                             // For each line,
    for (int x:                                           //  For each character in the pattern,
         P.replace("1",R[X-=48].length()>5?"151":"111")   //    *but* with a cell width of 3,
                                                          //    and with an optional character ('x')
         .getBytes())
      r+=R[X].charAt(x-48);                               //   append the real mapped character
    r+="\n";                                              //  then append a new line
  }
  for(int i:a)                                            // For each number in the input
    r = r.replaceFirst("x",i>0?""+i:" ");                 //  replace the first 'x' with that number.
                                                          //    (or space if zero)
  return r;                                               // Return the constructed string.
}

1

Tcl,599字节(295个字符)

这是一种非常幼稚的方法,但即使没有成为任何赢家,我也必须这样做:

puts ╔═══╤═══╤═══╦═══╤═══╤═══╦═══╤═══╤═══╗
set r ║ 
lmap x $L {if !$x {set x \ }
set r $r\ $x\ [expr [incr i]%3?"│":"║"]
if ![expr $i%9] {puts $r\n[expr $i%27?"╟───┼───┼───╫───┼───┼───╫───┼───┼───╢":$i<72?"╠═══╪═══╪═══╬═══╪═══╪═══╬═══╪═══╪═══╣":"╚═══╧═══╧═══╩═══╧═══╧═══╩═══╧═══╧═══╝"]
set r ║}}

在线尝试!


那是599个UTF-8字节。您应该尝试重用公共框字符以保存字节
dzaima

@dzaima:我知道,我可以做我在键盘
品尝到

@dzaima:这就是为什么我说非常幼稚的方法
sergiol
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.