diff --git a/Day11_2/Day11_2.csproj b/Day11_2/Day11_2.csproj new file mode 100644 index 0000000..051c6b3 --- /dev/null +++ b/Day11_2/Day11_2.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + PreserveNewest + + + + + + + + diff --git a/Day11_2/Program.cs b/Day11_2/Program.cs new file mode 100644 index 0000000..fca56b3 --- /dev/null +++ b/Day11_2/Program.cs @@ -0,0 +1,82 @@ +using System.Drawing; + +var input = File.ReadAllLines("input.txt"); + +var width = input[0].Length; +var height = input.Length; +var map = new char[width, height]; +var gallaxies = new List(); + +for (int x = 0; x < input[0].Length; x++) +{ + for (int y = 0; y < input.Length; y++) + { + map[x, y] = input[y][x]; + if (map[x, y] == '#') + { + gallaxies.Add(new Point(x, y)); + } + } +} + +var emptyRows = new List(); +var emptyColumns = new List(); + +for (int column = 0; column < width; column++) +{ + var isEmpty = true; + for (int row = 0; row < height; row++) + { + if (map[column, row] != '.') + { + isEmpty = false; + break; + } + } + + if (isEmpty) + { + emptyColumns.Add(column); + } +} + +for (int row = 0; row < height; row++) +{ + var isEmpty = true; + for (int column = 0; column < width; column++) + { + if (map[column, row] != '.') + { + isEmpty = false; + break; + } + } + + if (isEmpty) + { + emptyRows.Add(row); + } +} + +long totalDistance = 0; +foreach (var galaxy in gallaxies) +{ + foreach (var otherGalaxy in gallaxies) + { + if (galaxy == otherGalaxy) + { + continue; + } + + var distance = Math.Abs(galaxy.X - otherGalaxy.X) + Math.Abs(galaxy.Y - otherGalaxy.Y); + + // Number of empty rows/columns between the two galaxies + var emptyRowsBetween = emptyRows.Count(row => Math.Min(galaxy.Y, otherGalaxy.Y) < row && row < Math.Max(galaxy.Y, otherGalaxy.Y)); + var emptyColumnsBetween = emptyColumns.Count(column => Math.Min(galaxy.X, otherGalaxy.X) < column && column < Math.Max(galaxy.X, otherGalaxy.X)); + + distance += (emptyRowsBetween + emptyColumnsBetween) * 999999; + totalDistance += distance; + } +} + +Console.WriteLine(totalDistance / 2); \ No newline at end of file diff --git a/Day11_2/Properties/launchSettings.json b/Day11_2/Properties/launchSettings.json new file mode 100644 index 0000000..458e04e --- /dev/null +++ b/Day11_2/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "Run": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/Day11_2/input.txt b/Day11_2/input.txt new file mode 100644 index 0000000..21b77be --- /dev/null +++ b/Day11_2/input.txt @@ -0,0 +1,140 @@ +.....#...............#......................................................................#.............................#..........#...... +........................................#............................#......#............................................................... +..............................#..............#....................................#..............................................#.......... +..............#........................................................................#.................................................... +..#...........................................................................................#............................................. +..................#.....................................#.....#......................................#.........#.......#.................... +...........#......................................#.....................#...........#................................................#...... +.............................#............................................................#.....................................#........... +.....#..................#.................#.........................#......................................#................................ +....................................#.................#........................#..........................................................#. +................#..............................#....................................................#....................................... +..............................................................#.........................#.......................#.....................#..... +............#...............#......................#.....................#..............................#................................... +......................................#...............................................................................#..................... +.........................................................#.................................................................................. +..............................................#............................................................................................. +..#.................#.....................................................................#.....#..........#..............#......#.......... +..............................................................................#............................................................. +..........................................#.........................#..............................................#........................ +.....#........#.............#...........................#...........................................#....................................... +..................................#...............#.............#.................#..........................#.....................#........ +.......................................................................#.................................................................... +...............................................................................................................................#.........#.. +#.......................................#....................................................#.........................#.................... +........#...............#....................................#...........................................#.................................. +...............................#............................................................................................................ +.........................................................#.................................................................................. +..#..........................................#........................................#.............#............................#.......... +......................#............................#.................#........#............................................................. +.........#.......#...........................................................................#.....................#.........#.........#.... +................................#............................................................................#.............................. +.........................................................................................#.................................................. +.#.........................................#.........#..........#.......................................................#................... +....................#.................#...............................................................#..................................... +............................................................................#............................................................... +........................#.........................................................................................#.................#....... +.............#.......................................................#......................................................#............... +.......#.................................#.....#..............................................................#............................. +#...............................#...............................................................#.....................#..................... +.....................................#...................#.......................#.....#.......................................#............ +..........................#.................#......#...................................................................................#.... +..............#............................................................................................................................. +...#..........................................................................#............................................#................ +.............................................................................................#.....................#......................#. +.......................................#..........................................................................................#......... +.......................................................................................................#.................................... +.....#.........#.................#.................#............#.........................#.................#............................... +............................#.....................................................#......................................................... +...........................................#................................................................................................ +....................................#.......................#.........................#............................#.....#.................. +............................................................................................................................................ +.....................#..................#................................#......#.....................................................#..... +.......................................................#.........#...............................#.....#.................................... +............#.................................................................................................................#............. +..............................................#..............#.......#.................................................#.................... +................................................................................................................#........................... +.........#.............................................................................#............#....................................... +.............................#...........................................#.....#............................................................ +.....#..........#.....#...........#.............#.....#.............................................................................#....... +...........................................#............................................................#.....#............................. +......................................#.........................#.........................#.....................................#........... +............................................................................................................................................ +.......#.....#.....#........................................#.................................#............................................. +.........................................#................................................................#.....#......#.................... +........................#.......#..................#.................................#.......................................#.......#...... +..............................................#............................................................................................. +...............................................................................#............................................................ +......................................................#..........#.......................................................................... +...#......#.................#.............#.............................#..........................#...............................#........ +.............................................................#........................#..........................#.........#................ +......................................#....................................................#.............................................#.. +............................................................................................................................................ +...............#......#.......................#.....#.......................................................#............................... +.#......#.......................#.........................#.......#....................................#.................................... +...............................................................................................#........................#......#.......#.... +............#...............#............#....................#........#..........#......................................................... +...............................................................................................................#............................ +..........................................................................................................#.................#............... +......................#........#.........................................................................................................#.. +....................................................................................#.....#.....#....................#...................... +.#.........#...............#........................................#...............................................................#....... +..................#......................................................#..........................#....................................... +.................................#.............#................................................................#.......#................... +........................#......................................................#..............#................................#............ +.....#..........................................................#.....#.................#..................................................# +.....................................#......#.........#...............................................#..................................... +............................................................................................................................................ +............................................................................................................................................ +.................................#..........................................#................#...................#..............#........... +.......#....................#............#.............................................#...................................#................ +..#................#........................................#.......#..............................#........................................ +........................#...........................................................................................................#....... +...........#..........................................#.........................#......................#.................................... +...........................................#................................................................................................ +......#...............................................................................#.........#........................................... +............................#.....................#................#........................................#...........#................... +..................#.........................................#............................................................................... +........................#...............#................................#.......#..............................#............#.............. +............#......................#.................#..................................................#..................................# +..#................................................................................................#........................................ +.........................................................................................................................#.................. +..................................................................#................................................#................#....... +.......#.....................................#.............................................................................................. +.................#...............#......#.....................#............................................................................. +....................................................................................#..................................#.................... +..............................................................................................#............#................................ +....#......................#................................................................................................................ +......................................................#.................................#..........................#.............#.......... +..........#.................................................................................................................#............... +.................................#..........#..................................#............................................................ +.......................................................................................................................................#.... +.......................................#..................................................................#......#.......................... +.............................#.........................................................#.......#............................................ +.........#....................................#............#.................................................................#.............. +...#...............................................................................#.................#...................................... +..................................................................................................................................#......... +....................#.............#...................................................................................#....................# +....................................................#...........#...........#............................................................... +.........................................................................................................#.................................. +..........#...............................#............................#.................................................................... +.............................................................#....................#.............................#..............#............ +.#...............#..........................................................................#..........................................#.... +............................#............................................................................................................... +............#......................................#............#..................................................................#........ +.......#...............................................................................................................#.................... +..................................#.......#..........................#........#...............#..........#....................#............. +....................#....................................#............................#..........................#.......................#.. +..............................................................#............................................................................. +..#............................#............................................................................#............................... +..............#.....................#...............................................................................................#....... +..........................#........................................#....................................#...........#....................... +..........................................#.................................#.................#............................................. +................................................#...........#..........#.....................................................#.............. +.........#.................................................................................................................................. +...................................................................................#............................#.......#................... +....#.............................#.............................#.......................................................................#... +.........................................#.........................................................#........................................ +....................#...........................................................................................................#........... +..............#........................................#..................................#...................#......................#...... +.........#.................#........................................#...........#........................................#.................. \ No newline at end of file diff --git a/Day11_2/output.txt b/Day11_2/output.txt new file mode 100644 index 0000000..e69de29