Last active
December 4, 2019 06:01
-
-
Save Jura-Z/11e0495fbabec00328fc8d38ad14c163 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
namespace Table | |
{ | |
[DebuggerDisplay("{name} | {local_score} | #{id}")] | |
class Person | |
{ | |
public int global_score; | |
public int local_score; | |
public int stars; | |
public int id; | |
public long last_star_ts; | |
public Dictionary<int, Dictionary<int, Result>> completion_day_level; | |
public string name; | |
public bool HasResultForDay(int dayNumber) | |
{ | |
return completion_day_level.ContainsKey(dayNumber); | |
} | |
public Result ResultFor(int dayNumber, int problem) | |
{ | |
if (completion_day_level.TryGetValue(dayNumber, out var problems)) | |
if (problems.TryGetValue(problem, out var result)) | |
return result; | |
return new Result {get_star_ts = -1}; | |
} | |
public Result DeltaFor(int dayNumber) | |
{ | |
var part2 = ResultFor(dayNumber, 2); | |
var part1 = ResultFor(dayNumber, 1); | |
if (part2.get_star_ts == -1 || part1.get_star_ts == -1) | |
return new Result {get_star_ts = -1}; | |
return new Result {get_star_ts = part2.get_star_ts - part1.get_star_ts}; | |
} | |
public int StarsFor(int dayNumber) | |
{ | |
if (ResultFor(dayNumber, 2).get_star_ts != -1) | |
return 2; | |
return ResultFor(dayNumber, 1).get_star_ts != -1 ? 1 : 0; | |
} | |
} | |
[DebuggerDisplay("{get_star_ts}")] | |
internal class Result : IComparable<Result> | |
{ | |
public long get_star_ts; | |
public int CompareTo(Result other) | |
{ | |
return get_star_ts.CompareTo(other.get_star_ts); | |
} | |
public override string ToString() | |
{ | |
return DateTimeOffset.FromUnixTimeSeconds(get_star_ts).ToString("HH:mm:ss dd MMM"); | |
} | |
public string ToShortTimeString() | |
{ | |
return DateTimeOffset.FromUnixTimeSeconds(get_star_ts).ToString("HH:mm:ss"); | |
} | |
} | |
internal class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
// https://adventofcode.com/2019/leaderboard/private/view/18994.json | |
var json = File.ReadAllText("json.json"); | |
var o = JObject.Parse(json); | |
var ppl = o["members"].ToObject<Dictionary<int, JObject>>().Values.Select(p => p.ToObject<Person>()).ToArray(); | |
for (var dayNumber = 1; dayNumber <= 31; dayNumber++) | |
{ | |
var pplThisDay = ppl.Where(p => p.HasResultForDay(dayNumber)) | |
.OrderByDescending(p => p.StarsFor(dayNumber)) | |
.ThenBy(p => p.ResultFor(dayNumber, 2)) | |
.ThenBy(p => p.ResultFor(dayNumber, 1)) | |
.ToArray(); | |
if (!pplThisDay.Any()) | |
break; | |
var pplThisDaySorted1 = pplThisDay.Select(p => p.ResultFor(dayNumber, 1).get_star_ts).Where(ts => ts != -1).OrderBy(p => p).ToArray(); | |
var pplThisDaySorted2 = pplThisDay.Select(p => p.ResultFor(dayNumber, 2).get_star_ts).Where(ts => ts != -1).OrderBy(p => p).ToArray(); | |
var pplThisDayDelta12 = pplThisDay.Select(p => p.DeltaFor(dayNumber).get_star_ts).Where(ts => ts != -1).OrderBy(p => p).ToArray(); | |
Console.WriteLine($"DAY {dayNumber}"); | |
foreach (var p in pplThisDay) | |
{ | |
Console.Write($"{p.global_score, 3} {p.local_score, 5} {p.name, 25} "); | |
var res1 = p.ResultFor(dayNumber, 1); | |
if (res1.get_star_ts != -1) | |
Console.Write($"{res1} #{Array.IndexOf(pplThisDaySorted1, res1.get_star_ts) + 1, 3}"); | |
Console.Write(" | "); | |
var res2 = p.ResultFor(dayNumber, 2); | |
var delta = p.DeltaFor(dayNumber); | |
if (res2.get_star_ts != -1) | |
Console.Write($"{res2} #{Array.IndexOf(pplThisDaySorted2, res2.get_star_ts) + 1, 3} ({delta.ToShortTimeString()} second part #{Array.IndexOf(pplThisDayDelta12, delta.get_star_ts) + 1, 3})"); | |
Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment