Skip to content

Commit 986f4ae

Browse files
committed
D11: Both parts solved, will also push D10 later, got lazy.
1 parent 6321c33 commit 986f4ae

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed

2023.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="AoCHelper" Version="3.1.0" />
1313
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
1414
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
15+
<PackageReference Include="System.Interactive" Version="6.0.1" />
1516
</ItemGroup>
1617

1718
</Project>

Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using AoCHelper;
22
using Formula9.AdventOfCode.Solutions2023.Day10;
3+
using Formula9.AdventOfCode.Solutions2023.Day11;
4+
using Formula9.AdventOfCode.Utils;
35

46
namespace Formula9.AdventOfCode;
57

68
public class Program
79
{
810
public static void Main(string[] args)
911
{
10-
Type day = typeof(Day_10);
12+
Type day = typeof(Day_11);
1113
// var _d = Activator.CreateInstance(day) as AdventOfCodeProblem;
1214
// Console.WriteLine(_d.Solve_1().AsTask().Result);
1315
// Console.WriteLine(_d.Solve_2().AsTask().Result);

Runner/AdventOfCodeFetcher.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ private AdventOfCodeFetcher()
1515
Client = new HttpClient(ClientHandler);
1616
}
1717

18+
19+
1820
public string GetInputFromWeb(int year, int day, string saveToPath)
1921
{
22+
ThrowIfPuzzleIsNotRevealedYet(year, day);
2023
string result = FetchInputForDayFromWeb(year, day);
2124
if (!string.IsNullOrEmpty(saveToPath))
2225
{
23-
string dirName = Path.GetDirectoryName(saveToPath);
24-
if (!Directory.Exists(dirName))
26+
string? dirName = Path.GetDirectoryName(saveToPath);
27+
if (dirName != null && !Directory.Exists(dirName))
2528
{
2629
Directory.CreateDirectory(dirName);
2730
}
@@ -32,6 +35,15 @@ public string GetInputFromWeb(int year, int day, string saveToPath)
3235
return result;
3336
}
3437

38+
private static void ThrowIfPuzzleIsNotRevealedYet(int year, int day)
39+
{
40+
var date = DateTime.UtcNow;
41+
if (year > date.Year || (year == date.Year && day > date.Day) || (year == date.Year && day == date.Day && date.Hour < 5))
42+
{
43+
throw new PuzzleNotRevealedExceptionException();
44+
}
45+
}
46+
3547
private string FetchInputForDayFromWeb(int year, int day)
3648
{
3749
var response = Client.GetAsync($"https://adventofcode.com/{year}/day/{day}/input").Result;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Formula9.AdventOfCode.Runner;
4+
5+
[Serializable]
6+
public class PuzzleNotRevealedExceptionException : Exception
7+
{
8+
public PuzzleNotRevealedExceptionException() { }
9+
public PuzzleNotRevealedExceptionException(string message) : base(message) { }
10+
public PuzzleNotRevealedExceptionException(string message, Exception inner) : base(message, inner) { }
11+
protected PuzzleNotRevealedExceptionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
12+
}

Solutions/Day11/Day11.cs

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

55
public class Day_11 : AdventOfCodeProblem
66
{
7-
public Day_11() : base(2023, 11) { }
87

9-
public override ValueTask<string> Solve_1() => new("Solution 1");
8+
public ISet<int> EmptyRows { get; init; }
9+
public ISet<int> EmptyCols { get; init; }
10+
public ISet<(int y, int x)> Stars { get; init; }
11+
public ISet<((int y, int x) left, (int y, int x) right)> StarPairs { get; private set; }
1012

11-
public override ValueTask<string> Solve_2() => new("Solution 2");
13+
public Day_11() : base(2023, 11)
14+
{
15+
EmptyRows = new HashSet<int>();
16+
EmptyCols = new HashSet<int>();
17+
Stars = new HashSet<(int y, int x)>();
18+
StarPairs = new HashSet<((int y, int x) left, (int y, int x) right)>();
19+
ReadData();
20+
}
21+
22+
public void ReadData()
23+
{
24+
var grid = Input.SplitByNewline();
25+
foreach ((int y, var line) in grid.Enumerate())
26+
{
27+
if (IsLineEmpty(line))
28+
{
29+
EmptyRows.Add(y);
30+
continue;
31+
}
32+
33+
foreach ((int x, char c) in line.Enumerate())
34+
{
35+
if (c == '#') Stars.Add((y, x));
36+
}
37+
}
38+
39+
foreach (int x in Enumerable.Range(0, grid[0].Length))
40+
{
41+
if (Enumerable.Range(0, grid.Length).Select(y => grid[y][x]).ToHashSet().Count == 1)
42+
{
43+
EmptyCols.Add(x);
44+
}
45+
}
46+
47+
StarPairs = Stars.Combinations(2).Select(enumerable => (left: enumerable.First(), right: enumerable.Last())).ToHashSet();
48+
}
49+
50+
public long FindShortestPath((int x, int y) left, (int x, int y) right, int expansionLevel)
51+
{
52+
(int smallestX, int smallestY) = (Math.Min(left.x, right.x), Math.Min(left.y, right.y));
53+
(int biggestX, int biggestY) = (Math.Max(left.x, right.x), Math.Max(left.y, right.y));
54+
(long dx, long dy) = (Math.Abs(left.x - right.x), Math.Abs(left.y - right.y));
55+
foreach (var row in EmptyRows)
56+
{
57+
for (int x = smallestX; x < biggestX; x++)
58+
{
59+
if (row == x)
60+
{
61+
dx += expansionLevel;
62+
break;
63+
}
64+
}
65+
}
66+
foreach (var col in EmptyCols)
67+
{
68+
for (int y = smallestY; y < biggestY; y++)
69+
{
70+
if (col == y)
71+
{
72+
dy += expansionLevel;
73+
break;
74+
}
75+
}
76+
}
77+
return dx + dy;
78+
}
79+
80+
public static bool IsLineEmpty(string l) => l.Select(c => c).ToHashSet().Count == 1;
81+
82+
public override ValueTask<string> Solve_1() => new(StarPairs.Sum(pairTuple => FindShortestPath(pairTuple.left, pairTuple.right, 1)).ToString());
83+
84+
public override ValueTask<string> Solve_2() => new(StarPairs.Sum(pairTuple => FindShortestPath(pairTuple.left, pairTuple.right, 1_000_000 - 1)).ToString());
1285
}

Utils/EnumerableUtils.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,24 @@ public static T Next<T>(this IEnumerator<T> enumerator)
4646
}
4747

4848
public static double Product<T>(this IEnumerable<T> values, Func<T, double> keySelector) => values.Aggregate(1d, (acc, el) => acc * keySelector(el));
49+
50+
public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> values, int start = 0)
51+
{
52+
foreach (T element in values)
53+
{
54+
yield return (start, element);
55+
start++;
56+
}
57+
yield break;
58+
}
59+
60+
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> values, int k)
61+
{
62+
return k == 0
63+
? EnumerableEx.Return(Enumerable.Empty<T>())
64+
: values.SelectMany((e, i) =>
65+
values.Skip(i + 1)
66+
.Combinations(k - 1)
67+
.Select(c => EnumerableEx.Return(e).Concat(c)));
68+
}
4969
}

0 commit comments

Comments
 (0)