Skip to content

Commit

Permalink
Merge branch 'day10'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kefaku committed Dec 11, 2023
2 parents ed7da0a + d94adf5 commit b8d0062
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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
}
]
}
6 changes: 6 additions & 0 deletions adventofcode2023/adventofcode2023.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day8", "day8\day8.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
Expand Down Expand Up @@ -63,6 +65,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
Expand Down
180 changes: 180 additions & 0 deletions adventofcode2023/day10/Program.cs
Original file line number Diff line number Diff line change
@@ -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<List<char>> 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<List<char>> 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<List<char>> 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<List<char>> 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<List<char>> 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));
}
}
}
10 changes: 10 additions & 0 deletions adventofcode2023/day10/day10.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit b8d0062

Please sign in to comment.