Skip to content

Commit

Permalink
fix day 10 part 1, start on part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 10, 2023
1 parent aa040eb commit 8243d22
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions AdventOfCode/Day10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,16 @@ public Day10()

public override ValueTask<string> Solve_1()
{
Point start = GetStart();
(char value, Direction from)[] startOptions =
[
('|', Direction.North),
('-', Direction.East),
('L', Direction.North),
('J', Direction.West),
('7', Direction.West),
('F', Direction.South)
];

foreach ((char startOption, Direction from) in startOptions)
{
Point next = start with { Value = startOption };
Direction nextFrom = from;
long steps = 0;

while (next != null && next.Value != 'S')
{
(next, nextFrom) = GetNextMove(next, nextFrom) ?? default;
steps++;
}

if (next?.Value == 'S')
{
return new((steps / 2).ToString());
}
}
return new("Loop not found");
List<Point> points = GetLoopPoints();
return new((points.Count / 2).ToString());
}

public override ValueTask<string> Solve_2()
{
List<Point> points = GetLoopPoints();
int enclosed = 0;

Check warning on line 21 in AdventOfCode/Day10.cs

View workflow job for this annotation

GitHub Actions / build-and-run

The variable 'enclosed' is assigned but its value is never used

Check warning on line 21 in AdventOfCode/Day10.cs

View workflow job for this annotation

GitHub Actions / build-and-run

The variable 'enclosed' is assigned but its value is never used


return new("TBD");
}

Expand All @@ -70,15 +47,15 @@ private Point GetStart()
case ('|', Direction.North):
case ('7', Direction.West):
case ('F', Direction.East):
return point.I + 1 >= _input.Length ? null : (new Point(point.I + 1, point.J, _input[point.I + 1][point.J]), Direction.North);
return point.I + 1 >= _input[0].Length ? null : (new Point(point.I + 1, point.J, _input[point.I + 1][point.J]), Direction.North);
case ('|', Direction.South):
case ('L', Direction.East):
case ('J', Direction.West):
return point.I - 1 < 0 ? null : (new Point(point.I - 1, point.J, _input[point.I - 1][point.J]), Direction.South);
case ('-', Direction.West):
case ('L', Direction.North):
case ('F', Direction.South):
return point.J + 1 >= _input.Length ? null : (new Point(point.I, point.J + 1, _input[point.I][point.J + 1]), Direction.West);
return point.J + 1 >= _input[0].Length ? null : (new Point(point.I, point.J + 1, _input[point.I][point.J + 1]), Direction.West);
case ('-', Direction.East):
case ('J', Direction.North):
case ('7', Direction.South):
Expand All @@ -88,6 +65,40 @@ private Point GetStart()
}
}

private List<Point> GetLoopPoints()
{
Point start = GetStart();
(char value, Direction from)[] startOptions =
[
('|', Direction.North),
('-', Direction.East),
('L', Direction.North),
('J', Direction.West),
('7', Direction.West),
('F', Direction.South)
];

foreach ((char startOption, Direction from) in startOptions)
{
List<Point> points = new List<Point>();
Point next = start with { Value = startOption };
Direction nextFrom = from;
points.Add(next);

while (next != null && next.Value != 'S')
{
(next, nextFrom) = GetNextMove(next, nextFrom) ?? default;
points.Add(next);
}

if (next?.Value == 'S' && from == nextFrom)
{
return points;
}
}
throw new Exception("Loop not found");
}

private record Point(int I, int J, char Value);

private enum Direction
Expand Down

0 comments on commit 8243d22

Please sign in to comment.