using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
string[] lines = File.ReadAllLines("results.txt");
Dictionary<string, int> scores = new Dictionary<string, int>();
foreach (string line in lines)
{
// Split the line into name and score
string[] parts = line.Split(':');
string name = parts[0].Trim();
int score = int.Parse(parts[1].Trim());
// If the name is already in the dictionary, keep the highest score
if (scores.ContainsKey(name))
{
if (score > scores[name])
{
scores[name] = score;
}
}
else
{
// Add new name and score
scores[name] = score;
}
}
// Use the Max method from LINQ to find the maximum key length
int maxNameLength = scores.Keys.Max(k => k.Length);
// Sort players by score in descending order
var sortedScores = scores.OrderByDescending(entry => entry.Value);
// Print out the highest scores for each person
foreach (var entry in sortedScores)
{
Console.WriteLine($"{entry.Key.PadRight(maxNameLength)}:{entry.Value.ToString().PadLeft(3)} gissningar");
}
}
}
Erik: 6
Anna: 7
Lars: 6
Johan: 8
Anders: 7
Anna: 10
Sofia: 5
Johan: 9
Anna: 4
Lars: 7