Skip to content

Commit

Permalink
2023 Day03 Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 3, 2023
1 parent adbddbd commit eaa1b86
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions Solutions/2023/Day03.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ private static int Solution2(string[] input) {

int sumOfGearRatios = 0;
foreach (Point position in gearPositions) {
sumOfGearRatios += possibleGears
.Where(g => g.Position == position)
.Select(g => g.PartNo)
.Aggregate(1, (x, y) => x * y);
List<PartNoAndAPosition> gears = [.. possibleGears.Where(g => g.Position == position)];
sumOfGearRatios += gears[0].PartNo * gears[1].PartNo;
}

return sumOfGearRatios;
Expand Down Expand Up @@ -55,23 +53,21 @@ void SearchAdjacent(int row, Match number)
Point corner1 = new(Math.Max(number.Index - 1, 0), Math.Max(row - 1, 0));
Point corner2 = new(Math.Min(number.Index + number.Length + 1, maxX), Math.Min(row + 1, maxY));
int skip = Math.Max(number.Length - 2, 1); // Can skip self
bool foundPart = false;
for (int y = corner1.Y; y <= corner2.Y; y++) {
for (int x = corner1.X; x < corner2.X; x += (y == row ? skip : 1)) {
if (solutionPartNo == 1) {
if (IsSymbol(engineSchematic[x, y])) {
list.Add(new(number.Value.AsInt(), new(number.Index, row)));
foundPart = true;
break;
goto Exit;
}
} else if (engineSchematic[x, y] is GEAR) { // Part2
list.Add(new(number.Value.AsInt(), new(x, y)));
}
}
if (foundPart) {
break;
}
}

Exit:
return;
}

bool IsSymbol(char cell) => (char.IsDigit(cell) || cell is SPACE) is false;
Expand Down

0 comments on commit eaa1b86

Please sign in to comment.