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 2017-07-20T19:12:12.297

Reputation: 12 293

Sandbox – Stephen – 2017-07-20T19:12:43.547

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 – 2017-07-21T01:02:34.877

2@programmer5000 ninja'd. – ATaco – 2017-07-21T06:43:49.873

Answers

3

Retina, 50 bytes

T`l`L
 

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

Try it online!

ovs

Posted 2017-07-20T19:12:12.297

Reputation: 21 408

This is the answer to life, the universe, and everything ... +1 – Zacharý – 2017-07-20T21:16:45.427

@Zacharý Not anymore :( – ovs – 2017-07-21T06:21:57.987

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 2017-07-20T19:12:12.297

Reputation: 26 575

@totallyhuman No >.< I solved it after I came back from playing outside with my brother lol. – HyperNeutrino – 2017-07-20T19:29:04.053

Dude. Ýou really need to learn to remove useless spaces: 1 and=>1and. (Put the ý in if ýou credit, please). – Zacharý – 2017-07-20T21:11:54.400

Change the final line into these two: c=i.count and print c(1)+c(2). – Zacharý – 2017-07-20T21:13:38.090

@Zacharý ok, thanks. will make those edits – HyperNeutrino – 2017-07-20T21:14:09.823

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ý – 2017-07-20T21:18:23.683

@Zacharý yes, that works. thanks. – HyperNeutrino – 2017-07-20T21:19:34.423

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ý – 2017-07-20T21:20:26.543

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ý – 2017-07-20T21:25:05.163

@ovs thanks but Zacharý got a shorter one :) – HyperNeutrino – 2017-07-20T21:40:41.767

@Zacharý y u troll me with weird spaces :( lol – HyperNeutrino – 2017-07-20T21:46:55.687

What do you mean? I honestly have no idea what you are talking about. – Zacharý – 2017-07-20T21:55:38.347

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ý – 2017-07-21T15:42:42.047

@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 – 2017-07-21T15:47:35.420

Make the l definition l=lambda s:m[s][1].lower().replace(' ','') and then change the calls to l accordingly again. – Zacharý – 2017-07-21T15:50:03.970

Oh, I just realized something: python2 uses raw_input for strings ... – Zacharý – 2017-07-21T15:56:55.800

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 2017-07-20T19:12:12.297

Reputation: 21 408