diff --git a/AdvendOfCode.Runner/Program.cs b/AdvendOfCode.Runner/Program.cs index 062da71..149d813 100644 --- a/AdvendOfCode.Runner/Program.cs +++ b/AdvendOfCode.Runner/Program.cs @@ -7,9 +7,9 @@ InputReader inputReader = new() //IsDebug = true }; -inputReader.SetInputByChallange(5); +//inputReader.SetInputByChallange(5); -IChallange challange = new Day05(inputReader); +IChallange challange = new Day11(inputReader); Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}"); diff --git a/AdventOfCode.Solutions/2023/Day 11/Day11.cs b/AdventOfCode.Solutions/2023/Day 11/Day11.cs new file mode 100644 index 0000000..d379901 --- /dev/null +++ b/AdventOfCode.Solutions/2023/Day 11/Day11.cs @@ -0,0 +1,85 @@ +using AdventOfCode.Core; +using System.Text.RegularExpressions; + +namespace AdventOfCode.Solutions._2023 +{ + public class Day11(InputReader reader) : IChallange + { + private InputReader _inputReader = reader; + + public async Task GetSolutionPart1() + { + string[] data = await _inputReader.ReadAsArrayString(); + return CreateUniverseAndGetDistances(data).ToString(); + } + + public async Task GetSolutionPart2() + { + string[] data = await _inputReader.ReadAsArrayString(); + return CreateUniverseAndGetDistances(data, true).ToString(); + } + + private long CreateUniverseAndGetDistances(string[] lineInput, bool part2 = false) + { + List columns = []; // the list of indexes where expation takes place + for (int columnIndex = 0; columnIndex < lineInput[0].Length; columnIndex++) + { + if(lineInput.Select(line => line[columnIndex]).All(c => c == '.')) + { + columns.Add(columnIndex); + } + } + + List galaxies = []; + int rowsAdded = 0; // the rows where expation takes place + for (int rowIndex = 0; rowIndex < lineInput.Length; rowIndex++) + { + if (lineInput[rowIndex].All(c => c == '.')) + { + rowsAdded++; // add and continue + continue; + } + + foreach (Match galaxyMatch in Regex.Matches(lineInput[rowIndex], @"#")) + { + int columnExpantion = columns.Where(c => c < galaxyMatch.Index).Count(); + Galaxy newGalaxy = new(rowIndex, galaxyMatch.Index, columnExpantion, rowsAdded); + galaxies.Add(newGalaxy); + } + } + + long total = 0; + + for (int galaxiesIndex = 0; galaxiesIndex < galaxies.Count - 1; galaxiesIndex++) + { + total += galaxies + .Where(g => galaxies.IndexOf(g) > galaxiesIndex) // only take the next ones, skip all prev ones + .Select(g => + galaxies[galaxiesIndex].DistanceTo(g) + // get the distance + galaxies[galaxiesIndex].GetExpantionsDistance(g, part2) // get the expantion distance + ) + .Sum(); + } + + return total; + } + + private class Galaxy(int x, int y, int xExpantion, int yExpantion) : Node(x, y, '#') + { + public int HorizonalExpantions { get; set; } = xExpantion; + public int VerticalExpantions { get; set; } = yExpantion; + + public long GetExpantionsDistance(Galaxy other, bool part2 = false) + { + int times = Math.Abs(HorizonalExpantions - other.HorizonalExpantions) + Math.Abs(VerticalExpantions - other.VerticalExpantions); + + if (part2) + { + return (times * 1_000_000) - times; + } + + return times; + } + } + } +} \ No newline at end of file diff --git a/AdventOfCode.Solutions/2023/Day 11/day-11-input.txt b/AdventOfCode.Solutions/2023/Day 11/day-11-input.txt new file mode 100644 index 0000000..64c44c6 --- /dev/null +++ b/AdventOfCode.Solutions/2023/Day 11/day-11-input.txt @@ -0,0 +1,140 @@ +.#...........#.........................................................................#...............#...........#........................ +.......................#.................................................................................................................... +...............................#...........#.................#.............................#.............................................#.. +......#............................................#........................................................................................ +......................................................................#.....#.....................#......................................... +.................#.......#.......................................#................#.....#.....................#............................. +.........................................#.................#................................................................................ +................................#.............................................................#..........#.................................. +#.......................................................................#..............................................#.................#.. +..........#....................................#......................................#............................................#........ +................#....................................#........#............................................................................. +......#........................................................................#............................................................ +......................#..............#..............................................................#..........#.....#...................... +..................................................#.......................#..................................................#..........#... +............................#............................................................................................................... +........#................................................................................................................................... +..................#.......................#................................................................#................................ +.............................................................#........#..................................................................... +..............................#.......#......................................#.............#........................#....................... +.#............................................................................................................#...........................#. +...................................................................................................#....................#................... +....................#......................#.............#.....#..................................................................#......... +.................................................................................................................#.......................... +............................................................................................................................................ +....................................................................#.....................#................................................. +.................................#..............................................#..............#............#.............#................. +...........................#............................................#...........................................#....................... +......................#.........................#........................................................................................... +.......................................#....................................#...................................#........................... +.....#..........#.........................................#...............................................................................#. +#..........#......................#...................................................#.......#.....#....................................... +.........................#.................................................................................................................. +....................#..............................#.............#.........................................#.......#........#............... +...........................................#........................................................................................#....... +...........................................................#....................#........................................................... +..........#....................................................................................................#............................ +...............#................................#..........................#..............................................................#. +..#.................................#.................................................................#.......................#............. +.....................#......#...............#........................#................................................#..................... +..............................................................................................#....................................#........ +................................#.......#................................................................................................... +..........#................................................#...................#.....#..............#..........#..........#................. +.........................#.................................................................................................................. +..................#...............................#...........................................................................#............. +.........................................................................#............................................#..................... +........#.....#..............................................................................#.........#..................................#. +.#...............................#......#............#.............................................................................#........ +....................................................................................#.............#...............#........#................ +...........................#................................................................................................................ +......................#..................................#..............#................................#.............#................#... +...............................................................#..............#............................................................. +..................................#........................................................#........#....................................... +......#.................................#.......#....................#...............................................................#...... +.................#............#..............................................................................#.............................# +............................................................................................................................................ +..#.....................#.............................................................................#..................................... +.......................................................................................#..............................#..................... +...............................................................................#.............#.............................................. +....................#.....................#...............................................................#................................. +..........#........................#............#...................#.............................................#...............#......... +............................#.............................#..........................#...................................................... +...........................................................................................#................................................ +.................................................................#.................................#.........................#........#..... +.................#........................................................#..................................#.............................. +......................................#......................#.............................................................................. +.............#...........#...............................................................................#..........#....................... +.................................#...........#...........................................................................#..............#... +.......#............................................#.....#....................#..............#.....#....................................... +...................#...........................................#................................................#........................... +........................................................................................#................................................... +...#....................#.....#............#.........................................................................................#...... +...............#.............................................................#..................#........#................#................. +.........#.....................................#............................................................................................ +................................................................................................................................#........... +.....................#.............#.........................................................................#.............................. +.....#...............................................................................................................#..............#......# +...........#.......................................#.....#.................#.................#.............................................. +............................................................................................................................................ +.#.......................................#..........................................#................#...................................... +......................#.............#.................#.......................#...........#.............................#................... +...............#.............#................................#......#.......................................................#.............. +............................................................................................................................................ +...#............................................................................................#....................#...................... +..........#.....................#.............................................................................#............................. +...........................#...............................................................................................#...............# +.....................#......................................#.......#......#........#..................#...........................#........ +.....#...............................#............#......................................................................................... +............................................................................................................................................ +.........................................................#........................................................#......................... +#..............................#..........#..............................................................................#................#. +....................#..............................................#.......................#.........#...................................... +............................................................................................................................................ +...........#.......................................#.............................................................................#.......... +...#......................#...............................................#......................#.......................................... +..................#......................................................................................................................... +...........................................................#........#...........#.....#......#........................#.................#... +........#................................................................................................................................... +....................................................................................................#......#................................ +................................#...........#....................#.....#...................................................#........#....... +........................#............#......................................#.............#................................................. +...................#............................................................................................#........................... +#................................................................................................................................#.......... +..............#.............................................#............................................................................... +..............................#...............................................#.................#......................................#.... +....#....................#......................................#...................#...............................#.......#............... +........................................#..........#........................................#................#.............................. +............................................................................................................................................ +............................................................................................................................................ +........................................................................................................................................#... +...........................................................#..................#.................#.................#......................... +.......#........................................................#................................................................#.......... +..........................#......#......#..................................................................................#................ +...............................................#.....#.................#.................................................................... +......................................................................................................#..................................... +................#....................#.....................................................................#........#....................... +..............................................................................................................................#............. +...........................................#.............................................................................................#.. +....#........................#..............................#.............#................#................................................ +......................#..........................................................................................#......#................... +....................................................#................................#.........#............................................ +..........................#.......................................#......................................................................... +.................#........................#.....#........................................................#.................................. +.#.........................................................#............#....................................................#.............. +..........#..................#.....#..................................................................................................#..... +.................................................................................#.......................................................... +.....................................................#....................................#.........#...........#.....#..................... +.............................................#.....................#.......................................#.......................#........ +................................#........................................................................................................... +......#........................................................#..............................#............................................. +............#......................................#.....#............................#........................................#............ +............................................................................#............................................................... +.......................................#...............................#...................#..............................#...........#..... +.......................#..............................#..........................#..............................#........................... +.................................................................#...................................#...................................... +......................................................................................................................#...................#. +.................#.......................................................................................................................... +.........#......................#.....#...........#.......................................................#................................. +...........................................#.................#................#.....................................................#....... +..#...............................................................#......................................................................... +.....................#......#......#.............................................................#............#...........#................. \ No newline at end of file diff --git a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj index 5b0ba7c..abf309d 100644 --- a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj +++ b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj @@ -12,6 +12,9 @@ + + Always + Always diff --git a/AdventOfCode.Solutions/day-00-input.txt b/AdventOfCode.Solutions/day-00-input.txt index bd902a4..a0bda53 100644 --- a/AdventOfCode.Solutions/day-00-input.txt +++ b/AdventOfCode.Solutions/day-00-input.txt @@ -1,33 +1,10 @@ -seeds: 79 14 55 13 - -seed-to-soil map: -50 98 2 -52 50 48 - -soil-to-fertilizer map: -0 15 37 -37 52 2 -39 0 15 - -fertilizer-to-water map: -49 53 8 -0 11 42 -42 0 7 -57 7 4 - -water-to-light map: -88 18 7 -18 25 70 - -light-to-temperature map: -45 77 23 -81 45 19 -68 64 13 - -temperature-to-humidity map: -0 69 1 -1 0 69 - -humidity-to-location map: -60 56 37 -56 93 4 \ No newline at end of file +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file