Created
December 9, 2023 06:00
-
-
Save snluu/68a8951cb19669aabd2d65247e1bda6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Day9 | |
{ | |
public static void Run() | |
{ | |
var lines = Input.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | |
long total = 0; | |
foreach (var line in lines) | |
{ | |
// for part 2 just reverse this array | |
var nums = line.Split(' ').Select(int.Parse).ToArray(); | |
var iters = 0; | |
while (iters < nums.Length) | |
{ | |
iters += 1; | |
var allZeros = true; | |
for (int i = 0; i < nums.Length - iters; i++) | |
{ | |
nums[i] = nums[i + 1] - nums[i]; | |
if (nums[i] != 0) | |
{ | |
allZeros = false; | |
} | |
} | |
if (allZeros) | |
{ | |
break; | |
} | |
} | |
long next = 0; | |
for (int i = nums.Length - 1 - iters; i < nums.Length; i++) | |
{ | |
next += nums[i]; | |
} | |
total += next; | |
} | |
Console.WriteLine("Total: {0}", total); | |
} | |
const string Input = | |
""" | |
0 3 6 9 12 15 | |
1 3 6 10 15 21 | |
10 13 16 21 30 45 | |
"""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment