-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
46 lines (37 loc) · 1.3 KB
/
Program.cs
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
using AdventOfCode.Common;
bool[,] map = Resources.GetInputFileLines()
.ParseAsArray(c => c == '#');
var rows = map.Height();
var cols = map.Width();
List<Coor<long>> stars =
[.. from row in Enumerable.Range(0, rows)
from col in Enumerable.Range(0, cols)
where map[row, col]
select new Coor<long>(row, col)];
var part1sum = stars
.AllCombinations(includeIdentities: false, orderSensitive: false)
.Select(p => p.Item1.ManhattanDistance(p.Item2))
.Sum() / 2;
var part2sum = part1sum;
var emptyRows = Enumerable.Range(0, cols)
.Where(row => stars.All(s => s.Row != row));
foreach (var row in emptyRows)
{
var starsTop = stars.Count(s => s.Row < row);
var starsBelow = stars.Count(s => s.Row > row);
var connections = Math.Abs(starsTop * starsBelow);
part1sum += connections;
part2sum += (1_000_000L - 1) * connections;
}
var emptyCols = Enumerable.Range(0, rows)
.Where(col => stars.All(s => s.Col != col));
foreach (var col in emptyCols)
{
var starsLeft = stars.Count(s => s.Col < col);
var starsRight = stars.Count(s => s.Col > col);
var connections = Math.Abs(starsLeft * starsRight);
part1sum += connections;
part2sum += (1_000_000L - 1) * connections;
}
Console.WriteLine($"Part 1: {part1sum}");
Console.WriteLine($"Part 2: {part2sum}");