From 97def7eb2d3f59854a6c282cbad61437d81e8013 Mon Sep 17 00:00:00 2001 From: deckelmouck Date: Mon, 11 Dec 2023 12:54:56 +0000 Subject: [PATCH 1/3] solving day11 part 1 without expansion --- 2023/Day11/input | 140 +++++++++++++++++++++++++++++++++++++++++ 2023/Day11/solution.cs | 89 ++++++++++++++++++++++++++ 2023/Day11/test | 10 +++ 3 files changed, 239 insertions(+) create mode 100644 2023/Day11/input create mode 100644 2023/Day11/solution.cs create mode 100644 2023/Day11/test diff --git a/2023/Day11/input b/2023/Day11/input new file mode 100644 index 0000000..c97ea81 --- /dev/null +++ b/2023/Day11/input @@ -0,0 +1,140 @@ +.......#..........................................................................#......................................................... +....................#.........#............#.........................#.......................#.............................................. +.#.......................................................#.................#.........................................................#...... +..............#.................................#................#....................................................#..................... +..................................#......................................................................#.................................. +..................#..................................#.............................#.......#...............................#................ +.......#...............................#...................#................................................................................ +........................................................................#....................................#........................#..... +..#...........................................................................................................................#............. +............#............#..............................................................................#................................... +...........................................#.....................................................#................#......................... +.....................................................................#...........................................................#.......... +...............#....................#....................................................................................................... +#.....................................................#.......................................#............................#..........#..... +.....#................#......#................................................#............................................................. +.......................................#.......................#.......#..............#..................#......#........................... +............................................................................................................................................ +...................................................#..............................................#.............................#........#.. +..........#........................................................................................................#........................ +...............................................#.............................#............#...........#..................................... +....................#..........................................................................................#........#................... +....#.........#............#...............................................................................................................# +....................................#................................#.............#............#........................................... +...............................#..................................................................................#......................... +#...............................................#........#.....................................................................#............ +......#........................................................#......................................................#..................... +..........................................#....................................#............................................................ +..................#.....................................................................#..................................#.......#........ +.............................................................................................#.............................................. +.............#........#...........#..........................#............................................#.......#......................... +........#...................#.......................#...................#..................................................................# +.................................................................................#.......................................#.................. +................................................................#........................................................................... +...............................................#...........#..........................................#.....#............................... +..........................................#....................................................................................#............ +.......#.....#............................................................................#.............................................#... +......................#....................................................#....................................#........................... +.................#...................#................#....................................................................#................ +..#...............................................................#...................#.............................#....................... +.............................................................................................#.............................................. +.........#..........#....................................................................................................................... +...............................#.........#.......#...............................#.........................................................# +......................................................................................................#...................#.....#........... +........................................................................................#..................#................................ +..#......................................................................................................................................... +....................................................................................................................#...................#... +......................#..............................#..........#........................................................................... +......................................................................#..........................................................#.......... +#.......................................#......................................................................#............................ +.....#..........................#........................................................................................................... +..................#...............................#............................#........................#.................#................. +..........................................................#.................................#............................................... +.......................#..............................................................................................#...........#.....#... +............................................................................................................................................ +.......#............................................#....................................................................................... +...............#................#......#......................#......................................#...................................... +.....................................................................................#...................................................... +............................................................................................................................................ +............................................................................................................................................ +...#........................................................#...........................#.....#.........................#.........#......... +............................................................................................................................................ +...........#..........#......#................................................................................#.............#............... +.......................................#.................................................................................................#.. +...............................................#...............#............................................................................ +.............................................................................#.....................................#........................ +....#..............#..................................................#............#........................................................ +..............................#......................#..........................................#.....................................#..... +..............#..........................#...........................................................#...................................... +................................................#..............................#..............................#..........#.......#.......... +............................................................#............................................................................... +.#........................#................................................................................................................. +...............................#...........................................................#................................................ +.............#........................................#........#..................................................#......................... +...........................................#...........................................................#...................#................ +.........#.......................................#............................#.......#...................................................#. +.....................................................................#.............................................................#........ +...........................................................#................................................................................ +.....................................................#......................................#.......#..............#...........#............ +............................................................................................................................................ +#..........#.....................#..............................................................#........................#.................. +...........................#...................#...............................#.....#..................................................#... +................................................................#........................................................................... +.......................................#..................#.......................................................................#......... +...........................................................................................................#................................ +......#...............................................#......................#.................#.......................#.................... +...............................#..................................#.............................................#....................#...... +...........#......................................................................#................#............................#........... +.........................#.........#...............#........................................................................................ +.#.........................................#...........................#..................................................................#. +....................#.........................................#.............#...........................................#................... +............................#...................................................................#............................#.............. +............................................................................................................................................ +.............#..............................................................................#...............#.........................#..... +#......#..........................#...............................................................................#......................... +...............................................#.......................#..................................................................#. +.....................#...................................................................................................................... +............................................................#......................#.........................................#......#....... +.........................#............................#................................................................#.................... +..#..............................#...............................#.......................................................................... +.......................................................................................#.............#..........#........................... +.............#.........................#.................................................................................................... +..................................................#...............................#.........................................#............... +...................#..........................................................................#............................................. +.#....................................................#.......#..........................................#......................#.....#..... +...............................#............................................................................................................ +..............#....................................................#..........#....................#......................................#. +.........................................................#..............................#..........................#........................ +.....#.........................................................................................#.............#..........#................... +..................................#................................................................................................#........ +#........................#.................................................................................................................. +....................#.......................#................................#.............................................................. +...........#.......................................................................#...................#......................#............. +..............................................................#...................................................#......................... +...............................#..................#............................................#............................................ +................#.........#.............#............................#....................#................#................................ +.#.......................................................................................................................................... +........#................................................................................................................................... +..................................#...................................................#.............................................#....... +.................................................................................#.......................................................... +.............................#..........................#.....................................#....................#.......................# +........................................#..........................#........................................#...........#.....#............. +......................................................................................................#..................................... +..........#......#.......#....................................................#......................................................#...... +.....#...............................................#.................................#........#........................................... +#...........................................#...................................................................................#........... +..............................#..............................#.....................#.......#................................................ +......................#.........................#.........................................................................................#. +.......#...........................#.......................................#...............................#............#................... +............#.............................................#........................................#........................................ +.....................................................#..............#................#...................................................... +............................................................................................................................................ +.........................#.................................................................#............#.....................#............. +..................#..........................................................#..................................#......#..................#. +.#.................................................#..........#.........#.........#......................................................... +.......#.........................#.......#..............#..........................................#........................................ +.....................#.....#..............................................................................#................................. +...........................................................................#.....................................................#.......... +......................................#.........................................................#........................................#.. +#...............#...................................#.....................................#............#.................#.................. +.............................................#.........................#..........#.............................#........................... \ No newline at end of file diff --git a/2023/Day11/solution.cs b/2023/Day11/solution.cs new file mode 100644 index 0000000..c2c90ee --- /dev/null +++ b/2023/Day11/solution.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using adventofcode; + +namespace aoc2023; + +public class solutionDay11 : ISolver +{ + public void SolvePart1() + { + Console.WriteLine($"part 1"); + + solutionBase sb = new(); + //var input = sb.getInputLines(@"2023/Day11/input").ToArray(); + var input = sb.getInputLines(@"2023/Day11/test").ToArray(); + + int width = input[0].Length; + int height = input.Length; + + char[,] grid = new char[width, height]; + + for (int y = 0; y < height; y++) + { + var line = input[y]; + for (int x = 0; x < width; x++) + { + grid[x, y] = line[x]; + } + } + + List galaxies = new(); + + for (int y = 0; y < height; y++) + { + var line = input[y]; + for (int x = 0; x < width; x++) + { + if (grid[x, y] == '#') + { + Galaxy galaxy = new Galaxy(galaxies.Count, x, y); + galaxies.Add(galaxy); + } + } + } + + Console.WriteLine($"Galaxies: {galaxies.Count}"); + + List<(Galaxy, Galaxy)> connections = new(); + + for(int i = 0; i < galaxies.Count; i++) + { + for(int j = i + 1; j < galaxies.Count; j++) + { + var galaxy1 = galaxies[i]; + var galaxy2 = galaxies[j]; + + connections.Add((galaxy1, galaxy2)); + } + } + + Console.WriteLine($"Connections: {connections.Count}"); + + foreach(var connection in connections) + { + var galaxy1 = connection.Item1; + var galaxy2 = connection.Item2; + + //calculate manhatten distance between galaxies + int distance = Math.Abs(galaxy1.X - galaxy2.X) + Math.Abs(galaxy1.Y - galaxy2.Y); + + Console.WriteLine($"Distance between {galaxy1.Id} and {galaxy2.Id} is {distance}"); + } + + } + + public void SolvePart2() + { + Console.WriteLine($"part 2 under development"); + } + + class Galaxy (int id, int x, int y) + { + public int Id { get; set; } = id; + public int X { get; set; } = x; + public int Y { get; set; } = y; + } + + +} \ No newline at end of file diff --git a/2023/Day11/test b/2023/Day11/test new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/2023/Day11/test @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file From c949f0809bf5b5ee4deba4ac76294bf8abde54a1 Mon Sep 17 00:00:00 2001 From: deckelmouck Date: Tue, 12 Dec 2023 08:18:43 +0000 Subject: [PATCH 2/3] solved day11 part 1, part 2 still not right --- 2023/Day11/solution.cs | 92 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 8 deletions(-) diff --git a/2023/Day11/solution.cs b/2023/Day11/solution.cs index c2c90ee..bb15e92 100644 --- a/2023/Day11/solution.cs +++ b/2023/Day11/solution.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using adventofcode; namespace aoc2023; @@ -30,10 +31,10 @@ public void SolvePart1() List galaxies = new(); - for (int y = 0; y < height; y++) + for (long y = 0; y < height; y++) { var line = input[y]; - for (int x = 0; x < width; x++) + for (long x = 0; x < width; x++) { if (grid[x, y] == '#') { @@ -60,17 +61,90 @@ public void SolvePart1() Console.WriteLine($"Connections: {connections.Count}"); + //list all rows without Galaxy + List rows = new(); + + for(long y = 0; y < height; y++) + { + bool hasGalaxy = false; + for(long x = 0; x < width; x++) + { + if(grid[x, y] == '#') + { + hasGalaxy = true; + break; + } + } + + if(!hasGalaxy) + { + rows.Add(y); + } + } + + //list all columns without Galaxy + List columns = new(); + + for(int x = 0; x < width; x++) + { + bool hasGalaxy = false; + for(int y = 0; y < height; y++) + { + if(grid[x, y] == '#') + { + hasGalaxy = true; + break; + } + } + + if(!hasGalaxy) + { + columns.Add(x); + } + } + + Console.WriteLine($"Rows: {rows.Count}"); + Console.WriteLine($"Columns: {columns.Count}"); + + //Manhatten distance between galaxies without expansion foreach(var connection in connections) { var galaxy1 = connection.Item1; var galaxy2 = connection.Item2; //calculate manhatten distance between galaxies - int distance = Math.Abs(galaxy1.X - galaxy2.X) + Math.Abs(galaxy1.Y - galaxy2.Y); + long distance = Math.Abs(galaxy1.X - galaxy2.X) + Math.Abs(galaxy1.Y - galaxy2.Y); - Console.WriteLine($"Distance between {galaxy1.Id} and {galaxy2.Id} is {distance}"); + //Console.WriteLine($"Distance between {galaxy1.Id} and {galaxy2.Id} is {distance}"); } + long expansionLevel = 1; + long sum = 0; + long sum2 = 0; + //Manhatten distance between galaxies with expansion + foreach(var connection in connections) + { + var galaxy1 = connection.Item1; + var galaxy2 = connection.Item2; + + long expansionRows = rows.Count(row => row >= galaxy1.Y && row <= galaxy2.Y || row >= galaxy2.Y && row <= galaxy1.Y ); + long expansionColumns = columns.Count(column => column >= galaxy1.X && column <= galaxy2.X || column >= galaxy2.X && column <= galaxy1.X); + + //Console.WriteLine($"Expansion rows: {expansionRows}, galaxy1: {galaxy1.Y}, galaxy2: {galaxy2.Y}"); + //Console.WriteLine($"Expansion columns: {expansionColumns}, galaxy1: {galaxy1.X}, galaxy2: {galaxy2.X}"); + + //calculate manhatten distance between galaxies + long distance = Math.Abs(galaxy1.X - galaxy2.X) + (expansionLevel * expansionColumns) + Math.Abs(galaxy1.Y - galaxy2.Y) + (expansionLevel * expansionRows); + sum += distance; + //Console.WriteLine($"Distance between {galaxy1.Id + 1} and {galaxy2.Id + 1} with expansion is {distance} - {expansionRows} - {expansionColumns}"); + + var expansionLevel2 = 999999; + long distance2 = Math.Abs(galaxy1.X - galaxy2.X) + (expansionLevel2 * expansionColumns) + Math.Abs(galaxy1.Y - galaxy2.Y) + (expansionLevel2 * expansionRows); + sum2 += distance2; + } + Console.WriteLine($"Sum: {sum}"); + Console.WriteLine($"Sum2: {sum2}"); + // 82000210 is too low } public void SolvePart2() @@ -78,12 +152,14 @@ public void SolvePart2() Console.WriteLine($"part 2 under development"); } - class Galaxy (int id, int x, int y) + class Galaxy (long id, long x, long y) { - public int Id { get; set; } = id; - public int X { get; set; } = x; - public int Y { get; set; } = y; + public long Id { get; set; } = id; + public long X { get; set; } = x; + public long Y { get; set; } = y; } + + } \ No newline at end of file From fd0f0d9f28df67fb582d76af83d89a716f14525e Mon Sep 17 00:00:00 2001 From: deckelmouck Date: Tue, 12 Dec 2023 08:23:55 +0000 Subject: [PATCH 3/3] solved day 11 part 2 --- 2023/Day11/solution.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/2023/Day11/solution.cs b/2023/Day11/solution.cs index bb15e92..20a5b35 100644 --- a/2023/Day11/solution.cs +++ b/2023/Day11/solution.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using adventofcode; namespace aoc2023; @@ -12,8 +13,8 @@ public void SolvePart1() Console.WriteLine($"part 1"); solutionBase sb = new(); - //var input = sb.getInputLines(@"2023/Day11/input").ToArray(); - var input = sb.getInputLines(@"2023/Day11/test").ToArray(); + var input = sb.getInputLines(@"2023/Day11/input").ToArray(); + //var input = sb.getInputLines(@"2023/Day11/test").ToArray(); int width = input[0].Length; int height = input.Length; @@ -120,7 +121,7 @@ public void SolvePart1() long expansionLevel = 1; long sum = 0; - long sum2 = 0; + BigInteger sum2 = 0; //Manhatten distance between galaxies with expansion foreach(var connection in connections) { @@ -139,12 +140,16 @@ public void SolvePart1() //Console.WriteLine($"Distance between {galaxy1.Id + 1} and {galaxy2.Id + 1} with expansion is {distance} - {expansionRows} - {expansionColumns}"); var expansionLevel2 = 999999; - long distance2 = Math.Abs(galaxy1.X - galaxy2.X) + (expansionLevel2 * expansionColumns) + Math.Abs(galaxy1.Y - galaxy2.Y) + (expansionLevel2 * expansionRows); + BigInteger distance2 = Math.Abs(galaxy1.X - galaxy2.X) + (expansionLevel2 * expansionColumns) + Math.Abs(galaxy1.Y - galaxy2.Y) + (expansionLevel2 * expansionRows); + + Console.WriteLine($"{distance2}"); + sum2 += distance2; } Console.WriteLine($"Sum: {sum}"); Console.WriteLine($"Sum2: {sum2}"); // 82000210 is too low + // haha, forgot to change from test to input :D } public void SolvePart2()