Completed Day 11

This commit is contained in:
Rob Stoffelen 2023-12-11 07:43:34 +01:00
parent 8576e28af6
commit 561446f541
5 changed files with 240 additions and 35 deletions

View File

@ -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()}");

View File

@ -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<string> GetSolutionPart1()
{
string[] data = await _inputReader.ReadAsArrayString();
return CreateUniverseAndGetDistances(data).ToString();
}
public async Task<string> GetSolutionPart2()
{
string[] data = await _inputReader.ReadAsArrayString();
return CreateUniverseAndGetDistances(data, true).ToString();
}
private long CreateUniverseAndGetDistances(string[] lineInput, bool part2 = false)
{
List<int> 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<Galaxy> 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;
}
}
}
}

View File

@ -0,0 +1,140 @@
.#...........#.........................................................................#...............#...........#........................
.......................#....................................................................................................................
...............................#...........#.................#.............................#.............................................#..
......#............................................#........................................................................................
......................................................................#.....#.....................#.........................................
.................#.......#.......................................#................#.....#.....................#.............................
.........................................#.................#................................................................................
................................#.............................................................#..........#..................................
#.......................................................................#..............................................#.................#..
..........#....................................#......................................#............................................#........
................#....................................#........#.............................................................................
......#........................................................................#............................................................
......................#..............#..............................................................#..........#.....#......................
..................................................#.......................#..................................................#..........#...
............................#...............................................................................................................
........#...................................................................................................................................
..................#.......................#................................................................#................................
.............................................................#........#.....................................................................
..............................#.......#......................................#.............#........................#.......................
.#............................................................................................................#...........................#.
...................................................................................................#....................#...................
....................#......................#.............#.....#..................................................................#.........
.................................................................................................................#..........................
............................................................................................................................................
....................................................................#.....................#.................................................
.................................#..............................................#..............#............#.............#.................
...........................#............................................#...........................................#.......................
......................#.........................#...........................................................................................
.......................................#....................................#...................................#...........................
.....#..........#.........................................#...............................................................................#.
#..........#......................#...................................................#.......#.....#.......................................
.........................#..................................................................................................................
....................#..............................#.............#.........................................#.......#........#...............
...........................................#........................................................................................#.......
...........................................................#....................#...........................................................
..........#....................................................................................................#............................
...............#................................#..........................#..............................................................#.
..#.................................#.................................................................#.......................#.............
.....................#......#...............#........................#................................................#.....................
..............................................................................................#....................................#........
................................#.......#...................................................................................................
..........#................................................#...................#.....#..............#..........#..........#.................
.........................#..................................................................................................................
..................#...............................#...........................................................................#.............
.........................................................................#............................................#.....................
........#.....#..............................................................................#.........#..................................#.
.#...............................#......#............#.............................................................................#........
....................................................................................#.............#...............#........#................
...........................#................................................................................................................
......................#..................................#..............#................................#.............#................#...
...............................................................#..............#.............................................................
..................................#........................................................#........#.......................................
......#.................................#.......#....................#...............................................................#......
.................#............#..............................................................................#.............................#
............................................................................................................................................
..#.....................#.............................................................................#.....................................
.......................................................................................#..............................#.....................
...............................................................................#.............#..............................................
....................#.....................#...............................................................#.................................
..........#........................#............#...................#.............................................#...............#.........
............................#.............................#..........................#......................................................
...........................................................................................#................................................
.................................................................#.................................#.........................#........#.....
.................#........................................................#..................................#..............................
......................................#......................#..............................................................................
.............#...........#...............................................................................#..........#.......................
.................................#...........#...........................................................................#..............#...
.......#............................................#.....#....................#..............#.....#.......................................
...................#...........................................#................................................#...........................
........................................................................................#...................................................
...#....................#.....#............#.........................................................................................#......
...............#.............................................................#..................#........#................#.................
.........#.....................................#............................................................................................
................................................................................................................................#...........
.....................#.............#.........................................................................#..............................
.....#...............................................................................................................#..............#......#
...........#.......................................#.....#.................#.................#..............................................
............................................................................................................................................
.#.......................................#..........................................#................#......................................
......................#.............#.................#.......................#...........#.............................#...................
...............#.............#................................#......#.......................................................#..............
............................................................................................................................................
...#............................................................................................#....................#......................
..........#.....................#.............................................................................#.............................
...........................#...............................................................................................#...............#
.....................#......................................#.......#......#........#..................#...........................#........
.....#...............................#............#.........................................................................................
............................................................................................................................................
.........................................................#........................................................#.........................
#..............................#..........#..............................................................................#................#.
....................#..............................................#.......................#.........#......................................
............................................................................................................................................
...........#.......................................#.............................................................................#..........
...#......................#...............................................#......................#..........................................
..................#.........................................................................................................................
...........................................................#........#...........#.....#......#........................#.................#...
........#...................................................................................................................................
....................................................................................................#......#................................
................................#...........#....................#.....#...................................................#........#.......
........................#............#......................................#.............#.................................................
...................#............................................................................................#...........................
#................................................................................................................................#..........
..............#.............................................#...............................................................................
..............................#...............................................#.................#......................................#....
....#....................#......................................#...................#...............................#.......#...............
........................................#..........#........................................#................#..............................
............................................................................................................................................
............................................................................................................................................
........................................................................................................................................#...
...........................................................#..................#.................#.................#.........................
.......#........................................................#................................................................#..........
..........................#......#......#..................................................................................#................
...............................................#.....#.................#....................................................................
......................................................................................................#.....................................
................#....................#.....................................................................#........#.......................
..............................................................................................................................#.............
...........................................#.............................................................................................#..
....#........................#..............................#.............#................#................................................
......................#..........................................................................................#......#...................
....................................................#................................#.........#............................................
..........................#.......................................#.........................................................................
.................#........................#.....#........................................................#..................................
.#.........................................................#............#....................................................#..............
..........#..................#.....#..................................................................................................#.....
.................................................................................#..........................................................
.....................................................#....................................#.........#...........#.....#.....................
.............................................#.....................#.......................................#.......................#........
................................#...........................................................................................................
......#........................................................#..............................#.............................................
............#......................................#.....#............................#........................................#............
............................................................................#...............................................................
.......................................#...............................#...................#..............................#...........#.....
.......................#..............................#..........................#..............................#...........................
.................................................................#...................................#......................................
......................................................................................................................#...................#.
.................#..........................................................................................................................
.........#......................#.....#...........#.......................................................#.................................
...........................................#.................#................#.....................................................#.......
..#...............................................................#.........................................................................
.....................#......#......#.............................................................#............#...........#.................

View File

@ -12,6 +12,9 @@
</ItemGroup>
<ItemGroup>
<Compile Update="2023\Day 11\Day11.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 10\Day10.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>

View File

@ -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
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....