การสั่งซื้อสายที่ทับซ้อนกัน


17

(แรงบันดาลใจขณะวาดภาพบนกระดานลบแบบแห้ง)

ท้าทาย:

รับสตริงอินพุตที่มีอักขระที่แสดงสีที่แตกต่างกันของเครื่องหมายลบแบบแห้งบนกระดานสีขาวให้เรียงลำดับตามลำดับจากช่วงแรกถึงสุดท้าย

การป้อนข้อมูล:

สตริงที่มีสีมาร์กเกอร์ลบแบบแห้งซึ่งแสดงด้วยตัวอักษรตามตัวอักษร (ส่วนบนนั้นแตกต่างจากตัวพิมพ์เล็กคุณสามารถแทนที่อักขระใด ๆ ที่ใช้ในตัวอย่างของฉันตราบใดที่แต่ละสีมีตัวอักษรที่แตกต่างกัน) ไวท์บอร์ดส่วนที่เหลือจะเป็น white-space จะมีเพียงหนึ่งบรรทัดของแต่ละสีต่อบอร์ด จะไม่มีอินพุตที่ทุกบรรทัดทับซ้อนกัน (ดูกรณีทดสอบ4) เส้นทุกเส้นจะตรงและเป็นแนวนอนหรือแนวตั้ง

เอาท์พุท:

ลำดับที่ลากเส้นบนกระดานจากอันแรกที่ลากไปถึงอันสุดท้าย หากมีวิธีแก้ไขปัญหาหลายประการสำหรับอินพุตใด ๆ คุณสามารถส่งออกหนึ่งในโซลูชันเหล่านั้นได้ คุณสามารถจัดรูปแบบผลลัพธ์ได้ตามที่คุณต้องการ: อักขระสตริงเดียวหรือคั่นด้วยช่องว่างบรรทัดใหม่ ฯลฯ ตราบใดที่อักขระที่ใช้ตรงกับที่ใช้ในการป้อนข้อมูลของคุณ

กรณีทดสอบ:

อินพุต 1:

  R
  R
BBRBB
  R

เอาท์พุท 1:

BR

อินพุต 2:

    GY
    GY
RRRRGYRRR
    GY
    GY
BBBBBBBB
    GY
    GY

เอาท์พุท 2:

RGYB // or RYGB

อินพุต 3:

    R    P
    R    P
AAAARAAAAPA
    R    P
    R    P
GGGGRGGG P
    R

เอาท์พุท 3:

AGPR // or APGR

อินพุต 4:

 O Y
RRRYR
 O Y
GOGGG
 O Y

เอาท์พุท 4:

// Undefined, does not need to be handled by your program

อินพุต 5:

YYYB
   B
   B

ผลลัพธ์ 5:

// YB or BY

กฎ:

นี่คือดังนั้นรหัสที่สั้นที่สุดในหน่วยไบต์ชนะ


@StewieGriffin สามารถมีอักขระ ASCII ที่พิมพ์ได้ (33-127) มากที่สุด ฉันใช้สีปกติในกรณีทดสอบของฉัน แต่เนื่องจากพวกเขาเป็นตัวละครพวกเขาไม่ตรงกับสีจริง (แดงเขียวเหลือง ฯลฯ ) พวกเขาเพียงแค่แสดงสีที่ไม่ซ้ำกัน (R เป็นสีที่แตกต่างจาก G และ Y) .
Yodle

1
เอ๊ะใช่แล้วฉันจะพูดแค่ตัวอักษรตามตัวอักษรเท่านั้น (65-90 และ 97-122)
Yodle

เส้นทั้งหมดจะเป็นแนวนอนหรือแนวตั้งใช่ไหม คุณควรระบุไว้ในคำถาม

@ ais523 ใช่แก้ไขมันได้แล้ว
Yodle

เราขอสมมติว่าอินพุตนั้นถูกเสริมด้วยช่องว่างสี่เหลี่ยมหรือไม่?
PurkkaKoodari

คำตอบ:


5

Perl, 103 + 2 = 105 ไบต์

s/$/$"x y===c/gem;$a=$_;$_.=$"while$a=~s/^./!($_.=$&)/gem;s/$1/-/g,$b="$&$b"while/\s(\w)(\1|-)+ /;say$b

ทำงานด้วย-n0(โทษ 2 ไบต์)

คำอธิบาย:

# -n0: read entire input into `$_` at start of program
# (technically speaking it reads to the first NUL byte, but there aren't any)

# We want to be able to extract columns from the input, so we need to add spaces
# to the ends of each line such that each column is complete. Adding too many
# space is OK, so to ensure we have enough, we add a number of spaces equal to the
# length of the input.
s/$/             # At the end of {something},
$" x             # append a number of spaces ($" is a space by default)
y===c            # obtained by counting the characters in $_
/gem;            # where {something} is each (g) line (m)

$a = $_;         # store a copy of the transformed input in $a

# The next step is to create a transposition of the input. To do that, we
# repeatedly extract the first column of $a and append it to $_. This will lead to
# a bunch of junk whitespace at the end of $_ (of varying lengths, because once a
# line is empty it's omitted from the extracted column), but we're OK with that.
# To transpose properly, we'd want to place newlines between the extracted
# columns; however, it happens that the rest of the program treats space the same
# way it would newline, and separating via spaces is shorter, so we do that.

while (          # keep looping as long as there are matches
  $a =~ s/^./    # replace the first character of {something related to $a}
  !(             # with the null string (NOT of something truthy)
    $_.=$&)      # but append that character ($&) to $_
  /gem) {        # {something} is each (g) line (m) of $a
  $_.=$"         # append a space ($", equivalent to newline here) to $_
}

# Finally, we repeatedly replace every character in the topmost line with the -
# character (treating a line as continuous through the - character but not through
# other characters), thus finding the lines from top to bottom. Because we
# appended the transpose of $_ to $_ above, each line appears twice: once
# horizontally, once vertically. We find only the horizontal copy, but replace
# both with hyphens.
# (Note: I rewrote the regex into a bit more readable of a form in this ungolfed
# version, because the original version wouldn't allow me room to write comments
# inside it. The two should be equivalent; I tested the golfed version.)
while (          # keep looping as long as there are matches
  /\s(\w)        # match a space or newline, $1 (a letter/digit/underscore),
    (\1|-)+      # any positive number of $1s and hyphens,
    \ /x) {      # and a space
  s/$1/-/g,      # changes all $1s to spaces; set $& to $1, $1 becomes invalid
  $b = "$&$b"    # prepend $& to $b
}

# We need to output the lines from first (i.e. bottom) to last (i.e. top).
# We found them in the opposite order, but reversed them via prepending
# (not appending) the partial results to $b.
say $b           # output $b

ความบอบบางเล็กน้อยที่นี่มาพร้อมกับอินพุตเช่นนี้:

   เอบีซี
DDDDDDDDD
   เอบีซี
   เอบีซี
   เอบีซี

ดูบรรทัดที่สี่ที่นี่ หากลำดับการเขียนเป็น BACBD อาจมีเส้นแนวนอนอย่างแท้จริงBโดยไม่ละเมิดสมมติฐานใด ๆ ของปัญหา (นอกเหนือจากนั้นมีเพียงหนึ่งบรรทัดของแต่ละสีซึ่งเป็นสิ่งที่เราไม่ได้ตรวจสอบ) เพื่อให้ได้สิ่งนี้เรามั่นใจว่าใน regex สุดท้ายที่แต่ละบรรทัดเริ่มต้นด้วยตัวอักษร (หรือตัวเลขหรือขีดเส้นใต้ แต่เป็นไปไม่ได้) และพึ่งพาความจริงที่ว่าเส้นคู่ขนานจะพบจากซ้ายไปขวาและบนสุด -to-bottom (เพราะ regex จะค้นหาคู่แรกภายในสตริง) ด้วยเหตุนี้อักขระตัวแรกของแต่ละบรรทัดที่ไม่ชัดเจนในที่นี้จะถูกเขียนทับก่อนที่เส้นจะถูกมองว่าเป็นการจับคู่และป้องกันการจับคู่ regex


น่าประทับใจมาก ... ทำได้ดีมาก! (ฉันอยู่ที่ 161 bytes ด้วยperl -n0E '/.*/;for$i(/(\S)(?=(?:(?:.{@{+}})?(?:\1| ))*(?!.*\1))/gs){/.*/;unless(/$i+[^$i\s]+$i/||/$i(.{@{+}}[^$i ])+.{@{+}}$i/s){$r="$i$r";s/$i/ /g;last}}/\S/?redo:say$r'(ซึ่งต้องใช้บรรทัดอินพุตที่จะถูกทับด้วยช่องว่างให้มีความยาวเท่ากันทั้งหมด))
Dada

2

Python 2, 199 ไบต์

l=input()
w=len(l[0])
j="".join(l)
c=set(j)-{" "}
def f(s):
 for h in s:
  i=j.index(h);I=j.rindex(h);o=f(s-{h})
  if{0}>c-s&set(j[i:I:w**(i+w<=I)])and`o`>"Z":return[h]+o
 if{0}>s:return[]
print f(c)

สิ่งนี้จบลงนานกว่าที่ฉันคิดไว้มาก นอกเหนือจากที่rindexฉันเห็นนี่เป็นโปรแกรมที่ดีมากในการแปลเป็น Pyth

ใช้ในรายการของบรรทัดและแสดงรายการอักขระ รหัสสร้างการเรียงสับเปลี่ยนแบบวนซ้ำตรวจสอบให้แน่ใจว่าไม่มีการวาดเส้นใด ๆ ที่ควรวาดที่ด้านบนของบรรทัดปัจจุบัน

รหัสละเมิดสิทธิมนุษยชนมากคุณสมบัติงูใหญ่เช่นการซักwถึงพลังของบูลที่ทดสอบเซตว่างโดยการตรวจสอบย่อยของ{0}(ตั้งแต่ชุดของฉันไม่เคยมีสายที่ไม่ได้) และชื่นชอบความแตกต่างในรายการใด ๆ จากNoneโดยการตรวจสอบถ้า Zเป็นตัวแทนที่มีค่ามากกว่า

รหัสอธิบาย

lines = input()
width = len(lines[0])
joined = "".join(lines)
characters = set(joined) - {" "} # find unique characters except space in input

def solve(chars_left): # returns a solution for the given set of lines
    for try_char in chars_left: # try all lines left

        start_index = joined.index(try_char) # find start position of this line
        end_index = joined.rindex(try_char) # and end position

        step = width ** (start_index + width <= end_index) # take every width'th character if start
                                                           # and end indices differ by at least width

        used_chars = characters - chars_left # find all drawn lines

        line_chars = set(joined[start_index:end_index:step]) # find the characters inside the current line
        missed_chars = used_chars & line_chars # find all lines that are already drawn but should be on
                                               # top of this line

        solution = solve(chars_left - {try_char}) # find solutions if this line was drawn now

        if {0} > missed_chars and `solution` > "Z": # there should be no missed lines and a solution
                                                    # should exist
            return [try_char] + solution # solution found, prepend current character and return

    if {0} > chars_left: # if no lines are left
        return [] # solution found

print solve(characters) # solve for all lines
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.