Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var input = File.ReadAllLines("input.txt");
- var startingGalaxies = new List<(int Row, int Column)>();
- var emptyRows = new List<int>();
- for (var row = 0; row < input.Length; row++)
- {
- var line = input[row];
- if (!line.Contains('#'))
- {
- emptyRows.Add(row);
- }
- for (var col = 0; col < line.Length; col++)
- {
- var c = line[col];
- if (c == '#')
- {
- startingGalaxies.Add((row, col));
- }
- }
- }
- var emptyCols = new List<int>();
- for (var col = 0; col < input[0].Length; col++)
- {
- if (startingGalaxies.All(g => g.Column != col))
- {
- emptyCols.Add(col);
- }
- }
- Console.WriteLine($"Part 1: {CalculateDistances()}");
- Console.WriteLine($"Part 2: {CalculateDistances(1_000_000)}");
- return;
- long CalculateDistances(int factor = 2)
- {
- var galaxies = new List<(int Row, int Column)>(startingGalaxies);
- foreach (var emptyRow in emptyRows)
- {
- for (var i = 0; i < startingGalaxies.Count; i++)
- {
- if (startingGalaxies[i].Row > emptyRow)
- {
- galaxies[i] = (galaxies[i].Row + factor - 1, galaxies[i].Column);
- }
- }
- }
- foreach (var emptyCol in emptyCols)
- {
- for (var i = 0; i < startingGalaxies.Count; i++)
- {
- if (startingGalaxies[i].Column > emptyCol)
- {
- galaxies[i] = (galaxies[i].Row, galaxies[i].Column + factor - 1);
- }
- }
- }
- var result = 0L;
- for (var i = 0; i < galaxies.Count - 1; i++)
- {
- for (var j = i + 1; j < galaxies.Count; j++)
- {
- result += Math.Abs(galaxies[j].Row - galaxies[i].Row) + Math.Abs(galaxies[j].Column - galaxies[i].Column);
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement