C#-Quellcode
- private static void Part01()
- {
- var src = Source(FileName)
- .Select(x => x.Split(" ",
- StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse).ToArray());
- var result = 0;
- foreach (var numbers in src)
- result += ToAllZeroDifferences(numbers)
- .ToArray().Reverse().Sum(x => x.Last());
- Console.WriteLine($"{nameof(Part01)}: {result}");
- }
C#-Quellcode
- private static void Part02()
- {
- var src = Source(FileName)
- .Select(x => x.Split(" ",
- StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse).ToArray());
- var result = 0;
- foreach (var numbers in src)
- {
- var sum = 0;
- var tmp = ToAllZeroDifferences(numbers)
- .ToArray().Reverse().ToArray();
- for (var i = 1; i < tmp.Length; i++)
- sum = tmp[i].First() - sum;
- result += sum;
- }
- Console.WriteLine($"{nameof(Part02)}: {result}");
- }
C#-Quellcode
- private static List<List<int>> ToAllZeroDifferences(int[] numbers)
- {
- var result = new List<List<int>> { numbers.ToList() };
- while (true)
- {
- var tmp = new List<int>();
- for (var i = 1; i < result.Last().Count; i++)
- tmp.Add(result.Last()[i] - result.Last()[i - 1]);
- result.Add(tmp);
- if (tmp.Sum() == 0) return result;
- }
- }
C#-Quellcode
- private static void Part01_20_LINQ()
- {
- static int[][] Differences(int[] numbers)
- {
- var result = new List<int[]> { numbers };
- while (result.Last().Sum() != 0) result.Add(Difference(result.Last()));
- return [.. result];
- }
- static int[] Difference(int[] numbers) =>
- numbers.Zip(numbers.Skip(1)).Select(p => p.Second - p.First).ToArray();
- var part_1 = Source()
- .Select(x => x.Split(" ",
- StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse).ToArray()).Select(Differences).ToArray()
- .Select(x => x.Reverse().Sum(y=>y.Last())).Sum();
- Console.WriteLine($"{nameof(Part01)}: {part_1}");
- var part_2 = Source()
- .Select(x => x.Split(" ",
- StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse).ToArray()).Select(Differences).ToArray()
- .Select(x => x.Reverse().Aggregate(0, (a, b) => -a + b.First())).Sum();
- Console.WriteLine($"{nameof(Part02)}: {part_2}");
- }
Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „exc-jdbi“ ()