Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving2023 #15

Merged
merged 16 commits into from
Dec 9, 2023
Prev Previous commit
Next Next commit
solve day09 part 2
  • Loading branch information
deckelmouck committed Dec 9, 2023
commit 49548c4b0f0be0957e5d9b4b8ce958e93ebb27f7
80 changes: 80 additions & 0 deletions 2023/Day09/solution.cs
Original file line number Diff line number Diff line change
@@ -95,6 +95,86 @@ public void SolvePart1()
public void SolvePart2()
{
Console.WriteLine($"this is part 2");

solutionBase sb = new();
//var input = sb.getInputLines(@"2023/Day09/test").ToArray();
var input = sb.getInputLines(@"2023/Day09/input").ToArray();

long solution = 0;

for (int i = 0; i < input.Length; i++)
{
var inputLine = "0 " + input[i];
//Console.WriteLine(inputLine);

var line = inputLine.Split(" ");
//Console.WriteLine(line.Length);

//hint found - next value of last values each sequence
List<long> lastValues = new List<long>();

//initial sequence length
int sequenceLength = line.Length - 1;

//for temporary storage of last line
long[] lastLine = new long[sequenceLength];

//init with starting sequence
lastLine = line.Select(x => Convert.ToInt64(x)).ToArray();

//save last value for later
//lastValues.Add(lastLine[^1]);

ArrayOutput(lastLine);

for (int l = 0; l <= sequenceLength; l++)
{
//reduce sequence length for next iteration
//sequenceLength--;

long[] newline = new long[sequenceLength - l];

//create new sequence
for (int j = 0; j < newline.Length; j++)
{
long left = Convert.ToInt64(lastLine[j]);
long right = Convert.ToInt64(lastLine[j + 1]);

long newValue = right - left;

newline[j] = newValue;
}

//save last value for later
//lastValues.Add(newline[^1]);

ArrayOutput(newline);



//put new sequence in temp storage
lastLine = newline;

//check if all values are 0
//var sum = newline.Sum();

//check if all values are 0
// if(ArrayFullOfZeros(newline))
// {
// //Console.WriteLine($"line {i} is a solution");
// break;
// }
if(newline.Length == 1)
{
lastValues.Add(newline[0]);
break;
}
}
//lastValues.Add(lastLine[0] * -1);
Console.WriteLine($"{lastValues.Sum()}");
solution += lastValues.Sum();
}
Console.WriteLine($"solution is {solution}");
}

public void ArrayOutput(long[] array)