public static Int64 SecondPart(string filename) { var desiredSum = FirstPart(filename); var lines = ReadInputs.ReadAllInt64s(ReadInputs.GetFullPath(filename)); for (var i = 0; i < lines.Count; i++) { Int64 sum = lines[i]; var j = i + 1; Int64 min = lines[i]; Int64 max = lines[i]; while (sum < desiredSum && j < lines.Count) { if (min > lines[j]) { min = lines[j]; } if (max < lines[j]) { max = lines[j]; } sum += lines[j]; j++; } if (sum == desiredSum) { return(min + max); } } throw new Exception("We should never get here"); }
public static Int64 FirstPart(string filename) { var lines = ReadInputs.ReadAllInt64s(ReadInputs.GetFullPath(filename)); var preambleLength = 25; var currentSums = CalculateInitialSums(lines, preambleLength); for (var i = preambleLength; i < lines.Count; i++) { //foreach (var item in currentSums) //{ // Console.Write(string.Format("{0} ", item)); //} //Console.WriteLine(string.Format("comparing with {0}", lines[i])); if (!currentSums.Contains(lines[i])) { return(lines[i]); } else { currentSums = RecalculateCurrentSums(currentSums, lines, i, preambleLength); } } throw new Exception("We should never get here"); }
public static Int64 SecondPart(string filename) { var lines = ReadInputs.ReadAllInt64s(ReadInputs.GetFullPath(filename)).OrderBy(l => l).ToList(); var max = lines[lines.Count - 1]; // lines.Add(max); Int64[] numOfSolutions = new Int64[max + 1]; // Knapsackish algorithm for (int i = 0; i < lines.Count; i++) { Int64 num = lines[i]; if (num <= 3) { numOfSolutions[lines[i]]++; // If number < 3 then it can be added initially } for (var j = lines[i] - 1; j > 0 && j > lines[i] - 4; j--) { // Check if there exist previous solutions for numbers lines[j] - 3 if (numOfSolutions[j] != 0) { numOfSolutions[lines[i]] += numOfSolutions[j]; } } } return(numOfSolutions[numOfSolutions.Length - 1]); }