-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
49 lines (45 loc) · 2.19 KB
/
Program.cs
File metadata and controls
49 lines (45 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace aoc202309 // Advent of Code 2023 - Tag 9 - Günther Meusburger
{
internal class Program
{
static void Main(string[] args)
{
var stopwatch=new Stopwatch(); stopwatch.Start();
string path = @"C:\Users\guent\OneDrive\anp_GF07\aoc2023\C#\aoc202309\Input9.txt"; // Ersetzen Sie dies durch den Pfad zu Ihrer Datei
var rows = new List<List<int>>(); var lines = File.ReadLines(path);
foreach (var line in lines)
{
var numbers = new List<int>(); var values = line.Split(' ');
foreach (var value in values) if (int.TryParse(value, out int number)) numbers.Add(number);
rows.Add(numbers);
}
long part1 = 0; long part2 = 0;
foreach (var row in rows) { part1 += extrapolateFwd(row); part2 += extrapolateBwd(row); }
Console.WriteLine($"AoC 2023, Day 9: Part1: {part1}, Part2: {part2} ");
stopwatch.Stop(); Console.WriteLine($"Elapsed Time: {stopwatch.ElapsedMilliseconds} mSec");
int extrapolateFwd(List<int> r) // Extrapolation nach vorne
{ // Rekursion, Ende, wenn alle Elemente der Liste den gleichen Wert haben
if (r.All(x => x == r.Last())) return r.Last();
else
{ // an die Liste ein Int anhängen, der um die differenz der letzten liste erhöht ist
r.Add(r.Last() + extrapolateFwd(r.Zip(r.Skip(1), (a, b) => b - a).ToList()));
return r.Last();
};
}
int extrapolateBwd(List<int> r) // Extrapolation nach vorne
{ // Rekursion, Ende, wenn alle Elemente der Liste den gleichen Wert haben
if (r.All(x => x == r.Last())) return r.Last();
else
{ // an die erste Stelle die Differenz aus der Vorliste vom ersten Wert abziehen und einfügen
r.Insert(0, r[0] - extrapolateBwd(r.Zip(r.Skip(1), (a, b) => b - a).ToList()));
return r[0];
};
}
}
}
}