1
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – Stephen
    Jul 20, 2017 at 19:12
  • 3
    \$\begingroup\$ @programmer5000 ninja'd. \$\endgroup\$
    – ATaco
    Jul 21, 2017 at 6:43

3 Answers 3

3
\$\begingroup\$

Retina, 50 bytes

T`l`L
 

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This is the answer to life, the universe, and everything ... +1 \$\endgroup\$
    – Adalynn
    Jul 20, 2017 at 21:16
  • \$\begingroup\$ @Zacharý Not anymore :( \$\endgroup\$
    – ovs
    Jul 21, 2017 at 6:21
2
\$\begingroup\$

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ý

\$\endgroup\$
15
  • \$\begingroup\$ @totallyhuman No >.< I solved it after I came back from playing outside with my brother lol. \$\endgroup\$
    – hyper-neutrino
    Jul 20, 2017 at 19:29
  • \$\begingroup\$ Dude. Ýou really need to learn to remove useless spaces: 1 and=>1and. (Put the ý in if ýou credit, please). \$\endgroup\$
    – Adalynn
    Jul 20, 2017 at 21:11
  • \$\begingroup\$ Change the final line into these two: c=i.count and print c(1)+c(2). \$\endgroup\$
    – Adalynn
    Jul 20, 2017 at 21:13
  • \$\begingroup\$ @Zacharý ok, thanks. will make those edits \$\endgroup\$
    – hyper-neutrino
    Jul 20, 2017 at 21:14
  • \$\begingroup\$ 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]]? \$\endgroup\$
    – Adalynn
    Jul 20, 2017 at 21:18
1
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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