可视化包含-排除


11

包含-排除使您可以在知道其他一些值的情况下计算集合之间的某些并集和相交的大小。我不会确切解释,但是您的挑战是在维恩图上可视化包含-排除。

因为我很好,所以您将使用矩形,而不是圆形。

您将获得一个以任何合理格式(由4元组组成的列表,成对的列表,成对的列表等)表示的矩形列表,这些矩形由左上角和右下角的坐标表示。您可以假定所有坐标都是非负数,并且在您的语言的(合理的)数字范围内(请指定小于128的坐标)。您可以选择是左包含或左包含以及右包含或右包含。无论选择哪种格式,都可以假定所有矩形至少为1x1。

然后,您将使用一个非空白字符绘制屏幕上的每个矩形(ASCII画布)k,这是您可以选择的。

但是,每当两个矩形重叠时,重叠区域应使用另一个非空白字符绘制l != k,您也可以选择。

每当3个矩形重叠时,重叠区域应与被绘制k,以及用于奇数矩形的覆盖,k以及偶数,l

背景应为单个空格(0x20)。

测试用例(k = "#", l = "."

0 0 9 9
1 1 10 10
2 2 11 11

#########  
#........# 
#.#######.#
#.#######.#
#.#######.#
#.#######.#
#.#######.#
#.#######.#
#.#######.#
 #........#
  #########

1 1 3 3
2 2 4 4


 ## 
 #.#
  ##

1 1 9 9
2 2 8 8
3 3 7 7


 ########
 #......#
 #.####.#
 #.####.#
 #.####.#
 #.####.#
 #......#
 ########

笔记

  • 0, 0必须存在前导空格和换行符(如果没有最小坐标,则会出现换行符)
  • 任何尾随空格和换行符都被允许在合理范围内(即不要像100000000换行符那样尾随,这很烦人)
  • x轴和y轴可以面对任何一个方向,但您必须保持一致并指定哪个(默认值为x右和y向下)
  • 坐标可以是0、1,或2的索引。

参考质子实施

这是,因此目标是拥有最短的代码。打高尔夫球快乐!


x轴向右延伸,y轴从左上角向下延伸?
user202729

@ user202729在测试用例中,可以(实际上是模棱两可的),但是您可以使用任何一个,只要您保持一致
即可-HyperNeutrino

@dzaima是的。[...]
HyperNeutrino

1
@JoKing是的,我将使输入格式更加灵活。意识到这是一个古老的挑战,而我对挑战写作还不太熟悉
HyperNeutrino

1
@JoKing实际上,我将允许这四个组合中的任何一个。
HyperNeutrino

Answers:


4

6502机器代码例程(C64),57字节

20 44 E5 A0 03 84 FB 20 9B B7 A4 FB 96 22 C6 FB 10 F5 85 FC A6 24 20 F0 E9 A4
25 B1 D1 09 01 49 02 91 D1 C8 C4 23 D0 F3 E8 E4 22 D0 E9 A9 2C C5 FC F0 D0 A5
C6 F0 FC C6 C6 4C 44 E5

这是与位置无关的代码,将其放在RAM中的某个位置,并使用正确的起始地址通过调用它sys

在线演示(起始地址$C000/49152)。

用法: sys<startaddress>,<x1>,<y1>,<x2>,<y2>[,<x1>,<y1>,<x2>,<y2>[,...]]

例: sys49152,0,0,9,9,1,1,10,10,2,2,11,11

在合理的数字范围内:该8位计算机的自然范围为[0-255],程序将接受此范围作为参数。但是C64屏幕只有40列和25行,因此将x值的合理范围限制为[0-40],而y值的合理范围限制为[0-25]。使用其他值将具有不可预测的行为。


评论了拆装清单:

20 44 E5    JSR $E544           ; clear screen
 .mainloop:
A0 03       LDY #$03            ; index for reading coordinates
84 FB       STY $FB
 .inputrect:
20 9B B7    JSR $B79B           ; read 8bit value from parameter
A4 FB       LDY $FB
96 22       STX $22,Y           ; and store to $22-$25
C6 FB       DEC $FB
10 F5       BPL .inputrect      ; parameter reading loop
85 FC       STA $FC             ; store last character
A6 24       LDX $24             ; load y1
 .rowloop:
20 F0 E9    JSR $E9F0           ; get pointer to screen row in $d1/$d2
A4 25       LDY $25             ; load x1
 .colloop:
B1 D1       LDA ($D1),Y         ; load character at screen position
09 01       ORA #$01            ; set bit 0 ( -> '#')
49 02       EOR #$02            ; toggle bit 1 (toggle between '#' and '!' )
91 D1       STA ($D1),Y         ; store character at screen position
C8          INY                 ; next x
C4 23       CPY $23             ; equals x2?
D0 F3       BNE .colloop        ; no -> repeat
E8          INX                 ; next y
E4 22       CPX $22             ; equals y2?
D0 E9       BNE .rowloop        ; no -> repeat
A9 2C       LDA #$2C            ; load ','
C5 FC       CMP $FC             ; compare with last character from parsing
F0 D0       BEQ .mainloop       ; if ',', repeat reading coordinates
 .waitkey:
A5 C6       LDA $C6             ; load input buffer size
F0 FC       BEQ .waitkey        ; and repeat until non-empty
C6 C6       DEC $C6             ; set back to empty
4C 44 E5    JMP $E544           ; clear screen

3

Python 2中218个 192 189 185 158 154 147字节

def f(l):_,_,a,b=map(range,map(max,zip(*l)));print'\n'.join(''.join((' '+'#.'*len(l))[sum((x<=i<X)*(y<=j<Y)for x,y,X,Y in l)]for i in a)for j in b)

在线尝试!


3

木炭,40字节

WS«≔I⪪ι ιF…§ι⁰§ι²«Jκ§ι¹UMKD⁻§ι³§ι¹↓§10Σλ

在线尝试!链接是详细版本的代码。一旦仅@ASCII修复了Charcoal中的错误,将缩短6个字节。将输入作为以空格分隔的坐标列表的换行符终止列表。说明:

WS«

循环输入的每一行,直到到达空白行。

≔I⪪ι ι

将该行拆分为一个坐标列表。

F…§ι⁰§ι²«

循环所有X坐标。

Jκ§ι¹

跳到该列的顶部。

UM

映射每个...

KD⁻§ι³§ι¹↓

...该列中的所有单元格...

§10Σλ

...新值是0它们是否包含1,否则1。编辑:编写此代码后不久,木炭更改了行为,¬以便I¬Σλ在此处节省1个字节。


:| 我把tio.run/
仅ASCII

解决方法中,@ ASCII纯错误-我可以打印一个,\n我猜...
尼尔(Neil)

2

Python 2,181字节

l=input()
_,_,x,y=map(max,zip(*l))
m=eval(`[[32]*x]*y`)
for v,w,x,y in l:
 for i in range(v,x):
	for j in range(w,y):
	 m[i][j]=max(m[i][j]^1,34)
for n in m:print''.join(map(chr,n))

在线尝试!



2

[R 196个 189字节

m=matrix
x=m(scan(file("stdin")),4)
y=m(0,max(x[3,]),max(x[4,]))
n=ncol(x)
while(n){z=x[,n]  
i=z[1]:z[3]
j=z[2]:z[4]
y[i,j]=y[i,j]+1
n=n-1}
i=!y
y=y%%2+1
y[i]=' '
cat(rbind(y,'\n'),sep='')

在线尝试!

代码以stdin的形式读取输入,以x1 y1 x2 y2元组的形式排列,其中x是列,y是行。我将1和2用于重叠级别,其中1表示偶数级别。

感谢user2390246,节省了7个字节。


1
打高尔夫球的一些想法:1.是否需要变换矩阵x?2.使用nrow(或ncol如果不进行转换)而不是dim(x)[1]3.无需定义,i=y>0因为您只使用一次。
user2390246

4.将矩阵初始化为-1,然后仅使用y=y%%2y[y<0]=" "
user2390246

谢谢。我包括了建议1和2。建议3和4不起作用,因为:i = y> 0用于在应用模数之前存储电平,并且模数不应保留符号。但是,这给了我一个使用隐式R约定(0 = FALSE)并节省两个额外字节的想法。:)
NofP

2

Raku,54个字节

{my@a;{@a[$^a..$^b;$^c..$^d]X+^=1}for $_;@a >>~|>>' '}

在线尝试!

将输入作为平面坐标列表作为包含坐标,即x1,y1,x2,y2,x1,y1,x2,y2...,将输出作为具有kbe 1lbeing 的字符列表作为列表0

说明:

{                                                    }  # Anonymous codeblock
 my@a;    # Declare an array
      {                          }for $_;    # Loop over the input
       @a[                 ]   # Indexing into @a
          $^a..$^b             # The range of rows
                  ;$^c..$^d    # And the range of columns for each
                            X        # And for each cell
                             +^=1    # Set it to itself bitwise XOR'd with 1
                         # Cells not yet accessed are numerically zero
                                         @a >>~|>>' '   # Stringwise OR each cell with a space
                         # Cells not yet accessed are stringily empty         

1

果冻,43个字节

+µ>2Ḥạ
ạ1ẋ$0ẋ⁸¤;µ/€«þ/µ€z0z€0Z€Zz€0Z€ç"/o⁶Y

在线尝试!

说明

+µ>2Ḥạ                                Helper Link; combines final rectangles (0 is blank, 1 is covered, 2 is uncovered)
+                                     add the two values
 µ                                    (with the sum...)
  >2                                  check if it's greater than two
    Ḥ                                 double the result (2 if it's 3 or 4, 0 if it's 0, 1, or 2)
     ạ                                absolute difference (0 should take whatever the other thing's value is, 1+1 and 2+2 should give 2, 1+2 and 2+1 should give 1)
ạ1ẋ$0ẋ⁸¤;µ/€«þ/µ€z0z€0Z€Zz€0Z€ç"/o⁶Y  Main Link
               µ€                     For each rectangle stored as [[x1, x2], [y1, y2]]
         µ/€                          For each of [a, b] = [x1, x2] and [y1, y2], reduce it by (in other words, use a dyad on a size-2 list)
 1ẋ$                                  repeat [1]            times
ạ                                                abs(a - b)
        ;                             and append to
    0ẋ ¤                              [0] repeated   times
      ⁸                                            a
            «þ/                       and reduce by minimum outer product table (take the outer product table, by minimum, of the x results and the y results)
                                      [NOTE] At this point, we have a list of matrices with 0s as blanks and 1 as covered
                 z0z€0Z€Zz€0Z€        Make all of the matrices the same size:
                 z0                   zip, fill with 0 (all matrices are the same length, but not width, and now are lists of row-wise lists of rows)
                   z€0                zip each, fill with 0 (all rows are the same length within their row-wise lists, and are now lists of row-wise lists of columns)
                      Z€              zip each (flip rows back to lists of row-lists of rows)
                        Z             zip (flip back to matrices); however, if a matrix is smaller on both axes, its rows will not be the same length
                         z€0          zip each, fill with 0 (all rows in each matrix are the same length and the value is now a list of transposed matrices)
                            Z€        zip each (the value is now a list of matrices, all the same length, filled with 0 (empty space))
                              ç"/     reduce by (vectorized) the relation in the Helper Link (to combine all of the final values)
                                 o⁶   logical OR with " "; replace 0s with spaces
                                   Y  join with newlines (formatting)
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.