From d94adf5eb361f894e8de62700de1bc49c9080e6f Mon Sep 17 00:00:00 2001 From: Kefaku <114934849+Kefaku@users.noreply.github.com> Date: Sun, 10 Dec 2023 11:24:41 +0100 Subject: [PATCH] solution for 2023/day10-part1 --- .vscode/launch.json | 16 +++ adventofcode2023/adventofcode2023.sln | 6 + adventofcode2023/day10/Program.cs | 180 ++++++++++++++++++++++++++ adventofcode2023/day10/day10.csproj | 10 ++ 4 files changed, 212 insertions(+) create mode 100644 adventofcode2023/day10/Program.cs create mode 100644 adventofcode2023/day10/day10.csproj diff --git a/.vscode/launch.json b/.vscode/launch.json index d74dd32..869fbbb 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -112,6 +112,22 @@ // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console "console": "externalTerminal", "stopAtEntry": false + }, + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md + "name": "launch 2023/day10", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/adventofcode2023/day10/bin/Debug/net8.0/day10.dll", + "args": [], + "cwd": "${workspaceFolder}/adventofcode2023/day10", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "externalTerminal", + "stopAtEntry": false } ] } \ No newline at end of file diff --git a/adventofcode2023/adventofcode2023.sln b/adventofcode2023/adventofcode2023.sln index eca0d66..426a5d4 100644 --- a/adventofcode2023/adventofcode2023.sln +++ b/adventofcode2023/adventofcode2023.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day6", "day6\day6.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day9", "day9\day9.csproj", "{EDF859D4-203B-4630-A42C-A2803E311FC4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day10", "day10\day10.csproj", "{208D7A37-3B8E-4D6C-93E3-934FE7370C5F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {EDF859D4-203B-4630-A42C-A2803E311FC4}.Debug|Any CPU.Build.0 = Debug|Any CPU {EDF859D4-203B-4630-A42C-A2803E311FC4}.Release|Any CPU.ActiveCfg = Release|Any CPU {EDF859D4-203B-4630-A42C-A2803E311FC4}.Release|Any CPU.Build.0 = Release|Any CPU + {208D7A37-3B8E-4D6C-93E3-934FE7370C5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {208D7A37-3B8E-4D6C-93E3-934FE7370C5F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {208D7A37-3B8E-4D6C-93E3-934FE7370C5F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {208D7A37-3B8E-4D6C-93E3-934FE7370C5F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/adventofcode2023/day10/Program.cs b/adventofcode2023/day10/Program.cs new file mode 100644 index 0000000..fede1af --- /dev/null +++ b/adventofcode2023/day10/Program.cs @@ -0,0 +1,180 @@ +using System.Data; +using System.Net.WebSockets; +using System.Text.RegularExpressions; +using Microsoft.VisualBasic; + +namespace adventofcode2023 +{ + class day4 + { + public static void Main(String[] args) + { + // init + var input = Console.ReadLine(); + List> maze = new(); + int dist = 0, contained = 0; + + // input + while (input != null && input != string.Empty) + { + maze.Add(input.ToList()); + input = Console.ReadLine(); + } + + // find start + byte y = (byte)maze.FindIndex(s => s.Contains('S')); + byte x = (byte)maze[y].FindIndex(c => c == 'S'); + + // walk through + Queue<(int y, int x)> q = new(); + q.Enqueue((y, x)); + while (q.Count() != 0) + { + (int y, int x) posCur = q.Dequeue(); + switch (maze[posCur.y][posCur.x]) + { + case 'S': + CheckNorth(ref q, posCur, maze); + CheckSouth(ref q, posCur, maze); + CheckWest(ref q, posCur, maze); + CheckEast(ref q, posCur, maze); + goto default; + case '|': + CheckNorth(ref q, posCur, maze); + CheckSouth(ref q, posCur, maze); + goto default; + case '-': + CheckWest(ref q, posCur, maze); + CheckEast(ref q, posCur, maze); + goto default; + case 'F': + CheckSouth(ref q, posCur, maze); + CheckEast(ref q, posCur, maze); + goto default; + case '7': + CheckSouth(ref q, posCur, maze); + CheckWest(ref q, posCur, maze); + goto default; + case 'J': + CheckNorth(ref q, posCur, maze); + CheckWest(ref q, posCur, maze); + goto default; + case 'L': + CheckNorth(ref q, posCur, maze); + CheckEast(ref q, posCur, maze); + goto default; + default: + maze[posCur.y][posCur.x] = '#'; + break; + } + dist++; + + // foreach (var s in maze) + // Console.WriteLine(string.Join("",s)); + // Console.WriteLine(dist); + } + + // // flood fill from edge (part two) + // char[] blockingTiles = ['#', '0']; + // for (x = 0; x < maze[0].Count(); x++) + // { + // if (!blockingTiles.Contains(maze[0][x])) + // q.Enqueue((0, x)); + // if (!blockingTiles.Contains(maze[maze.Count() - 1][x])) + // q.Enqueue((maze.Count() - 1, x)); + // } + // for (y = 0; y < maze.Count(); y++) + // { + // if (!blockingTiles.Contains(maze[y][0])) + // q.Enqueue((y, 0)); + // if (!blockingTiles.Contains(maze[y][maze[0].Count() - 1])) + // q.Enqueue((y, maze[0].Count() - 1)); + // } + // while (q.Count() != 0) + // { + // (int y, int x) posCur = q.Dequeue(); + // maze[posCur.y][posCur.x] = '0'; + // // north + // if (posCur.y - 1 >= 0) + // if (!blockingTiles.Contains(maze[posCur.y - 1][posCur.x])) + // q.Enqueue((posCur.y - 1, posCur.x)); + // // south + // if (posCur.y + 1 < maze.Count()) + // if (!blockingTiles.Contains(maze[posCur.y + 1][posCur.x])) + // q.Enqueue((posCur.y + 1, posCur.x)); + // // west + // if (posCur.x - 1 >= 0) + // if (!blockingTiles.Contains(maze[posCur.y][posCur.x - 1])) + // q.Enqueue((posCur.y, posCur.x - 1)); + // // east + // if (posCur.x + 1 < maze[0].Count()) + // if (!blockingTiles.Contains(maze[posCur.y][posCur.x + 1])) + // q.Enqueue((posCur.y, posCur.x + 1)); + // // north-west + // if (posCur.x - 1 >= 0 && posCur.y - 1 >= 0) + // if (!blockingTiles.Contains(maze[posCur.y - 1][posCur.x - 1])) + // q.Enqueue((posCur.y - 1, posCur.x - 1)); + // // north-east + // if (posCur.x + 1 < maze[0].Count() && posCur.y - 1 >= 0) + // if (!blockingTiles.Contains(maze[posCur.y - 1][posCur.x + 1])) + // q.Enqueue((posCur.y - 1, posCur.x + 1)); + // // south-west + // if (posCur.x - 1 >= 0 && posCur.y + 1 < maze.Count()) + // if (!blockingTiles.Contains(maze[posCur.y + 1][posCur.x - 1])) + // q.Enqueue((posCur.y + 1, posCur.x - 1)); + // // south-east + // if (posCur.x + 1 < maze[0].Count() && posCur.y + 1 < maze.Count()) + // if (!blockingTiles.Contains(maze[posCur.y + 1][posCur.x + 1])) + // q.Enqueue((posCur.y + 1, posCur.x + 1)); + + // foreach (var s in maze) + // Console.WriteLine(string.Join("",s)); + // Console.WriteLine(); + // } + + // // count ground tiles in contained in the loop + // foreach (var s in maze) + // foreach(var c in s) + // if (c == '.') + // contained++; + + // output + Console.WriteLine($"distance to farthest point: {dist / 2}"); + Console.WriteLine($"number of tiles enclosed by the loop: {contained}"); + Console.ReadKey(); + } + + private static void CheckNorth(ref Queue<(int y, int x)> q, (int y, int x) posCur, List> maze) + { + char[] fittingPipes = ['|', 'F', '7']; + + if (posCur.y - 1 >= 0) + if (fittingPipes.Contains(maze[posCur.y - 1][posCur.x])) + q.Enqueue((posCur.y - 1, posCur.x)); + } + + private static void CheckSouth(ref Queue<(int y, int x)> q, (int y, int x) posCur, List> maze) + { + char[] fittingPipes = ['|', 'J', 'L']; + if (posCur.y + 1 < maze.Count()) + if (fittingPipes.Contains(maze[posCur.y + 1][posCur.x])) + q.Enqueue((posCur.y + 1, posCur.x)); + } + + private static void CheckWest(ref Queue<(int y, int x)> q, (int y, int x) posCur, List> maze) + { + char[] fittingPipes = ['-', 'F', 'L']; + if (posCur.x - 1 >= 0) + if (fittingPipes.Contains(maze[posCur.y][posCur.x - 1])) + q.Enqueue((posCur.y, posCur.x - 1)); + } + + private static void CheckEast(ref Queue<(int y, int x)> q, (int y, int x) posCur, List> maze) + { + char[] fittingPipes = ['-', 'J', '7']; + if (posCur.x + 1 < maze[0].Count()) + if (fittingPipes.Contains(maze[posCur.y][posCur.x + 1])) + q.Enqueue((posCur.y, posCur.x + 1)); + } + } +} diff --git a/adventofcode2023/day10/day10.csproj b/adventofcode2023/day10/day10.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/adventofcode2023/day10/day10.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +