From 2c416d69903e53414e4e60bea243ca4562c8eb4d Mon Sep 17 00:00:00 2001 From: KaterynaKateryna Date: Mon, 11 Dec 2023 08:37:58 +0100 Subject: [PATCH] day 10 part 2 --- AdventOfCode/Day10.cs | 61 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/AdventOfCode/Day10.cs b/AdventOfCode/Day10.cs index 3fdcbcf..0451ef6 100644 --- a/AdventOfCode/Day10.cs +++ b/AdventOfCode/Day10.cs @@ -17,11 +17,65 @@ public override ValueTask Solve_1() public override ValueTask Solve_2() { - List points = GetLoopPoints(); - int enclosed = 0; + List loopPoints = GetLoopPoints(); + //List outsidePoints = GetOutsidePoints(loopPoints); + + //List insidePoints = new List(); + //for (int i = -1; i < _input.Length + 1; ++i) + //{ + // for (int j = -1; j < _input[0].Length + 1; ++j) + // + // if (!loopPoints.Any(p => p.I == i && p.J == j) && !outsidePoints.Any(p => p.I == i && p.J == j)) + // { + // insidePoints.Add(new Point(i, j, 'X')); + // } + // } + //} + + var insidePoints = GetInsidePoints(loopPoints); + return new(insidePoints.Count.ToString()); + } - return new("TBD"); + private List GetInsidePoints(List loopPoints) + { + List points = new List(); + for (int i = 0; i < _input.Length; ++i) + { + var lineLoopPoints = loopPoints.Where(p => p.I == i); + bool inside = false; + for (int j = 0; j < _input[0].Length; ++j) + { + Point p = lineLoopPoints.SingleOrDefault(p => p.I == i && p.J == j); + if (p != null) + { + if (p.Value == '|') + { + inside = !inside; + } + else if (p.Value == 'L' || p.Value == 'F') + { + Point nextP = null!; + do + { + j++; + nextP = lineLoopPoints.Single(p => p.I == i && p.J == j); + } + while (nextP.Value != '7' && nextP.Value != 'J'); + + if ((p.Value == 'L' && nextP.Value == '7') || (p.Value == 'F' && nextP.Value == 'J')) + { + inside = !inside; + } + } + } + else if (inside) + { + points.Add(new Point(i, j, 'I')); + } + } + } + return points; } private Point GetStart() @@ -93,6 +147,7 @@ private List GetLoopPoints() if (next?.Value == 'S' && from == nextFrom) { + points.Remove(next); return points; } }