-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge2.cs
138 lines (113 loc) · 3.56 KB
/
Challenge2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Challenge 2 - The Lucky One
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Challenge2
{
static string InputFileName = @"/Inputs/Challenge 2/submitInput.txt";
static string OutputFileName = @"/Outputs/Challenge 2/submitOutput2.txt";
static void Main()
{
string[] input = File.ReadAllLines(InputFileName);
int casesTotal = int.Parse(input[0]);
string[] output = new string[casesTotal];
var indexInput = 1;
for (int i = 0; i < casesTotal; i++)
{
int resultsTotal = int.Parse(input[indexInput]);
indexInput++;
List<Result> results = GetResults(input, indexInput, resultsTotal);
indexInput += resultsTotal;
var bestPlayer = GetBestPlayer(results);
output[i] = string.Format("Case #{0}: {1}", i + 1, bestPlayer.Num);
}
File.WriteAllLines(OutputFileName, output);
}
static List<Result> GetResults(string[] input, int index, int total)
{
List<Result> results = new List<Result>();
string line = string.Empty;
for (int i = 0; i < total; i++)
{
line = input[index];
results.Add(GetResult(line));
index++;
}
return results;
}
static Player GetBestPlayer(List<Result> results)
{
Player betterPlayer = null,
aux = null;
var grouByWins = results
.GroupBy(x => x.Winner)
.Select(x => new Player(){ Num = x.Key.Num, Wins = x.Count(), Loses = 0 })
.OrderByDescending(x => x.Wins)
.ToList();
var grouByLoses = results
.GroupBy(x => x.Losser)
.Select(x => new Player(){ Num = x.Key.Num, Wins = x.Key.Wins, Loses = x.Count() })
.OrderByDescending(x => x.Wins)
.ToList();
foreach (var group in grouByWins)
{
group.Wins -= grouByLoses
.Where(x => x.Num == group.Num)
.Count();
if (aux == null)
{
aux = new Player() { Num = group.Num, Wins = group.Wins };
}
else
{
if (aux.Wins != group.Wins)
{
betterPlayer = aux.Wins > group.Wins ? aux :
new Player() { Num = group.Num, Wins = group.Wins };
break;
}
}
}
return betterPlayer;
}
static Result GetResult(string line)
{
string[] aux = line.Split(' ');
if (aux[2] == "0")
{
return new Result
{
Winner = new Player() { Num = int.Parse(aux[1]) },
Losser = new Player() { Num = int.Parse(aux[0]) },
};
}
else
{
return new Result
{
Winner = new Player() { Num = int.Parse(aux[0]) },
Losser = new Player() { Num = int.Parse(aux[1]) },
};
}
}
public class Player : IEquatable<Player>
{
public int? Num { get; set; }
public int? Wins { get; set; }
public int Loses { get; set; }
public bool Equals(Player other)
{
return Num == other.Num;
}
public override int GetHashCode()
{
return Num == null ? 0 : Num.GetHashCode();
}
}
public class Result
{
public Player Winner { get; set; }
public Player Losser { get; set; }
}
}