protected override string GetAnswer(string input) { var n = int.Parse(input); var elf1 = new ScoreNode(3); var elf2 = new ScoreNode(7); elf1.Next = elf1.Previous = elf2; elf2.Next = elf2.Previous = elf1; var lastNode = elf2; var scoreCount = 2; while (scoreCount < n + 10) { var scoreSum = elf1.Score + elf2.Score; if (scoreSum >= 10) { lastNode = lastNode.InsertAfter(scoreSum / 10 % 10); scoreCount++; } lastNode = lastNode.InsertAfter(scoreSum % 10); scoreCount++; elf1 = elf1.Increment(1 + elf1.Score); elf2 = elf2.Increment(1 + elf2.Score); } var node = lastNode; if (scoreCount > n + 10) { node = node.Previous; } node = node.Decrement(9); var answer = ""; for (var i = 0; i < 10; i++) { answer += node.Score; node = node.Next; } return(answer); }
protected override int GetAnswer(string input) { var searchScores = input.ToCharArray().Select(c => int.Parse(c.ToString())).ToArray(); var elf1 = new ScoreNode(3); var elf2 = new ScoreNode(7); elf1.Next = elf1.Previous = elf2; elf2.Next = elf2.Previous = elf1; var lastNode = elf2; var scoreCount = 2; while (true) { var scoreSum = elf1.Score + elf2.Score; if (scoreSum >= 10) { scoreCount++; lastNode = lastNode.InsertAfter(scoreSum / 10 % 10); if (CheckForMatch(lastNode, searchScores)) { break; } } scoreCount++; lastNode = lastNode.InsertAfter(scoreSum % 10); if (CheckForMatch(lastNode, searchScores)) { break; } elf1 = elf1.Increment(1 + elf1.Score); elf2 = elf2.Increment(1 + elf2.Score); } return(scoreCount - input.Length); }