diff --git a/AdventOfCode2025/Inputs/Day9_0.txt b/AdventOfCode2025/Inputs/Day9_0.txt new file mode 100644 index 0000000..7fb70de --- /dev/null +++ b/AdventOfCode2025/Inputs/Day9_0.txt @@ -0,0 +1,8 @@ +7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3 \ No newline at end of file diff --git a/AdventOfCode2025/Inputs/Day9_1.txt b/AdventOfCode2025/Inputs/Day9_1.txt new file mode 100644 index 0000000..26e2789 --- /dev/null +++ b/AdventOfCode2025/Inputs/Day9_1.txt @@ -0,0 +1,496 @@ +97989,50290 +97989,51491 +97502,51491 +97502,52723 +97969,52723 +97969,53929 +97784,53929 +97784,55111 +97415,55111 +97415,56355 +97603,56355 +97603,57620 +97806,57620 +97806,58795 +97420,58795 +97420,60036 +97381,60036 +97381,61082 +96479,61082 +96479,62403 +96738,62403 +96738,63461 +95990,63461 +95990,64680 +95819,64680 +95819,65812 +95362,65812 +95362,67051 +95198,67051 +95198,68160 +94676,68160 +94676,69290 +94212,69290 +94212,70413 +93728,70413 +93728,71204 +92575,71204 +92575,72650 +92730,72650 +92730,73331 +91442,73331 +91442,74529 +91099,74529 +91099,75554 +90450,75554 +90450,76726 +90023,76726 +90023,77816 +89454,77816 +89454,78682 +88568,78682 +88568,79549 +87701,79549 +87701,80687 +87173,80687 +87173,81723 +86503,81723 +86503,82480 +85516,82480 +85516,83576 +84896,83576 +84896,84275 +83863,84275 +83863,85339 +83187,85339 +83187,85797 +81950,85797 +81950,86483 +80938,86483 +80938,87077 +79861,87077 +79861,88209 +79208,88209 +79208,89104 +78352,89104 +78352,89637 +77234,89637 +77234,90451 +76310,90451 +76310,90829 +75107,90829 +75107,91245 +73945,91245 +73945,91916 +72934,91916 +72934,92396 +71820,92396 +71820,93211 +70872,93211 +70872,94077 +69930,94077 +69930,94527 +68790,94527 +68790,94258 +67369,94258 +67369,95368 +66491,95368 +66491,95479 +65238,95479 +65238,96120 +64164,96120 +64164,95840 +62817,95840 +62817,96910 +61843,96910 +61843,96748 +60550,96748 +60550,96930 +59348,96930 +59348,97072 +58143,97072 +58143,97949 +57048,97949 +57048,97240 +55726,97240 +55726,98112 +54598,98112 +54598,98237 +53380,98237 +53380,97914 +52139,97914 +52139,97992 +50925,97992 +50925,98410 +49707,98410 +49707,97863 +48497,97863 +48497,97485 +47303,97485 +47303,98306 +46027,98306 +46027,97967 +44828,97967 +44828,97098 +43711,97098 +43711,97154 +42483,97154 +42483,97339 +41219,97339 +41219,96502 +40149,96502 +40149,96571 +38895,96571 +38895,96615 +37628,96615 +37628,95738 +36612,95738 +36612,95467 +35431,95467 +35431,95568 +34115,95568 +34115,94799 +33098,94799 +33098,94374 +31962,94374 +31962,93971 +30814,93971 +30814,93488 +29698,93488 +29698,92987 +28589,92987 +28589,92387 +27531,92387 +27531,91964 +26374,91964 +26374,91195 +25412,91195 +25412,90587 +24359,90587 +24359,90142 +23193,90142 +23193,88876 +22590,88876 +22590,88559 +21324,88559 +21324,87802 +20371,87802 +20371,86713 +19692,86713 +19692,86217 +18525,86217 +18525,85067 +17930,85067 +17930,84389 +16911,84389 +16911,83742 +15846,83742 +15846,82958 +14904,82958 +14904,81974 +14175,81974 +14175,81240 +13160,81240 +13160,80274 +12409,80274 +12409,79076 +11962,79076 +11962,78077 +11274,78077 +11274,77102 +10554,77102 +10554,76169 +9765,76169 +9765,75200 +9019,75200 +9019,74195 +8323,74195 +8323,72908 +8132,72908 +8132,72167 +6929,72167 +6929,70969 +6588,70969 +6588,69949 +5879,69949 +5879,68497 +6165,68497 +6165,67407 +5643,67407 +5643,66510 +4579,66510 +4579,65176 +4704,65176 +4704,64177 +3836,64177 +3836,62896 +3875,62896 +3875,61817 +3194,61817 +3194,60482 +3554,60482 +3554,59411 +2752,59411 +2752,58208 +2547,58208 +2547,56980 +2517,56980 +2517,55823 +1958,55823 +1958,54524 +2667,54524 +2667,53339 +2335,53339 +2335,52126 +2375,52126 +2375,50925 +2009,50925 +2009,50270 +94671,50270 +94671,48487 +1828,48487 +1828,47303 +2513,47303 +2513,46074 +2271,46074 +2271,44890 +2603,44890 +2603,43602 +2079,43602 +2079,42477 +2804,42477 +2804,41220 +2668,41220 +2668,40118 +3347,40118 +3347,38850 +3239,38850 +3239,37808 +4061,37808 +4061,36600 +4219,36600 +4219,35259 +3995,35259 +3995,34127 +4466,34127 +4466,33015 +4977,33015 +4977,32062 +5871,32062 +5871,30606 +5552,30606 +5552,29497 +6079,29497 +6079,28474 +6780,28474 +6780,27581 +7708,27581 +7708,26356 +8004,26356 +8004,25160 +8381,25160 +8381,24143 +9070,24143 +9070,23088 +9700,23088 +9700,22283 +10688,22283 +10688,21165 +11226,21165 +11226,20507 +12371,20507 +12371,19452 +12995,19452 +12995,18580 +13845,18580 +13845,17660 +14637,17660 +14637,16652 +15341,16652 +15341,16010 +16419,16010 +16419,15192 +17311,15192 +17311,14185 +18034,14185 +18034,13675 +19196,13675 +19196,12791 +20033,12791 +20033,12200 +21105,12200 +21105,10904 +21654,10904 +21654,10593 +22924,10593 +22924,9648 +23754,9648 +23754,9111 +24855,9111 +24855,8201 +25734,8201 +25734,7917 +26974,7917 +26974,7749 +28254,7749 +28254,6483 +28979,6483 +28979,5864 +30043,5864 +30043,5557 +31245,5557 +31245,5054 +32361,5054 +32361,5264 +33738,5264 +33738,4569 +34777,4569 +34777,4441 +36008,4441 +36008,3722 +37060,3722 +37060,3583 +38281,3583 +38281,3084 +39411,3084 +39411,2546 +40546,2546 +40546,2210 +41732,2210 +41732,2116 +42960,2116 +42960,2271 +44214,2271 +44214,2396 +45450,2396 +45450,2291 +46657,2291 +46657,2476 +47878,2476 +47878,1871 +49071,1871 +49071,2077 +50289,2077 +50289,2199 +51500,2199 +51500,1876 +52732,1876 +52732,2410 +53913,2410 +53913,2140 +55159,2140 +55159,2707 +56314,2707 +56314,2740 +57532,2740 +57532,3112 +58696,3112 +58696,3343 +59882,3343 +59882,2828 +61247,2828 +61247,3779 +62266,3779 +62266,3854 +63506,3854 +63506,4047 +64723,4047 +64723,4846 +65739,4846 +65739,5010 +66972,5010 +66972,5441 +68112,5441 +68112,5931 +69227,5931 +69227,6172 +70459,6172 +70459,6919 +71456,6919 +71456,7579 +72486,7579 +72486,7942 +73678,7942 +73678,8504 +74766,8504 +74766,9270 +75730,9270 +75730,9968 +76731,9968 +76731,10498 +77850,10498 +77850,11140 +78898,11140 +78898,12581 +79328,12581 +79328,12708 +80784,12708 +80784,13781 +81475,13781 +81475,14230 +82711,14230 +82711,15695 +83006,15695 +83006,15935 +84479,15935 +84479,17023 +85114,17023 +85114,18332 +85480,18332 +85480,18794 +86798,18794 +86798,19621 +87719,19621 +87719,20964 +87983,20964 +87983,21736 +88981,21736 +88981,22800 +89587,22800 +89587,23867 +90178,23867 +90178,24657 +91212,24657 +91212,26069 +91221,26069 +91221,26769 +92456,26769 +92456,28143 +92466,28143 +92466,29304 +92844,29304 +92844,30131 +93940,30131 +93940,31209 +94528,31209 +94528,32510 +94564,32510 +94564,33582 +95164,33582 +95164,34788 +95398,34788 +95398,36002 +95576,36002 +95576,37113 +96087,37113 +96087,38310 +96298,38310 +96298,39518 +96444,39518 +96444,40649 +96940,40649 +96940,41756 +97650,41756 +97650,42969 +97823,42969 +97823,44226 +97631,44226 +97631,45435 +97757,45435 +97757,46669 +97524,46669 +97524,47877 +97545,47877 +97545,49067 +98367,49067 +98367,50290 \ No newline at end of file diff --git a/AdventOfCode2025/Solutions/Day9.cs b/AdventOfCode2025/Solutions/Day9.cs new file mode 100644 index 0000000..834a815 --- /dev/null +++ b/AdventOfCode2025/Solutions/Day9.cs @@ -0,0 +1,119 @@ +using Position = (long X, long Y); + +namespace AdventOfCode2025.Solutions; + +public class Day9() : Solution(9) +{ + private const char SplitChar = ','; + + public override string SolvePart1() => + GetPositionCombinations(GetPositions().ToList()) + .Select(combination => CalculateRectangleArea(combination.Pos1, combination.Pos2)) + .Max() + .ToString(); + + public override string SolvePart2() + { + List positions = GetPositions().ToList(); + + // var areas = + // GetPositionCombinations(positions) + // .Select(GetRectangleCorners) + // .Where(corners => corners.All(pos => IsPointInsidePolygon(pos, positions))) + // .Select(corners => CalculateRectangleArea(corners[0], corners[2])) + // .Max(); + var areas = + GetPositionCombinations(positions) + .AsParallel() + .Where(combination => AreAllRectanglePointsInsidePolygon(combination.Pos1, combination.Pos2, positions)) + .Select(combination => CalculateRectangleArea(combination.Pos1, combination.Pos2)) + .Max(); + return areas.ToString(); + } + + private static bool AreAllRectanglePointsInsidePolygon(Position pos1, Position pos2, List polygon) + { + var minX = Math.Min(pos1.X, pos2.X); + var maxX = Math.Max(pos1.X, pos2.X); + var minY = Math.Min(pos1.Y, pos2.Y); + var maxY = Math.Max(pos1.Y, pos2.Y); + + if (GetRectangleCorners(pos1, pos2).All(corner => IsPointInsidePolygon(corner, polygon))) + { + return false; + } + + for (var x = minX; x <= maxX; x++) + { + for (var y = minY; y <= maxY; y++) + { + if (!IsPointInsidePolygon((x, y), polygon)) + { + return false; + } + } + } + + return true; + } + + // ray-casting algorithm or even-odd rule + private static bool IsPointInsidePolygon(Position point, List polygon) + { + var intersections = 0; + var n = polygon.Count; + + for (var i = 0; i < n; i++) + { + var p1 = polygon[i]; + var p2 = polygon[(i + 1) % n]; + + if (p1.Y > point.Y != p2.Y > point.Y && point.X < (p2.X - p1.X) * (point.Y - p1.Y) / (p2.Y - p1.Y) + p1.X) + { + intersections++; + } + } + + return intersections % 2 == 1; + } + + private static IEnumerable<(Position Pos1, Position Pos2)> GetPositionCombinations(List positions) + { + for (var i = 0; i < positions.Count; i++) + { + for (var j = i + 1; j < positions.Count; j++) + { + yield return (positions.ElementAt(i), positions.ElementAt(j)); + } + } + } + + private static long CalculateRectangleArea(Position pos1, Position pos2) + { + var length = Math.Abs(pos1.X - pos2.X) + 1; + var width = Math.Abs(pos1.Y - pos2.Y) + 1; + return length * width; + } + + private static Position[] GetRectangleCorners(Position pos1, Position pos2) + { + var minX = Math.Min(pos1.X, pos2.X); + var maxX = Math.Max(pos1.X, pos2.X); + var minY = Math.Min(pos1.Y, pos2.Y); + var maxY = Math.Max(pos1.Y, pos2.Y); + + return + [ + (minX, minY), + (maxX, minY), + (maxX, maxY), + (minX, maxY) + ]; + } + + private IEnumerable GetPositions() => Input.Lines.Select(line => + { + var values = line.Split(SplitChar); + return (X: long.Parse(values[0]), Y: long.Parse(values[1])); + }); +} \ No newline at end of file