diff --git a/AdvendOfCode.Runner/Program.cs b/AdvendOfCode.Runner/Program.cs index 1289619..8844225 100644 --- a/AdvendOfCode.Runner/Program.cs +++ b/AdvendOfCode.Runner/Program.cs @@ -7,9 +7,9 @@ InputReader inputReader = new() //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()}"); diff --git a/AdventOfCode.Core/Shared/Grid.cs b/AdventOfCode.Core/Shared/Grid.cs index 6f3eec4..8a5449e 100644 --- a/AdventOfCode.Core/Shared/Grid.cs +++ b/AdventOfCode.Core/Shared/Grid.cs @@ -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 IEnumerable 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 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 IEnumerable FindWithValue(char toFind) => DataGrid.Where(n => n.Value == toFind); diff --git a/AdventOfCode.Solutions/2023/Day 16/Day16.cs b/AdventOfCode.Solutions/2023/Day 16/Day16.cs new file mode 100644 index 0000000..d201f80 --- /dev/null +++ b/AdventOfCode.Solutions/2023/Day 16/Day16.cs @@ -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 GetSolutionPart1() + { + + Grid grid = await _inputReader.ReadToGrid(); + Point? point = grid.TryGetNode(0, 0); + ISet set = Move(grid, new HashSet(), new HashSet(), point, Direction.Right); + return set.Count.ToString(); + } + + public async Task GetSolutionPart2() + { + Grid grid = await _inputReader.ReadToGrid(); + int columnMax = grid.GetColumn(0).Select(p => Move(grid, new HashSet(), new HashSet(), p, Direction.Down).Count).Max(); + int rowMax = grid.GetRow(0).Select(p => Move(grid, new HashSet(), new HashSet(), p, Direction.Right).Count).Max(); + return Math.Max(columnMax, rowMax).ToString(); + } + + private static ISet Move(Grid grid, ISet energized, ISet 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), + }; + } + } +} \ No newline at end of file diff --git a/AdventOfCode.Solutions/2023/Day 16/day-16-input.txt b/AdventOfCode.Solutions/2023/Day 16/day-16-input.txt new file mode 100644 index 0000000..ca009cf --- /dev/null +++ b/AdventOfCode.Solutions/2023/Day 16/day-16-input.txt @@ -0,0 +1,110 @@ +\....|...-...../.....|....................-./............|/.....................-........./.....-............. +\..........-.....................-......................................|....................\..../.....-..... +....\......-...../../../...\......................\.........................-.............|.............|..... +.........../..../...........|/\.............\........\.......|.........|..|../............/.........|......... +\./...\........\..........\......./....../...............................|....|........|................/..... +|-.....|............/...............|...\...|...../......../.....|............|..\..|..../.......-.........\.. +........-.............||../..........|.........../..................../.|.....|\..-/......./...\...\....-..... +....................//........................./..........\.......|...|.........../......\-......-...\........ +....../..........\......\.....................\................\......\..\..|................................. +.................\...............-....../........./....-./..........|..../..-..|../................\.......... +.............-...............-......|.../.....-.....//.\................../..........-......-...........-..... +.........../.\.....................\............-..........|.......\..................\....................... +...............\............/.|...\........../....\.............|...........\...............|.../........\.... +./..............\................-\.../\.......-....................-..............-|.............\.-......... +../....../.........|..../......\...\.\.................|......./.......|...............-.........-............ +-...-............................\.............-.\........-.....|.........././-......|......-..../...|........ +................-.....................|..|...\.././.............|..........................-.............-.... +.......|...............................|.......................................-......../.............|-...-.. +.\|............................\................|.....-.....-................................./...-..\...../.. +.......\......-....\|......|........................................./...||...............\................... +.................../......../.......-......\..............|..\...........-.........-.............../.......... +........................\......................../.................-........................\.....\........... +..........|......|........................|.|\...............\...|.............-...-....-............--....... +............|........../....\..........\........../..-..|...........-...\........................./..-........ +.............||../..../.......................-/.......\.....\............./|..-...............|.....|........ +................................|..\.........-......./...........-../-............\................\....-../.. +.............-.............................|.\...............................\\............................... +..|.......-................................\.......|.......\.....................|../\..........\..........-.. +......-..\..........-.................\.-..../\......./......./..-.......................\.....-........\..... +............................../.....................\.................\................|..../\.............\.. +..............................-.......\.........-..........\....../............./.....-........\...-.........| +/..................\...||..|...........\...............-............\..........|./.....-.-./.............../.. +.....|......./...............\.......-................-...........................................\........... +.|.................../.....\............./.....\.................-.../.......-..............\..-.............. +......\........|...|...........//..........|\.\..............................-..../...-............|.......... +|......\....|\......|........../.........../........-.........|.-\//................/..\....|.../........|.... +.-....-......................./.........\.........-.../............................-..|........|.....|...|.... +.........-..................-..........-....-....................-....-./.......................|.../......\.. +..-../......................-\.....|...\........./.-...............-.................../../............|...... +.-/.......-../.......-...|\...../..........-...\.......\.........-....\-................-......|........-..... +.............................\\\............-..............|........../...................-......../....../../ +.....-....\....\......................-..\...............-..|...............-................-.......\......|. +..-...\.............................../.../.............................--.......-..|.|\...../...............\ +......\.../...-.....|....|......\.../|...................................-................-................./. +.......-......-.../..................|../..................\..........................\-..........-\.......... +.-.......\.........|....|\/./-\.-.....|..................-...................................-|...\........... +...........-........../\..................\./..........................\...../...........-...\...../..\....-.. +..-.|...../|......\..\........|.............-.....|..........\...-...-../......................../.......|.... +......../................/..\.......\..-...|.....\.......|\..........................-..\../..........\./..... +........\.-......|......|..........|........-...................-..\.....-./..............-.\......\.......... +........|./..................\........|......................\............................./.-\....\........-. +........\.|......\................/..\....-/.\.....-.....|............................\..\.|............|...-. +...-.....-|..................|...............-./.....................\..\............|.....-.................. +..........|\./.....\..................|.........|.....................-\./..\........\........................ +../........\..............|....-...............................................|..........-......../.......... +..\..............|...../../....................-....\.....................|........\...\\..................... +...........................-...\......\......./../..........\-...../.......................-...........\...... +-.|........................../.....|...............\..........-.............\...-................-.......-.... +/../.........\........./......-.../..-..................-.......................\............--............... +\...............|......-..../......................................-........./|......................\|....../ +.....\.......................................-...................-.../...........\.......|../.............|... +....-....||......\..\.......-.........-......../..../............\..................................-......... +...-..|.......\..\..|..........-../-..........\............../...............|..../........./....../.......|./ +.......................\.......././.............\..................-..........|................../....|....... +-.|.....................\\.........../......./.|................/................|./......./....-............. +......-...............-....-................|./\..\-/...../......../....-....\....|.-.................../...\. +...............\......-...\...\....-....-/....................\./....../..................-|........\./...../. +.\......................./..\.........\.........|............|.......-../..\.-..|.-..........................| +..|../.....................|/......................./......../.../.........-.../.|./.|.-..............|....-.. +...-.-/............/............|..........|....../\...\./.....|..................|....\............/......... +.....|........./.........\.............../......./../../...................|.......//.......\..\..........\... +......./.............\...........\.\..............................................\...................|....... +...............-|...../..-............................\.....-.............../\.........-....................\. +.........-.....................-...\...................-..../.................||....\.........-.......-..-.... +.|../..\...\......|..\./.........\..............|..\/......../...............-..-....................-.....\.. +..................|........\.-.....|.......\............../................................/...-....|...../... +...........-......./........-...--........\.../..-...../.|............|......./...............|....../\......\ +.../.....\...................-..../....................................-......................|.............-. +.\.....|....-.....|.....-...................................................|../....\..................\.....- +....-........../\......../.......-....\............................./-.............-.......................... +....\.........-........./......../....|..............\........\../.................\.|...............\........ +.........-..............|./............-.......................\.......\......-\......\......../.............. +-.|...|./-..........\..........-...-............|...-......................\..\.............................-. +............/......................../.........................................\/..........|.....|.-........|/ +....../..|./..././.....-......................../....|................../..|........-.....................|./. +.......................\....................../......./......\/........\......./....../\|.....\............... +\...........\/.|../..................../...............\\...\..../..................\......./................. +.../................/...............\./....-./........../........./\..-.................................\..... +........-.......-...........\........-.......\............|............|.....-........|..|..../............... +.............|...|............/....\............................................-..\......./.../..........\... +...................../-..|...........-................../.\...|.....\.....|/.................|................ +..|................\.....-...........|.\............-.........|....../...................\......-.|.......\... +........\......../.....-...\..\...........\........|/.............../......|.......|.\..|........|............ +..........\.....................-..|/.....\............-............../.......................|./........\.... +.......\........|...........\......\.......-..-.....|............./......|\..............................|.... +.........\.......................-............-/.................................\............|............... +.........\................/.............................../..........\................-../../......|......-... +....\...-./...............\...............|.....\|....|/...............-...........................-.......... +.|.......\.......-............./.......\............./....................|......|../......-...........\...... +.....\.....-.....\...\................/......\...\../.\...\....\-....-././...............................|./.. +.....................-.\.\.........../......./...-......./-.........................\.......|../....\..|..../. +|..\......./-......-...../..-....../.................../..-.....................-..../...........\...../...... +......................................./................|....\/....\............/....../...................... +......................../..........|........-............|.........................|.\................|....../ +-.............-...|...........-....-..-.|\.......|......\..........-......|-...-......\....................... +...........\.\\.../.................\..........\..-......-......|.-............................-.||...|....../ +....|................--........|.|..........|......\.............................................-............ +.-..|................/../..-.........................../...../.....\.../....-.....\-..................../..... +............./|..........................\...................................-..-..........|....\..|.......... +....|...../........./.-....-..................../.........../....................-................\./......... \ No newline at end of file diff --git a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj index 7ecd75f..10bd450 100644 --- a/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj +++ b/AdventOfCode.Solutions/AdventOfCode.Solutions.csproj @@ -12,6 +12,9 @@ + + Always + Always diff --git a/AdventOfCode.Solutions/Usings.cs b/AdventOfCode.Solutions/Usings.cs index 9b9d2ed..92ad7f3 100644 --- a/AdventOfCode.Solutions/Usings.cs +++ b/AdventOfCode.Solutions/Usings.cs @@ -1 +1,2 @@ -global using AdventOfCode.Core.Shared; \ No newline at end of file +global using AdventOfCode.Core; +global using AdventOfCode.Core.Shared; \ No newline at end of file diff --git a/AdventOfCode.Solutions/day-00-input.txt b/AdventOfCode.Solutions/day-00-input.txt index c5bec3a..c78b2e7 100644 --- a/AdventOfCode.Solutions/day-00-input.txt +++ b/AdventOfCode.Solutions/day-00-input.txt @@ -1,6 +1,10 @@ -???.### 1,1,3 -.??..??...?##. 1,1,3 -?#?#?#?#?#?#?#? 1,3,1,6 -????.#...#... 4,1,1 -????.######..#####. 1,6,5 -?###???????? 3,2,1 \ No newline at end of file +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... \ No newline at end of file