Completed day 16
This commit is contained in:
parent
76405a5ae0
commit
b05d04d202
@ -7,9 +7,9 @@ InputReader inputReader = new()
|
|||||||
//IsDebug = true
|
//IsDebug = true
|
||||||
};
|
};
|
||||||
|
|
||||||
inputReader.SetInputByChallange(12);
|
//inputReader.SetInputByChallange(12);
|
||||||
|
|
||||||
IChallange challange = new Day12(inputReader);
|
IChallange challange = new Day16(inputReader);
|
||||||
|
|
||||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,7 @@ namespace AdventOfCode.Core.Shared
|
|||||||
public string GetRowAsString(long columnNumber) => string.Concat(DataGrid.Where(n => n.Y == columnNumber).OrderBy(n => n.X).Select(n => n.Value));
|
public string GetRowAsString(long columnNumber) => string.Concat(DataGrid.Where(n => n.Y == columnNumber).OrderBy(n => n.X).Select(n => n.Value));
|
||||||
public IEnumerable<T> GetColumn(long columnNumber) => DataGrid.Where(n => n.Y == columnNumber);
|
public IEnumerable<T> GetColumn(long columnNumber) => DataGrid.Where(n => n.Y == columnNumber);
|
||||||
public string GetColumnAsString(long rowNumber) => string.Concat(DataGrid.Where(n => n.X == rowNumber).OrderBy(n => n.Y).Select(n => n.Value));
|
public string GetColumnAsString(long rowNumber) => string.Concat(DataGrid.Where(n => n.X == rowNumber).OrderBy(n => n.Y).Select(n => n.Value));
|
||||||
|
public T? TryGetNode(long x, long y) => DataGrid.FirstOrDefault(n => n.X == x && n.Y == y);
|
||||||
public T GetNode(long x, long y) => DataGrid.First(n => n.X == x && n.Y == y);
|
public T GetNode(long x, long y) => DataGrid.First(n => n.X == x && n.Y == y);
|
||||||
public IEnumerable<T> FindWithValue(char toFind) => DataGrid.Where(n => n.Value == toFind);
|
public IEnumerable<T> FindWithValue(char toFind) => DataGrid.Where(n => n.Value == toFind);
|
||||||
|
|
||||||
|
|||||||
135
AdventOfCode.Solutions/2023/Day 16/Day16.cs
Normal file
135
AdventOfCode.Solutions/2023/Day 16/Day16.cs
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
using AdventOfCode.Core.Shared.Grid;
|
||||||
|
|
||||||
|
namespace AdventOfCode.Solutions._2023
|
||||||
|
{
|
||||||
|
public class Day16(InputReader reader) : IChallange
|
||||||
|
{
|
||||||
|
private InputReader _inputReader = reader;
|
||||||
|
|
||||||
|
public async Task<string> GetSolutionPart1()
|
||||||
|
{
|
||||||
|
|
||||||
|
Grid<Point> grid = await _inputReader.ReadToGrid<Point>();
|
||||||
|
Point? point = grid.TryGetNode(0, 0);
|
||||||
|
ISet<Point> set = Move(grid, new HashSet<Point>(), new HashSet<Point>(), point, Direction.Right);
|
||||||
|
return set.Count.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetSolutionPart2()
|
||||||
|
{
|
||||||
|
Grid<Point> grid = await _inputReader.ReadToGrid<Point>();
|
||||||
|
int columnMax = grid.GetColumn(0).Select(p => Move(grid, new HashSet<Point>(), new HashSet<Point>(), p, Direction.Down).Count).Max();
|
||||||
|
int rowMax = grid.GetRow(0).Select(p => Move(grid, new HashSet<Point>(), new HashSet<Point>(), p, Direction.Right).Count).Max();
|
||||||
|
return Math.Max(columnMax, rowMax).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ISet<Point> Move(Grid<Point> grid, ISet<Point> energized, ISet<Point> splittersHit, Point? currentLocation, Direction direction)
|
||||||
|
{
|
||||||
|
if (currentLocation == null)
|
||||||
|
return energized;
|
||||||
|
|
||||||
|
if ((currentLocation.Value == '-' || currentLocation.Value == '|') && !splittersHit.Add(currentLocation))
|
||||||
|
return energized;
|
||||||
|
|
||||||
|
energized.Add(currentLocation);
|
||||||
|
|
||||||
|
|
||||||
|
(long X, long Y, Direction NewDirection) newPosition = (currentLocation.X, currentLocation.Y, direction);
|
||||||
|
switch (currentLocation.Value)
|
||||||
|
{
|
||||||
|
case '.':
|
||||||
|
newPosition = MoveStraight(currentLocation, direction);
|
||||||
|
Point? newPoint = grid.TryGetNode(newPosition.X, newPosition.Y);
|
||||||
|
while(newPoint != null && newPoint.Value == '.')
|
||||||
|
{
|
||||||
|
energized.Add(newPoint);
|
||||||
|
newPosition = MoveStraight(newPoint, direction);
|
||||||
|
newPoint = grid.TryGetNode(newPosition.X, newPosition.Y);
|
||||||
|
}
|
||||||
|
return Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
case '\\':
|
||||||
|
if (direction == Direction.Down || direction == Direction.Up)
|
||||||
|
newPosition = MoveLeft(currentLocation, direction);
|
||||||
|
else if (direction == Direction.Right ||direction == Direction.Left)
|
||||||
|
newPosition = MoveRight(currentLocation, direction);
|
||||||
|
return Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
|
||||||
|
case '/':
|
||||||
|
if (direction == Direction.Down || direction == Direction.Up)
|
||||||
|
newPosition = MoveRight(currentLocation, direction);
|
||||||
|
else if (direction == Direction.Right || direction == Direction.Left)
|
||||||
|
newPosition = MoveLeft(currentLocation, direction);
|
||||||
|
|
||||||
|
return Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
case '|':
|
||||||
|
if (direction == Direction.Up || direction == Direction.Down)
|
||||||
|
newPosition = MoveStraight(currentLocation, direction);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newPosition = MoveLeft(currentLocation, direction);
|
||||||
|
energized = Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
newPosition = MoveRight(currentLocation, direction);
|
||||||
|
}
|
||||||
|
return Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
case '-':
|
||||||
|
|
||||||
|
if (direction == Direction.Right || direction == Direction.Left)
|
||||||
|
newPosition = MoveStraight(currentLocation, direction);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newPosition = MoveLeft(currentLocation, direction);
|
||||||
|
energized = Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
newPosition = MoveRight(currentLocation, direction);
|
||||||
|
}
|
||||||
|
return Move(grid, energized, splittersHit, grid.TryGetNode(newPosition.X, newPosition.Y), newPosition.NewDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return energized;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum Direction
|
||||||
|
{
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (long X, long Y, Direction NewDirection) MoveStraight(Point from, Direction direction)
|
||||||
|
{
|
||||||
|
return direction switch
|
||||||
|
{
|
||||||
|
Direction.Up => (from.X, from.Y - 1, direction),
|
||||||
|
Direction.Down => (from.X, from.Y + 1, direction),
|
||||||
|
Direction.Left => (from.X - 1, from.Y, direction),
|
||||||
|
Direction.Right => (from.X + 1, from.Y, direction),
|
||||||
|
_ => (from.X, from.Y, direction),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (long X, long Y, Direction NewDirection) MoveLeft(Point from, Direction direction)
|
||||||
|
{
|
||||||
|
return direction switch
|
||||||
|
{
|
||||||
|
Direction.Up => (from.X - 1, from.Y, Direction.Left),
|
||||||
|
Direction.Down => (from.X + 1, from.Y, Direction.Right),
|
||||||
|
Direction.Left => (from.X, from.Y + 1, Direction.Down),
|
||||||
|
Direction.Right => (from.X, from.Y - 1, Direction.Up),
|
||||||
|
_ => (from.X, from.Y, direction),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (long X, long Y, Direction NewDirection) MoveRight(Point from, Direction direction)
|
||||||
|
{
|
||||||
|
return direction switch
|
||||||
|
{
|
||||||
|
Direction.Up => (from.X + 1, from.Y, Direction.Right),
|
||||||
|
Direction.Down => (from.X - 1, from.Y, Direction.Left),
|
||||||
|
Direction.Left => (from.X, from.Y - 1, Direction.Up),
|
||||||
|
Direction.Right => (from.X, from.Y + 1, Direction.Down),
|
||||||
|
_ => (from.X, from.Y, direction),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
110
AdventOfCode.Solutions/2023/Day 16/day-16-input.txt
Normal file
110
AdventOfCode.Solutions/2023/Day 16/day-16-input.txt
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
\....|...-...../.....|....................-./............|/.....................-........./.....-.............
|
||||||
|
\..........-.....................-......................................|....................\..../.....-.....
|
||||||
|
....\......-...../../../...\......................\.........................-.............|.............|.....
|
||||||
|
.........../..../...........|/\.............\........\.......|.........|..|../............/.........|.........
|
||||||
|
\./...\........\..........\......./....../...............................|....|........|................/.....
|
||||||
|
|-.....|............/...............|...\...|...../......../.....|............|..\..|..../.......-.........\..
|
||||||
|
........-.............||../..........|.........../..................../.|.....|\..-/......./...\...\....-.....
|
||||||
|
....................//........................./..........\.......|...|.........../......\-......-...\........
|
||||||
|
....../..........\......\.....................\................\......\..\..|.................................
|
||||||
|
.................\...............-....../........./....-./..........|..../..-..|../................\..........
|
||||||
|
.............-...............-......|.../.....-.....//.\................../..........-......-...........-.....
|
||||||
|
.........../.\.....................\............-..........|.......\..................\.......................
|
||||||
|
...............\............/.|...\........../....\.............|...........\...............|.../........\....
|
||||||
|
./..............\................-\.../\.......-....................-..............-|.............\.-.........
|
||||||
|
../....../.........|..../......\...\.\.................|......./.......|...............-.........-............
|
||||||
|
-...-............................\.............-.\........-.....|.........././-......|......-..../...|........
|
||||||
|
................-.....................|..|...\.././.............|..........................-.............-....
|
||||||
|
.......|...............................|.......................................-......../.............|-...-..
|
||||||
|
.\|............................\................|.....-.....-................................./...-..\...../..
|
||||||
|
.......\......-....\|......|........................................./...||...............\...................
|
||||||
|
.................../......../.......-......\..............|..\...........-.........-.............../..........
|
||||||
|
........................\......................../.................-........................\.....\...........
|
||||||
|
..........|......|........................|.|\...............\...|.............-...-....-............--.......
|
||||||
|
............|........../....\..........\........../..-..|...........-...\........................./..-........
|
||||||
|
.............||../..../.......................-/.......\.....\............./|..-...............|.....|........
|
||||||
|
................................|..\.........-......./...........-../-............\................\....-../..
|
||||||
|
.............-.............................|.\...............................\\...............................
|
||||||
|
..|.......-................................\.......|.......\.....................|../\..........\..........-..
|
||||||
|
......-..\..........-.................\.-..../\......./......./..-.......................\.....-........\.....
|
||||||
|
............................../.....................\.................\................|..../\.............\..
|
||||||
|
..............................-.......\.........-..........\....../............./.....-........\...-.........|
|
||||||
|
/..................\...||..|...........\...............-............\..........|./.....-.-./.............../..
|
||||||
|
.....|......./...............\.......-................-...........................................\...........
|
||||||
|
.|.................../.....\............./.....\.................-.../.......-..............\..-..............
|
||||||
|
......\........|...|...........//..........|\.\..............................-..../...-............|..........
|
||||||
|
|......\....|\......|........../.........../........-.........|.-\//................/..\....|.../........|....
|
||||||
|
.-....-......................./.........\.........-.../............................-..|........|.....|...|....
|
||||||
|
.........-..................-..........-....-....................-....-./.......................|.../......\..
|
||||||
|
..-../......................-\.....|...\........./.-...............-.................../../............|......
|
||||||
|
.-/.......-../.......-...|\...../..........-...\.......\.........-....\-................-......|........-.....
|
||||||
|
.............................\\\............-..............|........../...................-......../....../../
|
||||||
|
.....-....\....\......................-..\...............-..|...............-................-.......\......|.
|
||||||
|
..-...\.............................../.../.............................--.......-..|.|\...../...............\
|
||||||
|
......\.../...-.....|....|......\.../|...................................-................-................./.
|
||||||
|
.......-......-.../..................|../..................\..........................\-..........-\..........
|
||||||
|
.-.......\.........|....|\/./-\.-.....|..................-...................................-|...\...........
|
||||||
|
...........-........../\..................\./..........................\...../...........-...\...../..\....-..
|
||||||
|
..-.|...../|......\..\........|.............-.....|..........\...-...-../......................../.......|....
|
||||||
|
......../................/..\.......\..-...|.....\.......|\..........................-..\../..........\./.....
|
||||||
|
........\.-......|......|..........|........-...................-..\.....-./..............-.\......\..........
|
||||||
|
........|./..................\........|......................\............................./.-\....\........-.
|
||||||
|
........\.|......\................/..\....-/.\.....-.....|............................\..\.|............|...-.
|
||||||
|
...-.....-|..................|...............-./.....................\..\............|.....-..................
|
||||||
|
..........|\./.....\..................|.........|.....................-\./..\........\........................
|
||||||
|
../........\..............|....-...............................................|..........-......../..........
|
||||||
|
..\..............|...../../....................-....\.....................|........\...\\.....................
|
||||||
|
...........................-...\......\......./../..........\-...../.......................-...........\......
|
||||||
|
-.|........................../.....|...............\..........-.............\...-................-.......-....
|
||||||
|
/../.........\........./......-.../..-..................-.......................\............--...............
|
||||||
|
\...............|......-..../......................................-........./|......................\|....../
|
||||||
|
.....\.......................................-...................-.../...........\.......|../.............|...
|
||||||
|
....-....||......\..\.......-.........-......../..../............\..................................-.........
|
||||||
|
...-..|.......\..\..|..........-../-..........\............../...............|..../........./....../.......|./
|
||||||
|
.......................\.......././.............\..................-..........|................../....|.......
|
||||||
|
-.|.....................\\.........../......./.|................/................|./......./....-.............
|
||||||
|
......-...............-....-................|./\..\-/...../......../....-....\....|.-.................../...\.
|
||||||
|
...............\......-...\...\....-....-/....................\./....../..................-|........\./...../.
|
||||||
|
.\......................./..\.........\.........|............|.......-../..\.-..|.-..........................|
|
||||||
|
..|../.....................|/......................./......../.../.........-.../.|./.|.-..............|....-..
|
||||||
|
...-.-/............/............|..........|....../\...\./.....|..................|....\............/.........
|
||||||
|
.....|........./.........\.............../......./../../...................|.......//.......\..\..........\...
|
||||||
|
......./.............\...........\.\..............................................\...................|.......
|
||||||
|
...............-|...../..-............................\.....-.............../\.........-....................\.
|
||||||
|
.........-.....................-...\...................-..../.................||....\.........-.......-..-....
|
||||||
|
.|../..\...\......|..\./.........\..............|..\/......../...............-..-....................-.....\..
|
||||||
|
..................|........\.-.....|.......\............../................................/...-....|...../...
|
||||||
|
...........-......./........-...--........\.../..-...../.|............|......./...............|....../\......\
|
||||||
|
.../.....\...................-..../....................................-......................|.............-.
|
||||||
|
.\.....|....-.....|.....-...................................................|../....\..................\.....-
|
||||||
|
....-........../\......../.......-....\............................./-.............-..........................
|
||||||
|
....\.........-........./......../....|..............\........\../.................\.|...............\........
|
||||||
|
.........-..............|./............-.......................\.......\......-\......\......../..............
|
||||||
|
-.|...|./-..........\..........-...-............|...-......................\..\.............................-.
|
||||||
|
............/......................../.........................................\/..........|.....|.-........|/
|
||||||
|
....../..|./..././.....-......................../....|................../..|........-.....................|./.
|
||||||
|
.......................\....................../......./......\/........\......./....../\|.....\...............
|
||||||
|
\...........\/.|../..................../...............\\...\..../..................\......./.................
|
||||||
|
.../................/...............\./....-./........../........./\..-.................................\.....
|
||||||
|
........-.......-...........\........-.......\............|............|.....-........|..|..../...............
|
||||||
|
.............|...|............/....\............................................-..\......./.../..........\...
|
||||||
|
...................../-..|...........-................../.\...|.....\.....|/.................|................
|
||||||
|
..|................\.....-...........|.\............-.........|....../...................\......-.|.......\...
|
||||||
|
........\......../.....-...\..\...........\........|/.............../......|.......|.\..|........|............
|
||||||
|
..........\.....................-..|/.....\............-............../.......................|./........\....
|
||||||
|
.......\........|...........\......\.......-..-.....|............./......|\..............................|....
|
||||||
|
.........\.......................-............-/.................................\............|...............
|
||||||
|
.........\................/.............................../..........\................-../../......|......-...
|
||||||
|
....\...-./...............\...............|.....\|....|/...............-...........................-..........
|
||||||
|
.|.......\.......-............./.......\............./....................|......|../......-...........\......
|
||||||
|
.....\.....-.....\...\................/......\...\../.\...\....\-....-././...............................|./..
|
||||||
|
.....................-.\.\.........../......./...-......./-.........................\.......|../....\..|..../.
|
||||||
|
|..\......./-......-...../..-....../.................../..-.....................-..../...........\...../......
|
||||||
|
......................................./................|....\/....\............/....../......................
|
||||||
|
......................../..........|........-............|.........................|.\................|....../
|
||||||
|
-.............-...|...........-....-..-.|\.......|......\..........-......|-...-......\.......................
|
||||||
|
...........\.\\.../.................\..........\..-......-......|.-............................-.||...|....../
|
||||||
|
....|................--........|.|..........|......\.............................................-............
|
||||||
|
.-..|................/../..-.........................../...../.....\.../....-.....\-..................../.....
|
||||||
|
............./|..........................\...................................-..-..........|....\..|..........
|
||||||
|
....|...../........./.-....-..................../.........../....................-................\./.........
|
||||||
@ -12,6 +12,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Update="2023\Day 16\Day16.cs">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Compile>
|
||||||
<Compile Update="2023\Day 15\Day15.cs">
|
<Compile Update="2023\Day 15\Day15.cs">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@ -1 +1,2 @@
|
|||||||
global using AdventOfCode.Core.Shared;
|
global using AdventOfCode.Core;
|
||||||
|
global using AdventOfCode.Core.Shared;
|
||||||
@ -1,6 +1,10 @@
|
|||||||
???.### 1,1,3
|
.|...\....
|
||||||
.??..??...?##. 1,1,3
|
|.-.\.....
|
||||||
?#?#?#?#?#?#?#? 1,3,1,6
|
.....|-...
|
||||||
????.#...#... 4,1,1
|
........|.
|
||||||
????.######..#####. 1,6,5
|
..........
|
||||||
?###???????? 3,2,1
|
.........\
|
||||||
|
..../.\\..
|
||||||
|
.-.-/..|..
|
||||||
|
.|....-|.\
|
||||||
|
..//.|....
|
||||||
Loading…
Reference in New Issue
Block a user