Skip to content

Commit

Permalink
Merge pull request #18 from deckelmouck/solving2023
Browse files Browse the repository at this point in the history
Solving2023
  • Loading branch information
deckelmouck authored Dec 27, 2023
2 parents 44b3e8f + 32c1b72 commit 50e0773
Show file tree
Hide file tree
Showing 25 changed files with 3,211 additions and 1 deletion.
23 changes: 23 additions & 0 deletions 2020/solutionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,28 @@ public List<long> getInputListLong(string filename )
throw;
}
}

public string getInputString(string filename)
{
string inputString = "";

try
{
using (StreamReader reader = new StreamReader(filename))
{
string line;
while ((line = reader.ReadLine()) != null)
{
inputString = inputString + line;
}
}
return inputString;
}
catch (System.Exception)
{

throw;
}
}
}
}
140 changes: 140 additions & 0 deletions 2023/Day10/input

Large diffs are not rendered by default.

308 changes: 308 additions & 0 deletions 2023/Day10/solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using adventofcode;

namespace aoc2023;

public class solutionDay10 : ISolver
{
public void SolvePart1()
{
Console.WriteLine($"part 1:");

solutionBase sb = new solutionBase();
//var input = sb.getInputLines(@"2023/Day10/testone").ToArray();
//var input = sb.getInputLines(@"2023/Day10/testtwo").ToArray();
var input = sb.getInputLines(@"2023/Day10/input").ToArray();

char[,] map = new char[input.Length, input[0].Length];
for (int y = 0; y < input.Length; y++)
{
//Console.WriteLine($"y = {y} ,input_y = {input[y]}");
var line = input[y].ToArray();
for (int x = 0; x < line.Length; x++)
{
map[x,y] = line[x];
}
}

//for testing
// for(int y = 0; y < map.GetLength(1); y++)
// {
// for(int x = 0; x < map.GetLength(0); x++)
// {
// Console.Write(map[x,y]);
// }
// Console.WriteLine();
// }

//locate the S as starting point
//find the two connected points
//go both two ways and count the steps
//repeat until both ways are meeting each other

//find the S
int sx = 0;
int sy = 0;
for(int y = 0; y < map.GetLength(1); y++)
{
for(int x = 0; x < map.GetLength(0); x++)
{
if(map[x,y] == 'S')
{
sx = x;
sy = y;
}
}
}

Console.WriteLine($"S is at {sx},{sy}");

//find the two connected points
//list all points around the S
//check if there are two points that connect as next step to the starting point

List<Pipe> visitedPipes = new();

Pipe startPipe = new Pipe('S', sx, sy);
visitedPipes.Add(startPipe);

Pipe currentPipe = startPipe;
Pipe nextPipe = startPipe;
Pipe beforePipe = startPipe;

int steps = 0;
bool foundStart = false;
Console.WriteLine($"start at {startPipe}");
//find the next pipe

while(!foundStart)
{
//Console.WriteLine($"current pipe: {currentPipe}");

foreach (var item in currentPipe.GetNeighboor(beforePipe))
{
//Console.WriteLine($"neighboor: {item}");
var neighboorPipe = new Pipe(map[item.Item1, item.Item2], item.Item1, item.Item2);
//Console.WriteLine($"check neighboor pipe: {neighboorPipe}");

if(neighboorPipe != beforePipe)
{
if (PipesAreConnected(currentPipe, neighboorPipe))
{
if(neighboorPipe.pipeChar == 'S' && steps > 3)
{
//Console.WriteLine($"found start again at {currentPipe.x},{currentPipe.y} after {steps} steps");
foundStart = true;
}
else if (neighboorPipe.pipeChar != 'S')
{
//Console.WriteLine($"found connected pipe: {neighboorPipe}");
visitedPipes.Add(neighboorPipe);
beforePipe = currentPipe;
currentPipe = neighboorPipe;
steps++;
break;
}
}
}
}
}

Console.WriteLine($"found start again at {currentPipe.x},{currentPipe.y} after {steps} steps");
var result = (steps + 1) / 2;
Console.WriteLine($"result: {result}");

// foreach (var item in currentPipe.GetNeighboors())
// {
// Console.WriteLine($"neighboor: {item}");
// var neighboorPipe = new Pipe(map[item.Item1, item.Item2], item.Item1, item.Item2);

// if(currentPipe.PipesAreConnected(currentPipe, neighboorPipe))
// {
// Console.WriteLine($"found connected pipe: {neighboorPipe}");
// visitedPipes.Add(neighboorPipe);
// currentPipe = neighboorPipe;
// steps++;
// break;
// }

// }

// for(int y = 0; y < map.GetLength(1); y++)
// {
// for(int x = 0; x < map.GetLength(0); x++)
// {
// if(map[x,y] != 'S' && map[x,y] != ' ' && map[x,y] != '.')
// {
// nextPipe = new Pipe(map[x,y], x, y);
// Console.WriteLine($"check next: {nextPipe}");
// if(currentPipe.PipesAreConnected(currentPipe, nextPipe))
// {
// visitedPipes.Add(nextPipe);
// currentPipe = nextPipe;
// steps++;
// break;
// }
// }
// else if(map[x,y] == 'S' && steps > 2)
// {
// Console.WriteLine($"found start again at {x},{y} after {steps} steps");
// }
// }
// }


}

public void SolvePart2()
{
Console.WriteLine($"part 2 under development");
}

public enum Direction
{
North,
South,
East,
West,
None,
Start
}

public class Pipe
{
public readonly int x;
public readonly int y;
public readonly char pipeChar;

public List<Direction> Directions { get; private set; }

public Pipe(char c, int x, int y)
{
Directions = new List<Direction>();

switch (c)
{
case '|':
Directions.Add(Direction.North);
Directions.Add(Direction.South);
break;
case '-':
Directions.Add(Direction.East);
Directions.Add(Direction.West);
break;
case 'L':
Directions.Add(Direction.North);
Directions.Add(Direction.East);
break;
case 'J':
Directions.Add(Direction.North);
Directions.Add(Direction.West);
break;
case '7':
Directions.Add(Direction.South);
Directions.Add(Direction.West);
break;
case 'F':
Directions.Add(Direction.South);
Directions.Add(Direction.East);
break;
case 'S': //start
Directions.Add(Direction.North);
Directions.Add(Direction.South);
Directions.Add(Direction.East);
Directions.Add(Direction.West);
break;
default:
Directions.Add(Direction.None);
break;
}

this.x = x;
this.y = y;
this.pipeChar = c;
}



public List<(int,int)> GetNeighboors()
{
List<(int,int)> neighboors = new List<(int,int)>();

if(Directions.Contains(Direction.North))
{
neighboors.Add((x, y - 1));
}
if(Directions.Contains(Direction.South))
{
neighboors.Add((x, y + 1));
}
if(Directions.Contains(Direction.East))
{
neighboors.Add((x + 1, y));
}
if(Directions.Contains(Direction.West))
{
neighboors.Add((x - 1, y));
}

return neighboors;
}
public List<(int,int)> GetNeighboor(Pipe pipeFrom)
{
List<(int,int)> neighboors = new List<(int,int)>();

if(Directions.Contains(Direction.North))
{
neighboors.Add((x, y - 1));
}
if(Directions.Contains(Direction.South))
{
neighboors.Add((x, y + 1));
}
if(Directions.Contains(Direction.East))
{
neighboors.Add((x + 1, y));
}
if(Directions.Contains(Direction.West))
{
neighboors.Add((x - 1, y));
}
neighboors.Remove((pipeFrom.x, pipeFrom.y));

return neighboors;
}

public override string ToString()
{
return $"Pipe '{pipeChar}' at {x},{y} with {Directions.Count} directions";
}
}

public bool PipesAreConnected(Pipe pipeFrom, Pipe pipeTo)
{
//check if the pipes are connected
if(pipeFrom.Directions.Contains(Direction.North) && pipeTo.Directions.Contains(Direction.South) && pipeFrom.y > pipeTo.y)
{
return true;
}
if(pipeFrom.Directions.Contains(Direction.South) && pipeTo.Directions.Contains(Direction.North) && pipeFrom.y < pipeTo.y)
{
return true;
}
if(pipeFrom.Directions.Contains(Direction.East) && pipeTo.Directions.Contains(Direction.West) && pipeFrom.x < pipeTo.x)
{
return true;
}
if(pipeFrom.Directions.Contains(Direction.West) && pipeTo.Directions.Contains(Direction.East) && pipeFrom.x > pipeTo.x)
{
return true;
}

return false;
}
}
5 changes: 5 additions & 0 deletions 2023/Day10/testone
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-L|F7
7S-7|
L|7||
-L-J|
L|-JF
5 changes: 5 additions & 0 deletions 2023/Day10/testtwo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
Loading

0 comments on commit 50e0773

Please sign in to comment.