Skip to content

Commit 6321c33

Browse files
committed
D9: Both parts solved and optimized.
1 parent fffba69 commit 6321c33

File tree

4 files changed

+22
-31
lines changed

4 files changed

+22
-31
lines changed

Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
using AoCHelper;
2-
using Formula9.AdventOfCode.Solutions2023.Day08;
3-
using Formula9.AdventOfCode.Utils;
2+
using Formula9.AdventOfCode.Solutions2023.Day10;
43

54
namespace Formula9.AdventOfCode;
65

76
public class Program
87
{
98
public static void Main(string[] args)
109
{
11-
Type day = typeof(Day_08);
10+
Type day = typeof(Day_10);
1211
// var _d = Activator.CreateInstance(day) as AdventOfCodeProblem;
13-
//Console.WriteLine(_d.Solve_1().AsTask().Result);
14-
//Console.WriteLine(_d.Solve_2().AsTask().Result);
12+
// Console.WriteLine(_d.Solve_1().AsTask().Result);
13+
// Console.WriteLine(_d.Solve_2().AsTask().Result);
1514
Solver.Solve(options => {
1615
options.ShowTotalElapsedTimePerDay = false;
1716
options.ShowOverallResults = false;

Solutions/Day08/Day08.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public override ValueTask<string> Solve_1()
4949
return new(steps.ToString());
5050
}
5151

52-
// 20685524831999
5352
public override ValueTask<string> Solve_2()
5453
{
5554
HashSet<string> startPoints = Graph.Where(kvp => kvp.Key.EndsWith("A")).Select(kvp => kvp.Key).ToHashSet();

Solutions/Day09/Day09.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@ namespace Formula9.AdventOfCode.Solutions2023.Day09;
44

55
public class Day_09 : AdventOfCodeProblem
66
{
7-
public Day_09() : base(2023, 09) { }
7+
public List<List<long>> Histories { get; init; }
88

9-
public override ValueTask<string> Solve_1() => new("Solution 1");
9+
public Day_09() : base(2023, 09)
10+
{
11+
Histories = Input.SplitByNewline().Select(l => l.Split(" ").Select(long.Parse).ToList()).ToList();
12+
}
1013

11-
public override ValueTask<string> Solve_2() => new("Solution 2");
14+
public static long Project(IEnumerable<long> values, int indexToExclude)
15+
{
16+
int n = values.Count();
17+
return (long)Math.Round(Enumerable.Range(0, n).Where(n => n != indexToExclude).Product(k => (double)(n - k) / (indexToExclude - k)));
18+
}
19+
20+
public static double LagrangeInterpolatingPolynomial(IEnumerable<long> values) => values.Sum((v, index) => v * Project(values, index));
21+
22+
public override ValueTask<string> Solve_1() => new(Histories.Sum(LagrangeInterpolatingPolynomial).ToString());
23+
24+
public override ValueTask<string> Solve_2() => new(Histories.Sum(h => LagrangeInterpolatingPolynomial(h.AsEnumerable().Reverse())).ToString());
1225
}

Utils/EnumerableUtils.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ public static bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate, o
1616
return false;
1717
}
1818

19-
public static IEnumerable<T> Sort<T>(this IEnumerable<T> source, IComparer<T> comparer)
20-
{
21-
var lst = source.ToList();
22-
lst.Sort(comparer);
23-
return lst;
24-
}
25-
2619
public static long Sum<T>(this IEnumerable<T> source, Func<T, int, long> func)
2720
{
2821
long result = 0;
@@ -46,24 +39,11 @@ public static IEnumerable<T> RepeatInfinitely<T>(this IEnumerable<T> source)
4639
}
4740
}
4841

49-
public static int Move<T>(this IEnumerator<T> enumerator, int steps)
50-
{
51-
for (int i = 0; i < steps; i++)
52-
{
53-
if (!enumerator.MoveNext()) return i;
54-
}
55-
return steps;
56-
}
57-
58-
public static IEnumerator<T> Move<T>(this IEnumerator<T> enumerator, int steps, out int stepsMoved)
59-
{
60-
stepsMoved = enumerator.Move(steps);
61-
return enumerator;
62-
}
63-
6442
public static T Next<T>(this IEnumerator<T> enumerator)
6543
{
6644
enumerator.MoveNext();
6745
return enumerator.Current;
6846
}
47+
48+
public static double Product<T>(this IEnumerable<T> values, Func<T, double> keySelector) => values.Aggregate(1d, (acc, el) => acc * keySelector(el));
6949
}

0 commit comments

Comments
 (0)