Skip to content

Commit 50e0773

Browse files
authored
Merge pull request #18 from deckelmouck/solving2023
Solving2023
2 parents 44b3e8f + 32c1b72 commit 50e0773

File tree

25 files changed

+3211
-1
lines changed

25 files changed

+3211
-1
lines changed

2020/solutionBase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,28 @@ public List<long> getInputListLong(string filename )
157157
throw;
158158
}
159159
}
160+
161+
public string getInputString(string filename)
162+
{
163+
string inputString = "";
164+
165+
try
166+
{
167+
using (StreamReader reader = new StreamReader(filename))
168+
{
169+
string line;
170+
while ((line = reader.ReadLine()) != null)
171+
{
172+
inputString = inputString + line;
173+
}
174+
}
175+
return inputString;
176+
}
177+
catch (System.Exception)
178+
{
179+
180+
throw;
181+
}
182+
}
160183
}
161184
}

2023/Day10/input

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

2023/Day10/solution.cs

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Security.Cryptography.X509Certificates;
6+
using adventofcode;
7+
8+
namespace aoc2023;
9+
10+
public class solutionDay10 : ISolver
11+
{
12+
public void SolvePart1()
13+
{
14+
Console.WriteLine($"part 1:");
15+
16+
solutionBase sb = new solutionBase();
17+
//var input = sb.getInputLines(@"2023/Day10/testone").ToArray();
18+
//var input = sb.getInputLines(@"2023/Day10/testtwo").ToArray();
19+
var input = sb.getInputLines(@"2023/Day10/input").ToArray();
20+
21+
char[,] map = new char[input.Length, input[0].Length];
22+
for (int y = 0; y < input.Length; y++)
23+
{
24+
//Console.WriteLine($"y = {y} ,input_y = {input[y]}");
25+
var line = input[y].ToArray();
26+
for (int x = 0; x < line.Length; x++)
27+
{
28+
map[x,y] = line[x];
29+
}
30+
}
31+
32+
//for testing
33+
// for(int y = 0; y < map.GetLength(1); y++)
34+
// {
35+
// for(int x = 0; x < map.GetLength(0); x++)
36+
// {
37+
// Console.Write(map[x,y]);
38+
// }
39+
// Console.WriteLine();
40+
// }
41+
42+
//locate the S as starting point
43+
//find the two connected points
44+
//go both two ways and count the steps
45+
//repeat until both ways are meeting each other
46+
47+
//find the S
48+
int sx = 0;
49+
int sy = 0;
50+
for(int y = 0; y < map.GetLength(1); y++)
51+
{
52+
for(int x = 0; x < map.GetLength(0); x++)
53+
{
54+
if(map[x,y] == 'S')
55+
{
56+
sx = x;
57+
sy = y;
58+
}
59+
}
60+
}
61+
62+
Console.WriteLine($"S is at {sx},{sy}");
63+
64+
//find the two connected points
65+
//list all points around the S
66+
//check if there are two points that connect as next step to the starting point
67+
68+
List<Pipe> visitedPipes = new();
69+
70+
Pipe startPipe = new Pipe('S', sx, sy);
71+
visitedPipes.Add(startPipe);
72+
73+
Pipe currentPipe = startPipe;
74+
Pipe nextPipe = startPipe;
75+
Pipe beforePipe = startPipe;
76+
77+
int steps = 0;
78+
bool foundStart = false;
79+
Console.WriteLine($"start at {startPipe}");
80+
//find the next pipe
81+
82+
while(!foundStart)
83+
{
84+
//Console.WriteLine($"current pipe: {currentPipe}");
85+
86+
foreach (var item in currentPipe.GetNeighboor(beforePipe))
87+
{
88+
//Console.WriteLine($"neighboor: {item}");
89+
var neighboorPipe = new Pipe(map[item.Item1, item.Item2], item.Item1, item.Item2);
90+
//Console.WriteLine($"check neighboor pipe: {neighboorPipe}");
91+
92+
if(neighboorPipe != beforePipe)
93+
{
94+
if (PipesAreConnected(currentPipe, neighboorPipe))
95+
{
96+
if(neighboorPipe.pipeChar == 'S' && steps > 3)
97+
{
98+
//Console.WriteLine($"found start again at {currentPipe.x},{currentPipe.y} after {steps} steps");
99+
foundStart = true;
100+
}
101+
else if (neighboorPipe.pipeChar != 'S')
102+
{
103+
//Console.WriteLine($"found connected pipe: {neighboorPipe}");
104+
visitedPipes.Add(neighboorPipe);
105+
beforePipe = currentPipe;
106+
currentPipe = neighboorPipe;
107+
steps++;
108+
break;
109+
}
110+
}
111+
}
112+
}
113+
}
114+
115+
Console.WriteLine($"found start again at {currentPipe.x},{currentPipe.y} after {steps} steps");
116+
var result = (steps + 1) / 2;
117+
Console.WriteLine($"result: {result}");
118+
119+
// foreach (var item in currentPipe.GetNeighboors())
120+
// {
121+
// Console.WriteLine($"neighboor: {item}");
122+
// var neighboorPipe = new Pipe(map[item.Item1, item.Item2], item.Item1, item.Item2);
123+
124+
// if(currentPipe.PipesAreConnected(currentPipe, neighboorPipe))
125+
// {
126+
// Console.WriteLine($"found connected pipe: {neighboorPipe}");
127+
// visitedPipes.Add(neighboorPipe);
128+
// currentPipe = neighboorPipe;
129+
// steps++;
130+
// break;
131+
// }
132+
133+
// }
134+
135+
// for(int y = 0; y < map.GetLength(1); y++)
136+
// {
137+
// for(int x = 0; x < map.GetLength(0); x++)
138+
// {
139+
// if(map[x,y] != 'S' && map[x,y] != ' ' && map[x,y] != '.')
140+
// {
141+
// nextPipe = new Pipe(map[x,y], x, y);
142+
// Console.WriteLine($"check next: {nextPipe}");
143+
// if(currentPipe.PipesAreConnected(currentPipe, nextPipe))
144+
// {
145+
// visitedPipes.Add(nextPipe);
146+
// currentPipe = nextPipe;
147+
// steps++;
148+
// break;
149+
// }
150+
// }
151+
// else if(map[x,y] == 'S' && steps > 2)
152+
// {
153+
// Console.WriteLine($"found start again at {x},{y} after {steps} steps");
154+
// }
155+
// }
156+
// }
157+
158+
159+
}
160+
161+
public void SolvePart2()
162+
{
163+
Console.WriteLine($"part 2 under development");
164+
}
165+
166+
public enum Direction
167+
{
168+
North,
169+
South,
170+
East,
171+
West,
172+
None,
173+
Start
174+
}
175+
176+
public class Pipe
177+
{
178+
public readonly int x;
179+
public readonly int y;
180+
public readonly char pipeChar;
181+
182+
public List<Direction> Directions { get; private set; }
183+
184+
public Pipe(char c, int x, int y)
185+
{
186+
Directions = new List<Direction>();
187+
188+
switch (c)
189+
{
190+
case '|':
191+
Directions.Add(Direction.North);
192+
Directions.Add(Direction.South);
193+
break;
194+
case '-':
195+
Directions.Add(Direction.East);
196+
Directions.Add(Direction.West);
197+
break;
198+
case 'L':
199+
Directions.Add(Direction.North);
200+
Directions.Add(Direction.East);
201+
break;
202+
case 'J':
203+
Directions.Add(Direction.North);
204+
Directions.Add(Direction.West);
205+
break;
206+
case '7':
207+
Directions.Add(Direction.South);
208+
Directions.Add(Direction.West);
209+
break;
210+
case 'F':
211+
Directions.Add(Direction.South);
212+
Directions.Add(Direction.East);
213+
break;
214+
case 'S': //start
215+
Directions.Add(Direction.North);
216+
Directions.Add(Direction.South);
217+
Directions.Add(Direction.East);
218+
Directions.Add(Direction.West);
219+
break;
220+
default:
221+
Directions.Add(Direction.None);
222+
break;
223+
}
224+
225+
this.x = x;
226+
this.y = y;
227+
this.pipeChar = c;
228+
}
229+
230+
231+
232+
public List<(int,int)> GetNeighboors()
233+
{
234+
List<(int,int)> neighboors = new List<(int,int)>();
235+
236+
if(Directions.Contains(Direction.North))
237+
{
238+
neighboors.Add((x, y - 1));
239+
}
240+
if(Directions.Contains(Direction.South))
241+
{
242+
neighboors.Add((x, y + 1));
243+
}
244+
if(Directions.Contains(Direction.East))
245+
{
246+
neighboors.Add((x + 1, y));
247+
}
248+
if(Directions.Contains(Direction.West))
249+
{
250+
neighboors.Add((x - 1, y));
251+
}
252+
253+
return neighboors;
254+
}
255+
public List<(int,int)> GetNeighboor(Pipe pipeFrom)
256+
{
257+
List<(int,int)> neighboors = new List<(int,int)>();
258+
259+
if(Directions.Contains(Direction.North))
260+
{
261+
neighboors.Add((x, y - 1));
262+
}
263+
if(Directions.Contains(Direction.South))
264+
{
265+
neighboors.Add((x, y + 1));
266+
}
267+
if(Directions.Contains(Direction.East))
268+
{
269+
neighboors.Add((x + 1, y));
270+
}
271+
if(Directions.Contains(Direction.West))
272+
{
273+
neighboors.Add((x - 1, y));
274+
}
275+
neighboors.Remove((pipeFrom.x, pipeFrom.y));
276+
277+
return neighboors;
278+
}
279+
280+
public override string ToString()
281+
{
282+
return $"Pipe '{pipeChar}' at {x},{y} with {Directions.Count} directions";
283+
}
284+
}
285+
286+
public bool PipesAreConnected(Pipe pipeFrom, Pipe pipeTo)
287+
{
288+
//check if the pipes are connected
289+
if(pipeFrom.Directions.Contains(Direction.North) && pipeTo.Directions.Contains(Direction.South) && pipeFrom.y > pipeTo.y)
290+
{
291+
return true;
292+
}
293+
if(pipeFrom.Directions.Contains(Direction.South) && pipeTo.Directions.Contains(Direction.North) && pipeFrom.y < pipeTo.y)
294+
{
295+
return true;
296+
}
297+
if(pipeFrom.Directions.Contains(Direction.East) && pipeTo.Directions.Contains(Direction.West) && pipeFrom.x < pipeTo.x)
298+
{
299+
return true;
300+
}
301+
if(pipeFrom.Directions.Contains(Direction.West) && pipeTo.Directions.Contains(Direction.East) && pipeFrom.x > pipeTo.x)
302+
{
303+
return true;
304+
}
305+
306+
return false;
307+
}
308+
}

2023/Day10/testone

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-L|F7
2+
7S-7|
3+
L|7||
4+
-L-J|
5+
L|-JF

2023/Day10/testtwo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
..F7.
2+
.FJ|.
3+
SJ.L7
4+
|F--J
5+
LJ...

0 commit comments

Comments
 (0)