Last active
September 4, 2024 14:41
-
-
Save realhaidinh/8157fe4361bcffe3ffe08a94dc7f43a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input = System.argv() | |
|> List.first() | |
if input != nil do | |
lines = File.stream!(input) | |
|> Stream.map(&String.trim/1) | |
output = lines | |
|> Enum.reduce(%{}, fn (line, result)-> | |
[_, team_a, score_a, team_b, score_b] = Regex.run(~r{^([\w\s]+)(\d+), ([\w\s]+)(\d+)$}, line) | |
[team_a, team_b, score_a, score_b] = [String.trim(team_a), String.trim(team_b), String.to_integer(score_a), String.to_integer(score_b)] | |
[point_a, point_b] = | |
cond do | |
score_a > score_b -> [3, 0] | |
score_a < score_b -> [0, 3] | |
true -> [1, 1] | |
end | |
result | |
|> Map.put(team_a, Map.get(result, team_a, 0) + point_a) | |
|> Map.put(team_b, Map.get(result, team_b, 0) + point_b) | |
end) | |
output | |
|> Enum.sort(fn({a, point_a}, {b, point_b}) -> if(point_a == point_b, do: a < b, else: point_a > point_b) end) | |
|> Enum.with_index(1) |> Enum.reduce({-1, 1}, fn ({{cur_team, cur_point}, index}, {prev_point, prev_pos}) -> | |
cur_pos = if(prev_point != cur_point, do: index, else: prev_pos) | |
IO.puts "#{cur_pos}. #{cur_team}, #{cur_point} pt#{if(cur_point != 1, do: "s", else: "")}" | |
{cur_point, cur_pos} | |
end) | |
else | |
IO.puts "Missing filename." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment