Grande codice ... per te!


32

Alla scena iniziale di Dark Knight sorge , c'è un dialogo piuttosto imbarazzante tra un agente della CIA e il cattivo Bane.

Agente della CIA: "Se toglierò quella [maschera], morirai?"

Bane: "Sarebbe estremamente doloroso ..."

Agente della CIA: "Sei un ragazzo grosso".

Bane: "... per te."

Non è sicuro se Bane intende dire "doloroso per te" o "ragazzo grosso per te". Risolviamo questo problema una volta per tutte attraverso il golf del codice!

Sfida

Il tuo compito è scrivere un programma che riordini la finestra di dialogo sopra in base a un numero intero indicato come input.

Le parole di dialogo degli agenti della CIA sono: If I pull that off will you die? You're a big guy.

Le parole di dialogo di Bane sono: It would be extremely painful... for you!

Si prega di notare che die?, You’re, painful...e you!sono considerati come singole parole.

  1. Dato un numero intero n come input, convertirlo in binario
  2. Leggendo le cifre binarie da sinistra a destra, emette la parola successiva dal dialogo dell'agente CIA se la cifra è 1, e la parola successiva dal dialogo Bane se la cifra è 0. Le parole dovrebbero essere separate da uno spazio. Quando l'altoparlante cambia, aggiungi un avanzamento riga. Inoltre, aggiungi a ogni riga di dialogo il nome del relatore ( BANE:o CIA:).

Si può presumere che l'input inizi sempre con un 1 in forma binaria e abbia 12 e 7 zero.

Esempio

522300

  1. Convertito in binario: 1111111100000111100

  2. Il numero inizia con 8, quindi abbiamo prodotto le 8 prime parole dal dialogo dell'agente della CIA e le abbiamo prefissate con la CIA: CIA: If I pull that off will you die?

  3. Quindi abbiamo ottenuto 5 zero, quindi otteniamo le 5 prime parole dal dialogo di Bane: BANE: It would be extremely painful...

  4. Quindi ce ne sono 4, quindi abbiamo prodotto le 4 parole successive della CIA: CIA: You’re a big guy.

  5. Quindi 2 zero: BANE: for you!

Risultato finale:

CIA: If I pull that off will you die?
BANE: It would be extremely painful...
CIA: You’re a big guy.
BANE: for you!

Altri casi di test:

494.542

CIA: If I pull that
BANE: It would be
CIA: off
BANE: extremely
CIA: will you die? You're
BANE: painful... for
CIA: a big guy.
BANE: you!

326711

CIA: If
BANE: It would
CIA: I pull that off will you
BANE: be extremely painful... for
CIA: die? You're
BANE: you!
CIA: a big guy.

Ogni linea può finire in uno spazio?
xnor

@xnor Sì, va bene.
Arnaud,

"that" e "it" come prima parola del discorso di Bane non si allineano nei casi di test / citazione
MildlyMilquetoast

@Mistah corretto su "it", grazie!
Arnaud,

3
Siamo solo io o qualcuno a leggere il dialogo con la voce di Bane.
Gurupad Mamadapur,

Risposte:


10

Pyth - 138 byte

Dovrei cercare di comprimere la sceneggiatura del film.

K_cc"guy big a You're die? you will off that pull I If you! for painful... extremely be would That"d12jmj;++@c"BANECIA"4ed\:m.)@Kedhdr8jQ2

Test Suite .


7

JavaScript (ES6), 203 201 byte

Modifica: salvato 2 byte prendendo spudoratamente in prestito l' trim()idea dalla risposta ETHproductions

n=>"If I pull that off will you die? You're a big guy. It would be extremely painful... for you!".split` `.map((_,i,a)=>[`
BANE: `,`
CIA: `,' '][j^(j=n>>18-i&1)?j:2]+a[k[j]++],k=[j=12,0]).join``.trim()

Casi test


7

JavaScript (ES6), 209 202 201 byte

(n,a="It would be extremely painful... for you! If I pull that off will you die? You're a big guy.".split` `,b=[6,18])=>(F=(s,p=n%2)=>n?F((p^(n>>=1)%2?p?`
CIA: `:`
BANE: `:` `)+a[b[p]--]+s):s.trim())``

Vecchio approccio:

(n,a="If I pull that off will you die? You're a big guy. It would be extremely painful... for you!".split` `,b=[12,0])=>(F=p=>p^n%2?F(n%2)+(p?`
CIA:`:`
BANE:`):n?F(p,n>>=1)+" "+a[b[p]++]:``)(n%2).trim()

Snippet di prova

f=(n,a="It would be extremely painful... for you! If I pull that off will you die? You're a big guy.".split` `,b=[6,18])=>(F=(s,p=n%2)=>n?F((p^(n>>=1)%2?p?`
CIA: `:`
BANE: `:` `)+a[b[p]--]+s):s.trim())``

console.log(f(522300))
console.log(f(494542))
console.log(f(326711))


Prendo spudoratamente in prestito la tua trim()idea. Ero sicuro che c'era qualcosa di meglio di slice(1)ma non riuscivo a capirlo.
Arnauld,

@Arnauld Bene con me, ora siamo tutti legati. Scommetto che c'è un modo per farlo in meno di 200 :-)
ETHproductions

6

C ++ 11 (GCC), 298 293 byte

#include<sstream>
[](int i){std::stringstream b("It would be extremely painful... for you!"),c("If I pull that off will you die? You're a big guy.");std::string s,w;int n=0,t=i,p;while(t/=2)n++;for(;n>=0;p=t)((t=i>>n&1)?c:b)>>w,s+=p^t?t?"CIA: ":"BANE: ":" ",s+=t^(i>>--n)&1?w+"\n":w;return s;}

Una funzione lambda che accetta un numero intero e restituisce il dialogo come a std::string. Puoi vederlo in azione qui .

Versione non golfata (con alcune spiegazioni):

#include<sstream>

[](int i) {
    std::stringstream bane("It would be extremely painful... for you!"),
                      cia("If I pull that off will you die? You're a big guy.");
    std::string s, w;
    int n = 0, t = i, p;
    // Find the position of the most significant bit (n)
    while (t/=2) n++;

    for (; n>=0; p=t) {
        t = i>>n&1; // Current bit
        // Append the speaker name if the previous bit was different
        if (t != p) s += (t ? "CIA: " : "BANE: ");
        else s += " ";

        // Read the next word from one of the streams
        if (t) cia >> w;
        else bane >> w;

        s += w;

        if (t != ((i>>--n)&1)) // Append a newline if the next bit is different
            s += "\n";
    }

    return s;
}

4

JavaScript (ES6), 252 227 226 byte

n=>[...n.toString(2,b="If I pull that off will you die? You're a big guy.".split` `,c="It would be extremely painful... for you!".split` `)].map((a,i,j)=>(a!=j[i-1]?+a?i?`
CIA: `:`CIA: `:`
BANE: `:``)+(+a?b:c).shift()).join` `

uso

f=n=>[...n.toString(2,b="If I pull that off will you die? You're a big guy.".split` `,c="It would be extremely painful... for you!".split` `)].map((a,i,j)=>(a!=j[i-1]?+a?i?`
CIA: `:`CIA: `:`
BANE: `:``)+(+a?b:c).shift()).join` `
f(522300)

Gli appunti

Si tratta di golf in progress, penso di poter ancora radere alcuni byte, ma mi sento libero di lasciare suggerimenti nei commenti.


4

Python 3.6, 232 byte

from itertools import*
c="you! for painful... extremely be would It".split(),"guy. big a You're die? you will off that pull I If".split()
for n,r in groupby(f'{int(input()):b}',int):
 print("BCAINAE::"[n::2],*[c[n].pop()for _ in r])

Modifica , equivalentemente:

from itertools import*
c="It would be extremely painful... for you!".split(),"_ If I pull that off will you die? You're a big guy.".split()
for n,r in groupby(map(int,f'{int(input()):b}')):
 print("BCAINAE::"[n::2],*map(c[n].pop,r))

3

Japt, 121 byte

A=[`It Ùd ¼ extÚ+ pafª... f y!`¸`If I pªl È     f Øi y ¹e? Y' a big guy.`¸]¢®^T?["
BANE:""
CIA:"]gT=Z :P +S+AgZ vÃx

Contiene molti non stampabili, quindi faresti meglio a provarlo online .


2

Perl 6 , 211 byte

{.put for map |*,zip map {.[0]X .[1].rotor: .[2..*]},zip <CIA: BANE:>,(<If I pull that off will you die? You're a big guy.>,<It would be extremely painful... for you!>),|(.base(2)~~m:g/(.)$0*/)».chars.rotor(2)}

2

C #, 398 390 385 396 389 byte

class P{static void Main(string[] a){string s="2"+System.Convert.ToString(int.Parse(a[0]),2),e="CIA: ",r="BANE: ",o="";int k=0,l=0,i=1;string[] c="If I pull that off will you die? You're a big guy.".Split(' '),b="It would be extremely painful... for you!".Split(' ');for(;i<s.Length;i++)o+=(s[i-1]==s[i]?" ":(i<2?"":"\n")+(s[i]>'0'?e:r))+(s[i]>'0'?c[k++]:b[l++]);System.Console.Write(o);}}

Avvia con il numero di input come parametro.

Ungolfed:

class P
{
    static void Main(string[] a)
    {
        string s = "2" + System.Convert.ToString(int.Parse(a[0]), 2), e = "CIA: ", r = "BANE: ", o = "";
        int k = 0, l = 0, i = 1;

        string[] c = "If I pull that off will you die? You're a big guy.".Split(' '), b = "It would be extremely painful... for you!".Split(' ');
        for (; i < s.Length; i++)
            o += (s[i - 1] == s[i] ? " " : (i<2?"":"\n") + (s[i] > '0' ? e : r))
                + (s[i] > '0' ? c[k++] : b[l++]);
        System.Console.Write(o); 
    }
}

Torna a 396 byte perché non ho notato la regola "nessuna newline all'inizio".


Purtroppo, s.Replace("1",c[k++]).Replace("0",b[l++])non sostituisce con le parole corrispondenti. Sembra che C # sostituisca una volta la stringa e la schiaffi in alcuni punti.
Mr Scapegrace,

2

Rubino, 204 + 1 = 205 byte

Richiede la -nbandiera.

d=[%w"It would be extremely painful... for you!",%w"If I pull that off will you die? You're a big guy."]
n=%w"BANE CIA"
("%b"%$_).scan(/((.)\2*)/).map{|i,b|puts n[k=b.to_i]+': '+d[k].slice!(0,i.size)*' '}

2

PHP, 198 byte

while($i++<19)echo($t-$s=1&$argv[1]>>19-$i)?"
"[$i<2].($s?CIA:BANE).": ":" ",explode(0,"It0would0be0extremely0painful...0for0you!0If0I0pull0that0off0will0you0die?0You're0a0big0guy.")[$$s+++7*$t=$s];

1

Perl, 205 byte

@t=([qw/It would be extremely painful... for you!/],[qw/If I pull that off will you die? You're a big guy./]);$_=sprintf'%b',$0;print$n?'BANE':'CIA',": @{[splice$t[$n=1-$n],0,length$&]}"while s/(.)\1*//;

Inseriscilo in un file chiamato 494542 ed eseguilo in questo modo:

perl -lX 494542

Testato su perl v5.22


1

Clojure, 401 byte

(require '[clojure.string :as s])(defn f[n](let[c(map #(-(int %) 48)(Integer/toString n 2))p[(s/split"It would be extremely painful... for you!"#" ")(s/split"If I pull that off will you die? You're a big guy."#" ")]a["BANE" "CIA"]](loop[i[0 0] g"" d c q 2](if(<(count d)1)g(let[b(first d)j(i b)](recur(assoc i b (inc j))(str g(if(= b q)" "(str(when(not= 2 q)"\n")(a b)": "))((p b) j))(rest d) b))))))

Invocazione:

(f 522300)

Ungolfed

(require '[clojure.string :as s])
(defn dialogue[num]
    (let [dacode (map #(- (int %) 48) (Integer/toString num 2))
          phrases [(s/split"It would be extremely painful... for you!"#" ")(s/split"If I pull that off will you die? You're a big guy."#" ")]
      actors ["BANE" "CIA"]]
      (loop [idxs [0 0] finaldial "" code dacode prevbit 2]
        (if (< (count code) 1) finaldial
            (let [bit (first code) idx (idxs bit)]
              (recur (assoc idxs bit (inc idx)) (str finaldial (if (= bit prevbit) " " (str (when (not= 2 prevbit) "\n") (actors bit) ": ")) ((phrases bit) idx)) (rest code) bit))))))
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.