32
\$\begingroup\$

At the opening scene of the Dark Knight rises, there is a rather awkward dialogue between a CIA agent and the villain Bane.

CIA agent: “If I pull that [mask] off, will you die?”

Bane: “It would be extremely painful…”

CIA agent: “You’re a big guy.”

Bane: “…for you.”

It’s unsure if Bane intends to say “painful for you” or “big guy for you”. Let’s settle this problem once for all through code golfing!

Challenge

Your task is write a program that reorders the above dialogue depending on an integer given as input.

CIA agent dialogue words are: If I pull that off will you die? You're a big guy.

Bane’s dialogue words are: It would be extremely painful... for you!

Please note that die?, You’re, painful... and you! are considered as single words.

  1. Given an integer n as input, convert it to binary
  2. Reading the binary digits from left to right, output the next word from CIA agent dialogue if the digit is 1, and the next word from Bane dialogue if the digit is 0. The words should be separated by a space. When the speaker changes, add a line feed. Also, prefix each dialogue line with the speaker name (BANE: or CIA:).

You can assume that the input always starts with a 1 in binary form, and has 12 ones and 7 zeroes.

Example

522300

  1. Converted to binary: 1111111100000111100

  2. The number starts with 8 ones, so we output the 8 first words from CIA’s agent dialogue, and prefix them with CIA: CIA: If I pull that off will you die?

  3. Then we got 5 zeroes, so we get the 5 first words from Bane’s dialogue: BANE: It would be extremely painful...

  4. Then there are 4 ones, so we output the 4 next CIA words: CIA: You’re a big guy.

  5. Then 2 zeroes: BANE: for you!

Final result:

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

More test cases:

494542

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.
\$\endgroup\$
  • \$\begingroup\$ Can each line end in a space? \$\endgroup\$ – xnor Feb 3 '17 at 3:44
  • \$\begingroup\$ @xnor Yes, that's ok. \$\endgroup\$ – Arnaud Feb 3 '17 at 3:46
  • \$\begingroup\$ "that" and "it" as first word of Bane's speech don't line up in test cases/quote \$\endgroup\$ – MildlyMilquetoast Feb 3 '17 at 3:51
  • \$\begingroup\$ @Mistah fixed to "it", thanks! \$\endgroup\$ – Arnaud Feb 3 '17 at 3:54
  • 3
    \$\begingroup\$ Is it just me or someone read the dialogue in bane's voice. \$\endgroup\$ – Gurupad Mamadapur Feb 3 '17 at 11:23

13 Answers 13

10
\$\begingroup\$

Pyth - 138 bytes

I should look into compressing the movie script.

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.

\$\endgroup\$
7
\$\begingroup\$

JavaScript (ES6), 203 201 bytes

Edit: saved 2 bytes by shamelessly borrowing the trim() idea from ETHproductions answer

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()

Test cases

let f =

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()

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

\$\endgroup\$
7
\$\begingroup\$

JavaScript (ES6), 209 202 201 bytes

(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())``

Old approach:

(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()

Test snippet

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))

\$\endgroup\$
  • \$\begingroup\$ I'm shamelessly borrowing your trim() idea. I was sure there was something better than slice(1) but couldn't figure it out. \$\endgroup\$ – Arnauld Feb 3 '17 at 18:53
  • \$\begingroup\$ @Arnauld Fine with me, now we're all tied up. I bet there's a way to do it in under 200 :-) \$\endgroup\$ – ETHproductions Feb 4 '17 at 2:02
6
\$\begingroup\$

C++11 (GCC), 298 293 bytes

#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;}

A lambda function that takes an integer and returns the dialogue as a std::string. You can see it in action here.

Ungolfed version (with some explanation):

#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;
}
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 252 227 226 bytes

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` `

Usage

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)

Notes

This is golfing-in-progress, I think I can still shave off some bytes, but feel free to leave suggestions in the comments.

\$\endgroup\$
4
\$\begingroup\$

Python 3.6, 232 bytes

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])

Edit, equivalently:

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))
\$\endgroup\$
3
\$\begingroup\$

Japt, 121 bytes

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

Contains many unprintables, so you'd just be better off testing it online.

\$\endgroup\$
2
\$\begingroup\$

Perl 6, 211 bytes

{.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)}
\$\endgroup\$
2
\$\begingroup\$

C#, 398 390 385 396 389 bytes

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);}}

Launch with input number as parameter.

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); 
    }
}

Back to 396 bytes because I didn't notice "no newline at the begining" rule.

\$\endgroup\$
  • \$\begingroup\$ Sadly, s.Replace("1",c[k++]).Replace("0",b[l++]) doesn't replace with corresponding words. It seems, C# gets replacing string once and slaps it in places. \$\endgroup\$ – Mr Scapegrace Feb 3 '17 at 15:02
2
\$\begingroup\$

Ruby, 204+1 = 205 bytes

Requires the -n flag.

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)*' '}
\$\endgroup\$
2
\$\begingroup\$

PHP, 198 bytes

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];
\$\endgroup\$
1
\$\begingroup\$

Perl, 205 bytes

@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*//;

Put that into a file named 494542 and run like this:

perl -lX 494542

Tested on perl v5.22

\$\endgroup\$
1
\$\begingroup\$

Clojure, 401 bytes

(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))))))

Invocation:

(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))))))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.