Ninjas in a Chat Log

2

1

Thanks to HyperNeutrino for making more test cases

Often in chat, someone will ask a question, and multiple people will answer it at the same time. Usually, the person who was beaten to the gun will say "ninja'd", creating even more unnecessary chat.

Given a chat log similar to the following:

Community: Hi
Test: Hi
Rando: What is 4 times 4?
Test: @Rando 16
Community: @Rando 16
Community: ninja'd

You are looking for the number of extraneous lines, which in the above exchange is 2. Two users both replied to another user with the same text, although he only wanted one answer, and then the second user said "ninja'd".

Extraneous Messages

Note that for the following statement, a message is only the content left of the :<space>. However, if an extraneous message is removed, the <username>:<space> is also removed.

Your only task is finding extraneous messages in the input, and counting them. The first type of extraneous message is a message starting with @ that is basically (spaces and case differences are ignored) the same as the message before it. The second type of extraneous message is a message reading exactly ninja'd (case insensitive) immediately after an extraneous message of the first type by the same user (there will never be users with the same name in different cases, and the same user will always be in the same case).

Input

Your input is the chat log, including the user's names, followed by a colon and a space, followed by their message. You may take the input as a newline-separated string, a list of strings, or another appropriate input format.

Output

Your output is the number of extraneous messages.

Test Cases

Community: Hi
Test: Hi
Rando: What is 4 times 4?
Test: @Rando 16
Community: @Rando 16
Community: NINJA'D

2

A: how is everyone doing today
B: good
C: good
C: this doesn't work: `print5`
A: @C add a space
B: @C add aSpace
B: ninja'd
C: ninja'd

2

A: test
B: @A hi
C: @Ahi
C: ninja'd

2

A: test
B: @A hi
B: @A hi
B: ninja'd

2

A: 
B: @
B: @
B: ninja'd
B: ninja'd

2

A: ninja'd
B: ninja'd
C: @B ninja'd
B: @B ninja'd
B: ninja'd
C: ninja'd
C: ninja'd

2

Test: test
Testie: @Test TESTIE
Test: @Testie TESTIE
Test: ninja'd
Testie: TESTIE
Test: TESTIE

0

A: @B hi
C: @B hi
C: ninja'd
B: @C no
A: @CNO
A: ninja'd

4

Stephen

Posted 8 years ago

Reputation: 12 293

Sandbox – Stephen – 8 years ago

3This reminds me of when ninja'd was ninja'd, than the explanation of what ninja'd means was ninja'd, and than the subsequent comment of being ninja'd while talking about ninja'd was ninja'd. – programmer5000 – 8 years ago

2@programmer5000 ninja'd. – ATaco – 8 years ago

Answers

3

Retina, 50 bytes

T`l`L
 

(:@.*)¶(.+)\1
b$2a
b(.*)a¶\1:NINJA'D
aa
a

Try it online!

ovs

Posted 8 years ago

Reputation: 21 408

This is the answer to life, the universe, and everything ... +1 – Zacharý – 8 years ago

@Zacharý Not anymore :( – ovs – 8 years ago

2

Python 2, 237 bytes

l=lambda s:s[1].lower().replace(' ','')
m=[x.split(': ')for x in input()]
i=[0]
for j in range(1,len(m)):i+=[[m[j][1].lower()=="ninja'd"and i[-1]>1and m[j-1][0]==m[j][0],2][l(m[j-1])==l(m[j])and'@'==m[j][1][0]]]
c=i.count
print c(1)+c(2)

Try it online!

-19 bytes thanks to Zacharý

HyperNeutrino

Posted 8 years ago

Reputation: 26 575

@totallyhuman No >.< I solved it after I came back from playing outside with my brother lol. – HyperNeutrino – 8 years ago

Dude. Ýou really need to learn to remove useless spaces: 1 and=>1and. (Put the ý in if ýou credit, please). – Zacharý – 8 years ago

Change the final line into these two: c=i.count and print c(1)+c(2). – Zacharý – 8 years ago

@Zacharý ok, thanks. will make those edits – HyperNeutrino – 8 years ago

Can you combine the for loop into this: for j in range(1,len(m)):i+=[2]if'@'==m[j][1][0]and l(m[j-1][1])==l(m[j][1])else[m[j][1].lower()=="ninja'd"and i[-1]>1and m[j-1][0]==m[j][0]]? – Zacharý – 8 years ago

@Zacharý yes, that works. thanks. – HyperNeutrino – 8 years ago

Oh, make it i+=[2if'@'==m[j][1][0]and l(m[j-1][1])==l(m[j][1])else m[j][1].lower()=="ninja'd"and i[-1]>1and m[j-1][0]==m[j][0]] – Zacharý – 8 years ago

Make it i+=[[m[j][1].lower()=="ninja'd"and i[-1]>1and m[j-1][0]==m[j][0],2][l(m[j-1][1])==l(m[j][1])and'@'==m[j][1][0]]] instead – Zacharý – 8 years ago

@ovs thanks but Zacharý got a shorter one :) – HyperNeutrino – 8 years ago

@Zacharý y u troll me with weird spaces :( lol – HyperNeutrino – 8 years ago

What do you mean? I honestly have no idea what you are talking about. – Zacharý – 8 years ago

Also, you can save a few 3 bytes by changing including the [1] in the definition of l and changing the calls to l accordingly. – Zacharý – 8 years ago

@Zacharý 1. Never mind, I forgot that comments do weird things with spaces to force wrapping; ignore my comment. Also, thanks for the other golf. – HyperNeutrino – 8 years ago

Make the l definition l=lambda s:m[s][1].lower().replace(' ','') and then change the calls to l accordingly again. – Zacharý – 8 years ago

Oh, I just realized something: python2 uses raw_input for strings ... – Zacharý – 8 years ago

1

Python 3, 122 bytes

lambda s:sub(r"b(.*)a\n\1:NINJA'D",'aa',sub(r'.+(:@.*)\n(.+)\1',r'b\2a',sub(' ','',s.upper()))).count('a')
from re import*

Try it online!

Port of my Retina answer

ovs

Posted 8 years ago

Reputation: 21 408