using PwcLib; namespace _2_relative_sort; public static class RelativeSort { public static void Main(string[] args) { var line1 = Console.ReadLine() ?? ""; var line2 = Console.ReadLine() ?? ""; var sortedInts = line1.GetInts("list1") .OrderRelativeTo(line2.GetInts("list2")); Console.WriteLine($"({string.Join(", ", sortedInts)})"); } /// /// Orders the elements of the source list relative to the order of the reference list. /// private static int[] OrderRelativeTo(this IEnumerable source, IEnumerable reference) { var rank = reference.ToRankDictionary(); return source .OrderBy(x => rank.GetValueOrDefault(x, defaultValue: x + rank.Count)) .ToArray(); } /// /// Returns a dictionary that maps each element of the source to its index in the source. /// private static Dictionary ToRankDictionary(this IEnumerable source) where TSource : notnull => source.Select((x, i) => (x, i)).ToDictionary(xi => xi.x, xi => xi.i); }