#* http://hyperpublic.com/challenge/
This is a solution to Question 1:
Hyperpublic users can add their friends by emailing a photo of them to
add@hyperpublic.com. We want to determine a user’s influence on the system
by determining how many users he is responsible for. A user’s influence is
calculated by giving him 1 point for every user he’s added in addition to
the sum of the influence scores of each user that he’s added.
Example: User 0 adds User 1 and User 2. User 1 adds User 3.
User 0’s influence score is 3. (He added two users and one of them added a
third user.)
User 1's is 1.
User 2’s is 0.
User 3’s is 0.
The above scenario is represented by the following input file. Line i is
user ID i and position j within the line is marked with an X if user ID i
added user ID j. Both row and column indicies are 0-based:
OXXO
OOOX
OOOO
OOOO
Use the input file here (http://hyperpublic.com/challenge2input.txt) to determine what the highest influence score is among 100 random Hyperpublic users
*#
include :file
#Keep track of users in an array (of arrays)
users = []
user_index = 0
#Read in file. For each user ID, keep an array of user IDs they added
file.each_line "challenge2input.txt" { l |
users[user_index] = []
#Iterate over each character in the line
l.dice.each_with_index { c, i |
true? c == "X"
{ users[user_index] << i }
}
user_index = user_index + 1
}
#Recusively determine influence of given user ID
influence_of = { index |
users[index].length + users[index].map({ u | influence_of(u) }).sum
}
influence = []
#Calculate influence of each user
0.to user_index - 1 { i |
influence[i] = influence_of i
}
p influence.sort.reverse[0, 2]