public string Solve(string input) { var firstCups = input.Select(c => c - '0').ToArray(); var cups = new LinkedList <int>( firstCups .Concat(Enumerable.Range(firstCups.Max() + 1, 1_000_000 - firstCups.Length))); var currentCup = cups.First; var removedCups = new LinkedListNode <int> [3]; var maxCup = cups.Max(); var current = cups.First; var cupNodes = new Dictionary <int, LinkedListNode <int> >(); while (current != null) { cupNodes[current.Value] = current; current = current.Next; } for (int move = 1; move <= 10_000_000; move++) { if (move % 1_000_000 == 0) { Console.WriteLine(move); } removedCups[0] = currentCup.Next ?? cups.First; removedCups[1] = removedCups[0].Next ?? cups.First; removedCups[2] = removedCups[1].Next ?? cups.First; var destinationCup = currentCup.Value - 1; while (destinationCup == 0 || removedCups.Any(c => c.Value == destinationCup)) { if (destinationCup == 0) { destinationCup = maxCup; continue; } destinationCup--; } //Console.WriteLine($"-- move {move} --"); //Console.WriteLine("cups: " + string.Join("", cups.Select(c => c == currentCup.Value ? $"({c})" : $" {c} "))); //Console.WriteLine("pick up: " + string.Join(", ", removedCups.Select(c => c.Value))); //Console.WriteLine("destination: " + destinationCup); //Console.WriteLine(); foreach (var cupToRemove in removedCups) { cups.Remove(cupToRemove); } var destinationCupNode = cupNodes[destinationCup]; cups.AddAfter(destinationCupNode, removedCups[0]); destinationCupNode = destinationCupNode.Next; cups.AddAfter(destinationCupNode, removedCups[1]); destinationCupNode = destinationCupNode.Next; cups.AddAfter(destinationCupNode, removedCups[2]); currentCup = currentCup.Next ?? cups.First; } var first = cups.Find(1).Next ?? cups.First; var second = first.Next ?? cups.First; var result = (long)first.Value * second.Value; return(result.ToString()); }
protected List <int> PlayGameLinkedList(List <int> cups, int iterations) { var list = new LinkedList <int>(cups); var cupDictionary = new LinkedListNode <int> [list.Count]; var cupNode = list.First; while (cupNode != null) { cupDictionary[cupNode.Value - 1] = cupNode; cupNode = cupNode.Next; } var minCup = cups.Min(); var maxCup = cups.Max(); LinkedListNode <int> currentItem = list.First; var removedItems = new LinkedListNode <int> [3]; for (var i = 0; i < iterations; i++) { for (var j = 0; j < 3; j++) { if (currentItem.Next != null) { removedItems[j] = currentItem.Next; list.Remove(currentItem.Next); } else { removedItems[j] = list.First; list.Remove(list.First); } } var requiredValue = currentItem.Value - 1; if (requiredValue < minCup) { requiredValue = maxCup; } while (removedItems.Any(a => a.Value == requiredValue)) { requiredValue--; if (requiredValue < minCup) { requiredValue = maxCup; } } var insertNode = cupDictionary[requiredValue - 1]; foreach (var value in removedItems) { list.AddAfter(insertNode, value); insertNode = value; } currentItem = currentItem.Next; if (currentItem == null) { currentItem = list.First; } } return(list.ToList()); }
public IEnumerable <IndexResult> RetrieveDocuments(params string[] keywords) { keywords = keywords.Where(keyword => index.ContainsKey(keyword)).ToArray(); if (keywords.Length == 0) { yield break; } PostingsList[] keywordEntries = keywords .Select(keyword => index[keyword]) //.Select(keyword => (index.ContainsKey(keyword) ? index[keyword] : null)) //.Where(x => (x != null)) //.OrderBy(postingList => postingList.Documents.Count) .ToArray(); //if (keywordEntries.Length == 0) //{ // return Enumerable.Empty<IndexResult>(); //} LinkedListNode <Tuple <long, int> >[] currentNodes = new LinkedListNode <Tuple <long, int> > [keywordEntries.Length]; for (int i = 0; i < keywordEntries.Length; i++) { currentNodes[i] = keywordEntries[i].Documents.First; } while (currentNodes.Any(x => (x != null))) { long smallestID = currentNodes.Where(x => (x != null)).OrderBy(node => node.Value.Item1).First().Value.Item1; IDictionary <string, int> keywordsPresent = new Dictionary <string, int>(); for (int i = 0; i < keywordEntries.Length; i++) { if ((currentNodes[i] != null) && (currentNodes[i].Value.Item1 == smallestID)) { keywordsPresent[keywords[i]] = currentNodes[i].Value.Item2; currentNodes[i] = currentNodes[i].Next; } } yield return(new IndexResult(documentsByID[smallestID].URI, keywordsPresent)); } //LinkedList<Tuple<long, int>> matchingDocuments = keywordEntries.Select(postingList => postingList.Documents).Aggregate((postingList1, postingList2) => // { // LinkedList<Tuple<long, int>> newList = new LinkedList<Tuple<long, int>>(); // LinkedListNode<Tuple<long, int>> currentNode1 = postingList1.First; // LinkedListNode<Tuple<long, int>> currentNode2 = postingList2.First; // while ((currentNode1 != null) && (currentNode2 != null)) // { // if (currentNode1.Value.Item1 == currentNode2.Value.Item1) // { // newList.AddLast(currentNode1.Value); // currentNode1 = currentNode1.Next; // currentNode2 = currentNode2.Next; // } // else if (currentNode1.Value.Item1 < currentNode2.Value.Item1) // { // currentNode1 = currentNode1.Next; // } // else // { // currentNode2 = currentNode2.Next; // } // } // return newList; // }); // return matchingDocuments.Select(document => new IndexResult(documentsByID[document.Item1].URI, // keywords // .Where(keyword => index.ContainsKey(keyword)) // .ToDictionary(keyword => keyword, keyword => index[keyword].Documents.Single(doc => doc.Item1 == document.Item1).Item2))); }
public IEnumerable<IndexResult> RetrieveDocuments(params string[] keywords) { keywords = keywords.Where(keyword => index.ContainsKey(keyword)).ToArray(); if (keywords.Length == 0) { yield break; } PostingsList[] keywordEntries = keywords .Select(keyword => index[keyword]) //.Select(keyword => (index.ContainsKey(keyword) ? index[keyword] : null)) //.Where(x => (x != null)) //.OrderBy(postingList => postingList.Documents.Count) .ToArray(); //if (keywordEntries.Length == 0) //{ // return Enumerable.Empty<IndexResult>(); //} LinkedListNode<Tuple<long, int>>[] currentNodes = new LinkedListNode<Tuple<long, int>>[keywordEntries.Length]; for (int i = 0; i < keywordEntries.Length; i++) { currentNodes[i] = keywordEntries[i].Documents.First; } while (currentNodes.Any(x => (x != null))) { long smallestID = currentNodes.Where(x => (x != null)).OrderBy(node => node.Value.Item1).First().Value.Item1; IDictionary<string, int> keywordsPresent = new Dictionary<string, int>(); for (int i = 0; i < keywordEntries.Length; i++) { if ((currentNodes[i] != null) && (currentNodes[i].Value.Item1 == smallestID)) { keywordsPresent[keywords[i]] = currentNodes[i].Value.Item2; currentNodes[i] = currentNodes[i].Next; } } yield return new IndexResult(documentsByID[smallestID].URI, keywordsPresent); } //LinkedList<Tuple<long, int>> matchingDocuments = keywordEntries.Select(postingList => postingList.Documents).Aggregate((postingList1, postingList2) => // { // LinkedList<Tuple<long, int>> newList = new LinkedList<Tuple<long, int>>(); // LinkedListNode<Tuple<long, int>> currentNode1 = postingList1.First; // LinkedListNode<Tuple<long, int>> currentNode2 = postingList2.First; // while ((currentNode1 != null) && (currentNode2 != null)) // { // if (currentNode1.Value.Item1 == currentNode2.Value.Item1) // { // newList.AddLast(currentNode1.Value); // currentNode1 = currentNode1.Next; // currentNode2 = currentNode2.Next; // } // else if (currentNode1.Value.Item1 < currentNode2.Value.Item1) // { // currentNode1 = currentNode1.Next; // } // else // { // currentNode2 = currentNode2.Next; // } // } // return newList; // }); // return matchingDocuments.Select(document => new IndexResult(documentsByID[document.Item1].URI, // keywords // .Where(keyword => index.ContainsKey(keyword)) // .ToDictionary(keyword => keyword, keyword => index[keyword].Documents.Single(doc => doc.Item1 == document.Item1).Item2))); }