Tavole impilabili


11

Ho un sacco di schede che devo impilare nel minor spazio possibile. Sfortunatamente, le schede cadono se le impilo più di 10. Ho bisogno di un programma per dirmi come impilare le schede per occupare il minor spazio orizzontale possibile, senza impilare le schede più alte di dieci o avere le schede sospese nello spazio vuoto.

Il tuo compito:

Scrivi un programma o una funzione, che quando viene dato un array contenente le lunghezze delle schede, genera come arte ASCII il modo di impilare le schede per conservare il maggior spazio orizzontale possibile, senza impilare le schede più alte di 10 o avere qualsiasi parte di qualsiasi tavola sospesa nello spazio vuoto. La tua arte ASCII dovrebbe mostrare la configurazione delle schede, con ognuna mostrata usando un carattere diverso. Ci saranno un massimo di 20 schede. Ad esempio, se l'ingresso era [2,2,4,2,2,4,4,4], un possibile output è:

dhh
dgg
dff
dee
abc
abc
abc
abc

che è una configurazione stabile (sebbene ciò cadrà in ~ 0,1 secondi nella vita reale).

Ingresso:

Un array contenente fino a 20 numeri interi, che mostra le lunghezze delle schede.

Produzione:

Arte ASCII che mostra le configurazioni delle schede, come indicato sopra.

Casi test:

Si noti che potrebbero esserci altre soluzioni per i casi di test e che i caratteri mostrati per ciascuna scheda potrebbero essere diversi.

[12,2,2,2,3,4,4,8,8]        -> ffgghhiii
                               ddddeeeeeeee
                               bbbbbbbbcccc
                               aaaaaaaaaaaa

[4,4,4,4,4,4,4,4,4,4,4,4]   -> llll
                               aaaa
                               cfghk
                               cfghk
                               cfghk
                               cfghk
                               debij
                               debij
                               debij
                               debij

[4,4,4,4,4,4,3,3,3,2,2,2,1] -> jjml
                               iiil
                               hhhk
                               gggk
                               ffff
                               eeee
                               dddd
                               cccc
                               bbbb
                               aaaa

punteggio:

Questo è , il punteggio più basso in byte vince

Risposte:


3

Python 3 , 513 512 511 509 499 497 485 465 459 458 444 byte

Il runtime incredibilmente cattivo, finirà ad un certo punto

e,j,c=enumerate,len,range
def f(n,p=[],o=97):
    r,l,m=lambda x:min(b,f(n[:i]+n[i+1:],x,o+1),key=j),chr(o),j(p)
    b=[p,l*(sum(n)*2+m)][n>[]]
    for i,a in e(n):
        for h,d in e(p):
            if a<11-j(d):b=r([p[f]+l*a*(f==h)for f in c(m)])
            if(j(d)<10)*all(j(x)==j(d)for x in p[h:h+a])*(a<=m-h):b=r([p[f]+l*(h<=f<h+a)for f in c(m)])
        if a<11:b=r(p+[l*a])
        b=r(p+[l]*a)
    return["\n".join("".join(9-u<j(x)and x[9-u]or" "for x in b)for u in c(10)),b][o>97]

Provalo online!

Modifica: - 2 -8 byte grazie a @Mr. Modifica Xcoder : -8 byte grazie a @notjagan

Spiegazione

e,j,c=enumerate,len,range      
         # These built-ins are used a lot
def f(n,p=[],o=97):
         # n is the remaining blocks
         # p is the current stack
         # o is the ASCI code for the next letter to use
    r,l,m=lambda x:min(b,f(n[:i]+n[i+1:],x,o+1),key=j),chr(o),j(p)
         # r is the recursive call, that also selects the smallest stack found
         # l is the letter to use next
         # m is the length of the current stack
    b=[p,l*(sum(n)*2+m)][n>[]]
         # Sets the current best, if there are no remaining blocks, select the found stack, else we set it to be worse than the possible worst case
    for i,a in e(n):
         # Loop through all the remaining blocks
        for h,d in e(p):
         # Loop through all the columns in the current stack
            if a<11-j(d):b=r([p[f]+l*a*(f==h)for f in c(m)])
         # If we can place the current block vertically in the current column, try it
            if(j(d)<10)*all(j(x)==j(d)for x in p[h:h+a])*(a<=m-h):b=r([p[f]+l*(h<=f<h+a)for f in c(m)])
         # If we can place the current block horizontally starting in the current column, try it
        if a<11:b=r(p+[l*a])
         # If the current block is lower than 10, try place it vertically to the right of the current stack
        b=r(p+[l]*a)
         # Try to place the current horizontally to the right of the current stack
    return["\n".join("".join(9-u<j(x)and x[9-u]or" "for x in b)for u in c(10)),b][o>97]
         # Return the best choice if we aren't in the first call to the function, that is the next letter is a. Else return the found best option formatted as a string

Python 3 , 587 byte

In realtà eseguibile su TIO per alcuni dei casi di test

e,j,c=enumerate,len,range
def f(n,p=[],o=97,b=[]):
    if(not n):return p
    if not b:b="a"*sum(n)*2
    r,q,s,l,m=lambda x:q(f(n[:i]+n[i+1:],x,o+1,b)),lambda x:[b,x][j(b)>j(x)],b,chr(o),j(p)
    if j(b)<=m:return b
    for i,a in e(n):
        for h,d in e(p):
            if a<11-j(d):b=r([p[f]+l*a*(f==h)for f in c(m)])
            if j(d)<10 and a<=m-h and all(map(lambda x:j(x)==j(d),p[h:h+a])):b=r([p[f]+l*(h<=f<h+a)for f in c(m)])
        if s==b:
            if a<11and m+1<j(b):b=r(p[:]+[l*a])
            if m+a<j(b):b=r(p[:]+[l for r in c(a)])
    return["\n".join("".join(map(lambda x:" "if u>=j(x)else x[u],b))for u in c(9,-1,-1)),b][o>97]

Provalo online!

Entrambe le soluzioni potrebbero probabilmente essere giocate a golf un bel po '.




Signor Xcoder, il secondo può essere ridotto di circa 50 byte, non ho applicato le modifiche dal primo al secondo
Halvard Hummel,

So che il secondo può essere giocato a golf molto, ma le modifiche al primo dovrebbero essere utili.
Mr. Xcoder,

1
Hai guadagnato il mio voto, per un ottimo codice con una spiegazione meravigliosa, che mostra un grande sforzo e pensiero. Complimenti e benvenuto in PPCG!
Mr. Xcoder,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.