From edc24dcb657fbf93605fac26d2e227830405fa81 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Thu, 14 Dec 2023 02:57:59 +0100 Subject: [PATCH] AdventOfCode 2023 Day11 --- AdventOfCode2023.Tests/Day11_Tests.cs | 28 ++++ AdventOfCode2023/Day11.cs | 212 ++++++++++++++++++++++++++ AdventOfCode2023/inputs/Day11.txt | 140 +++++++++++++++++ 3 files changed, 380 insertions(+) create mode 100644 AdventOfCode2023.Tests/Day11_Tests.cs create mode 100644 AdventOfCode2023/Day11.cs create mode 100644 AdventOfCode2023/inputs/Day11.txt diff --git a/AdventOfCode2023.Tests/Day11_Tests.cs b/AdventOfCode2023.Tests/Day11_Tests.cs new file mode 100644 index 0000000..ce53ea9 --- /dev/null +++ b/AdventOfCode2023.Tests/Day11_Tests.cs @@ -0,0 +1,28 @@ +namespace AdventOfCode2023.Tests; + +public class Day11_Tests +{ + private readonly string[] _example1 = { + "...#......", + ".......#..", + "#.........", + "..........", + "......#...", + ".#........", + ".........#", + "..........", + ".......#..", + "#...#.....", + }; + + [Fact] + public void ResolvePart1__Example1() + { + Day11 day = new(); + + string result = day.ResolvePart1(_example1); + + Assert.Equal("374", result); + } + +} \ No newline at end of file diff --git a/AdventOfCode2023/Day11.cs b/AdventOfCode2023/Day11.cs new file mode 100644 index 0000000..df0b2f3 --- /dev/null +++ b/AdventOfCode2023/Day11.cs @@ -0,0 +1,212 @@ +namespace AdventOfCode2023; + +/* +--- Day 11: Cosmic Expansion --- + +You continue following signs for "Hot Springs" and eventually come across an observatory. The Elf within turns out to be a researcher studying cosmic expansion using the giant telescope here. + +He doesn't know anything about the missing machine parts; he's only visiting for this research project. However, he confirms that the hot springs are the next-closest area likely to have people; he'll even take you straight there once he's done with today's observation analysis. + +Maybe you can help him with the analysis to speed things up? + +The researcher has collected a bunch of data and compiled the data into a single giant image (your puzzle input). The image includes empty space (.) and galaxies (#). For example: + +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... + +The researcher is trying to figure out the sum of the lengths of the shortest path between every pair of galaxies. However, there's a catch: the universe expanded in the time it took the light from those galaxies to reach the observatory. + +Due to something involving gravitational effects, only some space expands. In fact, the result is that any rows or columns that contain no galaxies should all actually be twice as big. + +In the above example, three columns and two rows contain no galaxies: + + v v v + ...#...... + .......#.. + #......... +>..........< + ......#... + .#........ + .........# +>..........< + .......#.. + #...#..... + ^ ^ ^ + +These rows and columns need to be twice as big; the result of cosmic expansion therefore looks like this: + +....#........ +.........#... +#............ +............. +............. +........#.... +.#........... +............# +............. +............. +.........#... +#....#....... + +Equipped with this expanded universe, the shortest path between every pair of galaxies can be found. It can help to assign every galaxy a unique number: + +....1........ +.........2... +3............ +............. +............. +........4.... +.5........... +............6 +............. +............. +.........7... +8....9....... + +In these 9 galaxies, there are 36 pairs. Only count each pair once; order within the pair doesn't matter. For each pair, find any shortest path between the two galaxies using only steps that move up, down, left, or right exactly one . or # at a time. (The shortest path between two galaxies is allowed to pass through another galaxy.) + +For example, here is one of the shortest paths between galaxies 5 and 9: + +....1........ +.........2... +3............ +............. +............. +........4.... +.5........... +.##.........6 +..##......... +...##........ +....##...7... +8....9....... + +This path has length 9 because it takes a minimum of nine steps to get from galaxy 5 to galaxy 9 (the eight locations marked # plus the step onto galaxy 9 itself). Here are some other example shortest path lengths: + + Between galaxy 1 and galaxy 7: 15 + Between galaxy 3 and galaxy 6: 17 + Between galaxy 8 and galaxy 9: 5 + +In this example, after expanding the universe, the sum of the shortest path between all 36 pairs of galaxies is 374. + +Expand the universe, then find the length of the shortest path between every pair of galaxies. What is the sum of these lengths? + +--- Part Two --- + +The galaxies are much older (and thus much farther apart) than the researcher initially estimated. + +Now, instead of the expansion you did before, make each empty row or column one million times larger. That is, each empty row should be replaced with 1000000 empty rows, and each empty column should be replaced with 1000000 empty columns. + +(In the example above, if each empty row or column were merely 10 times larger, the sum of the shortest paths between every pair of galaxies would be 1030. If each empty row or column were merely 100 times larger, the sum of the shortest paths between every pair of galaxies would be 8410. However, your universe will need to expand far beyond these values.) + +Starting with the same initial image, expand the universe according to these new rules, then find the length of the shortest path between every pair of galaxies. What is the sum of these lengths? + +*/ + +public class Day11 : IDay +{ + public string ResolvePart1(string[] inputs) + { + GalaxyMap map = new(inputs); + map.Expand(1); + long sum = map.SumMinDistances(); + return sum.ToString(); + } + + public string ResolvePart2(string[] inputs) + { + GalaxyMap map = new(inputs); + map.Expand(1000000 - 1); + long sum = map.SumMinDistances(); + return sum.ToString(); + } + + private struct Point + { + public long X { get; set; } + public long Y { get; set; } + } + + private class GalaxyMap + { + private readonly List _galaxies = new(); + + private readonly List _horizontalGaps = new(); + private readonly List _verticalGaps = new(); + + private readonly List _galaxiesExpanded = new(); + + public GalaxyMap(string[] map) + { + for (int j = 0; j < map.Length; j++) + { + bool rowWithGalaxy = false; + for (int i = 0; i < map[j].Length; i++) + { + if (map[j][i] != '#') { continue; } + + _galaxies.Add(new Point { X = i, Y = j, }); + _galaxiesExpanded.Add(new Point { X = i, Y = j, }); + rowWithGalaxy = true; + } + if (rowWithGalaxy == false) + { + _horizontalGaps.Add(j); + } + } + + for (int i = 0; i < map[0].Length; i++) + { + bool columnWithGalaxy = false; + foreach (string mapRow in map) + { + if (mapRow[i] == '#') + { + columnWithGalaxy = true; + } + } + if (columnWithGalaxy == false) + { + _verticalGaps.Add(i); + } + } + } + + public void Expand(long expand) + { + _galaxiesExpanded.Clear(); + foreach (Point galaxy in _galaxies) + { + int verticalGaps = _verticalGaps.Count(vg => vg < galaxy.X); + int horizontalGaps = _horizontalGaps.Count(hg => hg < galaxy.Y); + _galaxiesExpanded.Add(new Point { + X = galaxy.X + (verticalGaps * expand), + Y = galaxy.Y + (horizontalGaps * expand), + }); + } + } + + public long SumMinDistances() + { + long sum = 0; + for (int i = 0; i < _galaxiesExpanded.Count; i++) + { + for (int j = i + 1; j < _galaxiesExpanded.Count; j++) + { + Point a = _galaxiesExpanded[i]; + Point b = _galaxiesExpanded[j]; + long distance = Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y); + sum += distance; + } + } + return sum; + } + } +} \ No newline at end of file diff --git a/AdventOfCode2023/inputs/Day11.txt b/AdventOfCode2023/inputs/Day11.txt new file mode 100644 index 0000000..dc1e9a0 --- /dev/null +++ b/AdventOfCode2023/inputs/Day11.txt @@ -0,0 +1,140 @@ +...#...................#.......................#.......................................................#.................................... +........................................................#................................................................................... +...............................#..........#..........................#.....#......................#......................................... +......#.................................................................................#.............................#..............#...... +...........................................................................................................................................# +.................#........#.........................#...........#................#....................#......#.............................. +........................................#................#....................................#..............................#.............. +......................#..................................................................................................................... +..#.........#.......................#..............................#...............................................................#........ +.............................................................#..................................................#........................... +...................#...........#...................#....................#...............................................#................... +...........................................#...................................#.....................#......#............................... +....#.........................................................................................................................#............. +........................................................................................#..........................#........................ +...........#........................#..........................#............#............................................................... +.#...........................#.............................................................................................................. +...............#..........................#................#.......................#.......................#................................ +.....................#...........................................................................................#.......................... +.........#.......................................#..................#..........#............#..........#..................#................. +.......................................................................................................................................#.... +....................................#........................#..........................#................................................... +.....#.....................................#................................................................................................ +...............#......................................#......................................................#....................#......... +....................#................................................................#.......#...............................#.............. +...........#................................................................................................................................ +.........................#......................#........................................................................................... +...#..............................#....................................#...........................#........................................ +..................................................................#..................................................................#...... +..........................................#..........#......................#............................................#.................. +...............#......................................................................#..........................#........................#. +.....................#...................................................................................................................... +..................................................#.............#.................................#......................................... +#..........................#.........................................................................................#...................... +.....................................#......#................................#..............#...............#..................#............ +........#............................................................................................#................................#..... +................................................................................................................#........................... +...............................#.......................#..................#................................................#................ +..................................................................................................#...................#....................# +............................................................................................................................................ +................#..........#....................#..................#..........................................#............................. +........#.................................#........................................#.....#.....................................#............ +.......................#............................................................................................#....................... +.........................................................................................................................#.........#........ +........................................................................................................#................................... +....#.......#...............#........................................#...................................................................... +...........................................#.....#.............................................................#..............#.........#... +............................................................................................................................................ +.....................#................#..................#....................#..............#.............................................. +......#..................................................................................................................................... +...............#......................................................................................#.........................#........... +........................#...........................#..........#......................................................#..................... +.........................................#...........................#.................#......................#............................. +............................................................................................................................................ +............................#.............................#.................................#..............................#...........#.... +.#...........#......................#.........#...........................#................................................................. +............................................................................................................................................ +.................#..................................................................#...........#.........#......................#.......... +............................................................................................................................................ +............................................................................................................................................ +.........#...................#.................................#.............#.............................................................. +...............#....................................................#...............................................#.....#................. +..........................................#..............................................#...........#.......#.............................. +..#..................................................................................................................................#...... +..........................................................................#.........#....................................................... +....................................#.........................#.........................................................#................... +...............................#................................................................#........................................... +...........#.............................................#............................................#............#........................ +...........................................................................................................................#............#... +................#...........................#......................#..........#.......#..................................................... +#......................................................................................................................#............#....... +........................................................................#..................................#................................ +..............................................................#...........................#.....#.........................................#. +......#.......#.....#.............#......................................................................................................... +.............................#.........#..................#................................................................................. +..........#.........................................................................................................#....................... +......................................................#................................................#...................#................ +....#....................#.........................................#.........................................#.............................. +....................................#.........#...............#...............#..............#...................................#.......... +............................................................................................................................................ +.............................#..................................................................................#.......#................... +........................................#................#...............#.................................................................. +............................................................................................................................................ +.............................................................#.........................#................#................................... +..........#......#............................#..................................#...............#..............................#.........#. +..................................#.................................................................................#....................... +....................................................#.......................#...............#.................#.......................#..... +.#...........#........................#..............................................#...................................................... +................................................................#.......#..............................#.................................... +..................................................................................................#...........................#............. +............................................................#............................................................#..............#... +......#.....................#......................................#...........#............................................................ +.....................................................#.............................................................#.............#.......... +.......................#.............#.....................................................................................................# +...........#.................................#............................#...........................#..................................... +.#...............................#....................................................................................#..................... +...................#...................................#..........................................#..............#.......................... +.........................#.......................................#.............#............................................................ +..........................................#.......#.....................#................#..................................#............... +...#.......................................................................................................#............................#... +............#.............................................#..................................#...................................#.......... +............................#..........#.............................................................#.............#........................ +......................#..................................................................................................................... +.............................................................................................................#.............................. +....#........................................#.....................................#.......................................#................ +.....................................................#..............#...................................................................#... +#....................................#....................#.......................................................................#......... +................................................................................................#........................................... +...........#.........#....................#...............................#.............#................................#.................. +............................................................................................................................................ +......#..........................#...........................#...........................................#...........#...................... +.#.............#.........#.........................................#...........#.....#..................................................#... +.....................................#...................#.....................................................................#............ +...........................................................................................#................................................ +...................#................................................................................#..............#........................ +...........................................#......#........................................................................................# +..#........................#..................................#.........................#................................................... +........#.............................#...............................................................................#..................... +...............................#........................#...............#..............................#.................................... +.................................................................#..........................................#............................... +....................................................#........................#........#....................................#.........#...... +....#.........#....................................................................................#.......................................# +......................#.....#.............................................................#................................................. +#......................................#......#....................................#...............................#.............#.......... +.......................................................#..................................................#................................. +.........................................................................#......................#........................................... +...............................................................................#............................................#............... +....#........#................#...............................#.......................................................................#..... +.......................#.................................................................#..................#............................... +.....................................#.............#........................................................................................ +...................................................................................................#......................................#. +..#......................................................................................................................................... +........#.................................#.............#...........#................#...................................................... +...............................................................................................#............................#............... +.................#....................#.........................#..................................................................#........ +....#.............................................#........................................................#........#....................... +.........................#.............................................#............................#....................................... +......................................................#.................................#................................................#.. +.............................#...............#.................................#............................................................ +.........#....................................................................................#........................#........#........... +..................#...........................................#.................................................#........................... \ No newline at end of file