รหัสใหญ่…สำหรับคุณ!


32

ในฉากเปิดของDark Knight Risesมีการพูดคุยที่ค่อนข้างงุ่มง่ามระหว่างตัวแทน CIA และ Bane จอมวายร้าย

ตัวแทน CIA: “ ถ้าฉันดึง [หน้ากาก] ออกมาคุณจะตายไหม”

สารพิษ: “ มันจะเจ็บปวดอย่างยิ่ง…”

ตัวแทน CIA: “ คุณเป็นคนที่ใหญ่มาก”

สารพิษ: “ …สำหรับคุณ”

ไม่แน่ใจว่า Bane ตั้งใจที่จะพูดว่า "เจ็บปวดกับคุณ" หรือ "ผู้ชายตัวใหญ่สำหรับคุณ" มาแก้ไขปัญหานี้กันดีกว่าสำหรับการเล่นกอล์ฟรหัส!

ท้าทาย

งานของคุณคือเขียนโปรแกรมที่เรียงลำดับบทสนทนาข้างต้นขึ้นอยู่กับจำนวนเต็มที่ระบุเป็นอินพุต

คำพูดของตัวแทน CIA คือ: If I pull that off will you die? You're a big guy.

คำพูดของ Bane คือ: It would be extremely painful... for you!

โปรดทราบว่าdie?, You’re, painful...และyou!จะถือว่าเป็นคำเดียว

  1. กำหนดจำนวนเต็ม n เป็นอินพุตให้แปลงเป็นไบนารี
  2. การอ่านตัวเลขไบนารีจากซ้ายไปขวาออกคำต่อไปจากการเจรจาสายลับซีไอเอถ้าหลักคือและคำถัดไปจากสารพิษบทสนทนาถ้าหลักคือ1 0คำควรคั่นด้วยช่องว่าง เมื่อผู้พูดเปลี่ยนให้เพิ่มการป้อนบรรทัด นอกจากนี้ให้ใส่คำนำหน้าแต่ละบรรทัดสนทนาด้วยชื่อผู้พูด ( BANE:หรือCIA:)

คุณสามารถสันนิษฐานได้ว่าอินพุตเริ่มต้นด้วย 1 ในรูปแบบไบนารีเสมอและมี 12 อันและ 7 ศูนย์

ตัวอย่าง

522300

  1. แปลงเป็นไบนารี่: 1111111100000111100

  2. จำนวนเริ่มต้นด้วย 8 คำดังนั้นเราจึงส่งออก 8 คำแรกจากบทสนทนาตัวแทนของ CIA และนำหน้าด้วย CIA: CIA: If I pull that off will you die?

  3. จากนั้นเราได้ 5 ศูนย์ดังนั้นเราจึงได้ 5 คำแรกจากบทสนทนาของ Bane: BANE: It would be extremely painful...

  4. จากนั้นมี 4 คำดังนั้นเราจึงส่งออกคำ CIA ถัดไปที่ 4: CIA: You’re a big guy.

  5. จากนั้น 2 ศูนย์: BANE: for you!

ผลสุดท้าย:

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

กรณีทดสอบเพิ่มเติม:

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.

แต่ละบรรทัดสามารถลงท้ายด้วยช่องว่างได้หรือไม่?
xnor

@xnor ใช่มันไม่เป็นไร
Arnaud

"that" และ "it" เป็นคำแรกของคำพูดของ Bane ที่ไม่เข้าแถวในกรณีทดสอบ / ข้อความอ้างอิง
MildlyMilquetoast

@Mistah จับจ้องไปที่ "มัน" ขอบคุณ!
Arnaud

3
มันเป็นแค่ฉันหรือคนที่อ่านบทสนทนาด้วยเสียงของสารพิษ
Gurupad Mamadapur

คำตอบ:


10

Pyth - 138 ไบต์

ฉันควรดูการบีบอัดสคริปต์ภาพยนตร์

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 ไบต์

แก้ไข: บันทึก 2 ไบต์โดยการยืมtrim()ความคิดจากคำตอบ 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()

กรณีทดสอบ


7

JavaScript (ES6), 209 202 201 ไบต์

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

วิธีเก่า:

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

ตัวอย่างการทดสอบ

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


ฉันยืมtrim()ความคิดของคุณอย่างไร้ยางอาย ฉันแน่ใจว่ามีบางสิ่งที่ดีกว่าslice(1)แต่ไม่สามารถเข้าใจได้
Arnauld

@Arnauld สบายดีกับฉันตอนนี้เราทุกคนก็ผูกกันหมด ฉันเดิมพันว่ามีวิธีที่จะทำได้ภายใน 200 :-)
ETHproductions

6

C ++ 11 (GCC), 298 293 ไบต์

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

std::stringฟังก์ชั่นแลมบ์ดาที่ใช้เวลาเป็นจำนวนเต็มและผลตอบแทนการเจรจาเป็นที่ คุณสามารถดูได้ในการดำเนินการที่นี่

เวอร์ชันที่ไม่ได้อัปโหลด (พร้อมคำอธิบาย):

#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 ไบต์

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

หมายเหตุ

นี่คือความคืบหน้าของการเล่นกอล์ฟฉันคิดว่าฉันยังคงสามารถกำจัดบางไบต์ แต่อย่าลังเลที่จะออกคำแนะนำในความคิดเห็น


4

Python 3.6, 232 ไบต์

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

แก้ไขอย่างเท่าเทียมกัน:

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 ไบต์

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

มี unprintables จำนวนมากดังนั้นคุณก็ต้องการจะดีกว่าการทดสอบออนไลน์


2

Perl 6 , 211 ไบต์

{.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 ไบต์

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

เรียกทำงานด้วยหมายเลขอินพุตเป็นพารามิเตอร์

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

กลับไปที่ 396 ไบต์เพราะฉันไม่ได้สังเกตเห็นกฎ "ไม่มีการขึ้นบรรทัดใหม่ที่จุดเริ่มต้น"


น่าเศร้าs.Replace("1",c[k++]).Replace("0",b[l++])ไม่ได้แทนที่ด้วยคำที่เกี่ยวข้อง ดูเหมือนว่า C # จะได้รับการแทนที่สตริงหนึ่งครั้งและตบมันในสถานที่
Mr Scapegrace

2

ทับทิม, 204 + 1 = 205 ไบต์

ต้องใช้-nธง

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 ไบต์

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 ไบต์

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

ใส่ลงในไฟล์ชื่อ 494542 และเรียกใช้ดังนี้:

perl -lX 494542

ทดสอบกับ perl v5.22


1

Clojure, 401 ไบต์

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

ภาวนา:

(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))))))
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.