Battaglia delle lettere


9

Il tuo compito è semplice: dimmi chi vince la battaglia delle lettere.

Le truppe

Ci sono tre diverse "truppe" in questa battaglia, riassunte da questa tabella.

name | health | damage
   A       25       25
   B      100        5
   C       10       50

Puoi usare tre caratteri univoci per rappresentare le truppe, ma devi specificare se non sono queste lettere.

La battaglia

Supponiamo di avere una battaglia di esempio:

ABC # army 1
CBA # army 2

Ogni esercito spara ripetutamente contro l'unità più a sinistra, fino a quando non è morto; poi si spostano nella truppa a destra e si ripetono. Quindi l'esercito 2 attacca Anell'esercito 1 fino a quando non Aè morto, quindi passa a B, quindi C. L'esercito 1 attacca Cprima, poi B, poi A. Supponi che gli eserciti attaccino allo stesso tempo, e quindi le truppe spareranno sempre se fossero vivi prima del round e possano uccidersi a vicenda allo stesso tempo. Sparano in ordine da sinistra a destra.

La battaglia sarebbe finita così:

ABC
CBA

BC # A (25 hp) killed by C (-50 hp), B (100 hp) attacked by B (-5 hp) and A (-25 hp), has 70 hp
BA # C (10 hp) killed by A (-25 hp), B (100 hp) attacked by B (-5 hp) and C (-50 hp), has 45 hp

BC # B (70 hp) attacked by B (-5 hp) and A (-25 hp), has 40 hp
A  # B (45 hp) killed by B (-5 hp) and C (-50 hp)

BC # B (40 hp) attacked by A (-25 hp), has 15 health
   # A (25 hp) killed by B (-5 hp) and C (-50 hp), army 2 dead

Pertanto, l'esercito 1 vince la battaglia!

Ingresso

Due stringhe, la prima che rappresenta l'esercito 1 e la seconda armata 2. Non hanno necessariamente le stesse dimensioni (perché chi ha detto che sarebbe stato un combattimento equo?)

Produzione

Qualsiasi tre valori unici e costanti per rappresentare l'esercito 1 vincente, l'esercito 2 vincente o l'improbabile evento di un pareggio. Sì, è possibile che le ultime truppe si uccidano a vicenda, finendo in parità.

battaglie

ABC
CBA
Army 1

CCCCC
CCCCC
Tie

CABCAB
ABBABBA
Army 2

Si applicano scappatoie standard . Devi inviare un programma completo.

Questo è , vince la soluzione più breve.


"costante" Perché?
Calcolatrice

Inoltre, Abatte Be Ccravatte Be Acravatte C. Cambiare uno dei Avalori di uno per 20farlo legare B.
Calcolatrice

2
Ci è consentito utilizzare una rappresentazione di input diversa? Dire 012 invece di ABC?
Grimmy,

@Grimy: Sì, modificherò il post.
Neil A.

Risposte:


3

Pyth 145 97 byte

=Y.d[,\A,25 25,\B,*TT5,\C,T*5T)Km=G@YdzJm=H@YdwW&KJMX0hG_smedH gKJ gJKI<hhK0=tK)I<hhJ0=tJ;?K1?J2Z

Un po 'meno ingenuo di prima.

Provalo online!

spiegazioni:

Tagliamo il programma in più parti.

=Y.d[,\A,25 25,\B,*TT5,\C,T*5T)

     ,\A,25 25                     Create this list: ['A', [25, 25]]
              ,\B,*TT5             Create this list: ['B', [100, 5]]
                      ,\C,T*5T     Create this list: ['C', [10, 50]]
  .d[                         )    From the three lists, create a dictionary whose keys are the letters, and values are the inner lists
=Y                                 Assign to the variable Y

Il dizionario Yè: {'A': [25, 25], 'C': [10, 50], 'B': [100, 5]}. Poi:

Km=G@YdzJm=H@YdwMX0hG_smedH

 m=G@Ydz                           For all letters in first input string, make a copy of Y[letter]. Make a list of all those values...
K                                  ...and assign the list to the variable K
         m=H@Ydw                   For all letters in second input string, make a copy of Y[letter]. Make a list of all those values...
        J                          ...and assign the list to the variable J
                MX0hG_smedH        Create a function g which takes two lists of couples, and subtract the sum of the second elements of the couples of the second list from the first element of the first couple of the first list

A questo punto, Kè un elenco di coppie che rappresenta il primo esercito. Ogni coppia di questa lista è una lettera dell'esercito, ed è (health, damage)per quella lettera. Jè esattamente lo stesso, ma per il secondo esercito. gè una funzione che prende due eserciti e infligge il danno arrecato dal secondo esercito al primo. Adesso:

W&KJ gKJ gJKI<hhK0=tK)I<hhJ0=tJ;

W&KJ                               While K not empty and J not empty
     gKJ                           Call g(K,J). That computes the damages to first army
         gJK                       Call g(J,K). That computes the damages to second army
            I<hhK0=tK)             If the first army's first letter is dead, remove it
                      I<hhJ0=tJ    If the second army's first letter is dead, remove it
                               ;   End of while loop

Quando il ciclo while è finito Ke Jhanno il loro valore finale. Se sono entrambi vuoti, è un pareggio; altrimenti vince l'esercito non vuoto. Questo è gestito dall'ultimo pezzo di codice:

?K1?J2Z

?K1                                If K non-empty, display 1. Else...
   ?J2                             ...if J non-empty, display 2. Else...
      Z                            ...display zero

Questo è tutto!


2

Haskell , 199 193 179 176 171 byte

a!b=(t<$>a)?(t<$>b)
t v=[(5,5),(20,1),(2,10)]!!(fromEnum v-65)
m=sum.map snd
f=filter((>0).fst)
[]?[]=0
[]?_=2
_?[]=1
a@((c,d):e)?b@((h,i):j)=f((c-m b,d):e)?f((h-m a,i):j)

Provalo online!

Piccolo trucco: diviso tutte le statistiche dell'esercito per 5.


0

C #, 446 byte

using System.Linq;(c,d)=>{int z=c.Length,v=d.Length,j=0,k=0,l=0,m=0,q=0;int[]e=(c+d).Select(x=>x!='A'?x=='B'?100:10:25).ToArray(),f=e.Skip(z).ToArray();e=e.Take(z).ToArray();int[]g=(c+d).Select(x=>x!='A'?x=='B'?5:100:25).ToArray(),h=g.Skip(z).ToArray();g=g.Take(z).ToArray();try{for(;;){for(q=l;q<z;q++){if(e[j]<=0)j++;e[j]-=h[q];}for(q=k;q<v;q++){if(f[m]<=0)m++;f[m]-=g[q];}if(e[k]<=0)k++;if(f[l]<=0)l++;}}catch{}return k-z>=l-v?k-z>l-v?0:2:1;};

Versione formattata:

         (c, d) => {
                int z = c.Length, v = d.Length, j = 0, k = 0, l = 0, m = 0, q = 0;

                int[] e = (c + d).Select(x => x != 'A' ? x == 'B' ? 100 : 10 : 25).ToArray(), f = e.Skip(z).ToArray();
                e = e.Take(z).ToArray();

                int[] g = (c + d).Select(x => x != 'A' ? x == 'B' ? 5 : 100 : 25).ToArray(), h = g.Skip(z).ToArray();
                g = g.Take(z).ToArray();

                try {
                    for (;;) {
                        for (q = l; q < z; q++) {
                            if (e[j] <= 0) j++; e[j] -= h[q];
                        }
                        for (q = k; q < v; q++) {
                            if (f[m] <= 0) m++; f[m] -= g[q];
                        }
                        if (e[k] <= 0) k++; if (f[l] <= 0) l++;
                    }
                }
                catch {
                }

                return k - z >= l - v ? k - z > l - v ? 0 : 2 : 1;
            };

Emette 1 se Army1 vince, 2 per Army2 e 0 per pareggio


Puoi aggiungere una versione formattata / espansa? 446 byte è alto anche per C # Sono sicuro che ci saranno alcuni miglioramenti.
TheLethalCoder

Per i principianti hai più righe che dichiarano int[]che penso che puoi combinarle, `` <= 0` è <1sicuramente lo stesso ? Ti serve il try-catch?
TheLethalCoder il

0

Javascript (ES6) - 316 269 ​​byte

Sono sicuro che questo può essere giocato a golf come l'inferno, ma questo è quello che ho inventato :) Sono riuscito a radere 47 byte!

Uscite 0 per pareggio, 1 per squadra 1 e 2 per squadra 2.

l=(d,f)=>{for(t=[d,f].map(g=>g.split``.map(k=>[[25,100,10],[25,5,50]].map(m=>m[k.charCodeAt()-65])));(w=t.map(g=>g.some(k=>0<k[0])))[0]&&w[1];)t.forEach((g,k,m)=>m[k].sort(o=>0>o[0])[0][0]-=m[+!k].filter(o=>0<o[0]).reduce((o,p)=>o+p[1],0));return w[0]||w[1]?w[0]?1:2:0}

Leggibile :

function ltt(a,b){
    t=[a,b].map(x=>x.split``.map(c=>[[25,100,10],[25,5,50]].map(e=>e[c.charCodeAt()-65])))
    while((w=t.map(_=>_.some(x=>x[0]>0)))[0]&&w[1]){
        t.forEach((y,i,n)=>n[i].sort(j=>j[0]<0)[0][0]-=n[+!i].filter(x=>x[0]>0).reduce((h,v)=>h+v[1],0))
    }
    return(!w[0]&&!w[1])?0:(w[0])?1:2
}

Demo :

l=(d,f)=>{for(t=[d,f].map(g=>g.split``.map(k=>[[25,100,10],[25,5,50]].map(m=>m[k.charCodeAt()-65])));(w=t.map(g=>g.some(k=>0<k[0])))[0]&&w[1];)t.forEach((g,k,m)=>m[k].sort(o=>0>o[0])[0][0]-=m[+!k].filter(o=>0<o[0]).reduce((o,p)=>o+p[1],0));return w[0]||w[1]?w[0]?1:2:0}

var prnt=(g,h)=>{
  n=l(g,h);
  return(n==0)?"Tie!":`Team ${n} wins!`
}

console.log("ABCB - ABC: " + prnt("ABCB","ABC"));
console.log("BAAA - BBC: " + prnt("BAAA","BBC"));
console.log("AAAA - BBC: " + prnt("AAAA","BBC"));
console.log("ABC - BBC: " + prnt("ABC","BBC"));
console.log("ABC - CBA: " + prnt("ABC","CBA"));


Ho appena notato che la versione corrente non è in grado di valutare i legami, cosa che la versione di 316 byte potrebbe. Ci penserò su
Hankrecords il
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.