In the PPCG chatroom the Nineteenth Byte, using carets ^
(or carrots) is a way of indicating that you agree with one of the previously made comments just above yours.
A caret message consists solely of N ^
characters (where N is a positive integer) and it means agreement with the Nth previous message. So a single ^
means agreement with the message immediately previous, ^^
means agreement with the message two lines up, ^^^
means agreement with the message three lines up, and so on.
Additionally, when a caret message X is in agreement (a.k.a. pointing towards) another caret message Y, then X is said to be in agreement with what Y is in agreement with. There may be multiple layers of this and, in the end, all caret messages are indicating agreement with one non-caret message.
For example, if a chat transcript looks like this: (one message per line)
I like dogs [line 1]
I like cats [line 2]
^ [line 3]
^^^ [line 4]
^^ [line 5]
I like turtles [line 6]
^ [line 7]
^^^ [line 8]
^^ [line 9]
Then lines 1, 2, and 6 are non-caret messages and all the others are caret messages which point to non-caret messages:
- Line 3 points directly to line 2.
- Line 4 points directly to line 1.
- Line 5 points to line 3, which points to line 2.
- Line 7 points to line 6.
- Line 8 points to line 5, which points to line 3, which points to line 2.
- Line 9 points to line 7, which points to line 6.
Thus, including the users who wrote the non-caret message (and assuming people don't caret their own message) we can conclude that:
- 2 people agree with
I like dogs
(Lines 1 and 4.) - 4 people agree with
I like cats
(Lines 2, 3, 5, and 8.) - 3 people agree with
I like turtles
(Lines 6, 7, and 9.)
Challenge
Write a program or function that takes in a multiline string similar to the example above where every line represents a chat message, with older messages coming first.
Every line will have at least one character and there will be at least one line. All messages will either be caret messages consisting solely of ^
's, or be non-caret messages consisting of letters and spaces ([ a-zA-Z]+
in regex).
For every non-caret message, in any order, output the number of people that agree with it in some clear format that contains the message text, e.g.
2 - I like dogs
4 - I like cats
3 - I like turtles
or
I like cats (4)
I like dogs (2)
I like turtles (3)
or
{"I like cats" : 4, "I like turtles" : 3, "I like dogs" : 2}
You can assume that:
- People always agree with their own messages and do not caret themselves.
- No two non-caret messages are identical.
- Caret messages won't point to things before the first message.
- Lines will not contain leading or trailing spaces.
The shortest code in bytes wins.
Test Cases
bread is bread
1 - bread is bread
---
animals are fuzzy
^
^
^
^^^
^^
^^^^^^
7 - animals are fuzzy
---
pie
^
^^
pi
^
^^
^^^^
^
^^^^^
^^^^^
^^^
^^^^
^^
^
^^^^^^^^^
9 - pie
6 - pi
---
a
b
c
^
^
^
1 - a
1 - b
4 - c
---
a
b
c
^
^^
^^^
1 - a
1 - b
4 - c
---
a
b
c
^^^
^^^^
^^^^^
4 - a
1 - b
1 - c
---
W
^
^^
X
^^^
^^^^
Y
^^^^^
^^^^^^
Z
^^^^^^^
^^^^^^^^
1 - Y
3 - X
1 - Z
7 - W
---
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqretuvwxyz
^
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqretuvwxyz
2 - ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqretuvwxyz
1 - ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqretuvwxyz
---
I like dogs
I like cats
^
^^^
^^
I like turtles
^
^^^
^^
2 - I like dogs
4 - I like cats
3 - I like turtles