diff --git a/input/day09 b/input/day09 new file mode 100644 index 0000000..519c592 --- /dev/null +++ b/input/day09 @@ -0,0 +1,496 @@ +98107,50192 +98107,51403 +97816,51403 +97816,52648 +98404,52648 +98404,53825 +97715,53825 +97715,55038 +97649,55038 +97649,56313 +98041,56313 +98041,57420 +97175,57420 +97175,58579 +96790,58579 +96790,59854 +97000,59854 +97000,60991 +96518,60991 +96518,62292 +96707,62292 +96707,63299 +95787,63299 +95787,64633 +95996,64633 +95996,65671 +95257,65671 +95257,66805 +94824,66805 +94824,68064 +94703,68064 +94703,69040 +93885,69040 +93885,70033 +93144,70033 +93144,71442 +93273,71442 +93273,72223 +92132,72223 +92132,73626 +92168,73626 +92168,74530 +91292,74530 +91292,75470 +90502,75470 +90502,76416 +89735,76416 +89735,77334 +88939,77334 +88939,78456 +88429,78456 +88429,79755 +88124,79755 +88124,80206 +86743,80206 +86743,81651 +86572,81651 +86572,82637 +85835,82637 +85835,83550 +85013,83550 +85013,84208 +83935,84208 +83935,85223 +83215,85223 +83215,85505 +81820,85505 +81820,86396 +80992,86396 +80992,87581 +80394,87581 +80394,87810 +79026,87810 +79026,88858 +78295,88858 +78295,89626 +77345,89626 +77345,90479 +76446,90479 +76446,90760 +75180,90760 +75180,91580 +74253,91580 +74253,91749 +72954,91749 +72954,92987 +72236,92987 +72236,92764 +70765,92764 +70765,93388 +69726,93388 +69726,94574 +68917,94574 +68917,94680 +67640,94680 +67640,95382 +66601,95382 +66601,95646 +65398,95646 +65398,95949 +64215,95949 +64215,95773 +62899,95773 +62899,96533 +61849,96533 +61849,96742 +60649,96742 +60649,96781 +59418,96781 +59418,97093 +58246,97093 +58246,97486 +57080,97486 +57080,97991 +55917,97991 +55917,97935 +54680,97935 +54680,97953 +53458,97953 +53458,98064 +52244,98064 +52244,97870 +51021,97870 +51021,98482 +49806,98482 +49806,97943 +48593,97943 +48593,97538 +47398,97538 +47398,98218 +46133,98218 +46133,97704 +44956,97704 +44956,97150 +43803,97150 +43803,97022 +42603,97022 +42603,97296 +41327,97296 +41327,97133 +40117,97133 +40117,97002 +38894,97002 +38894,96124 +37860,96124 +37860,96328 +36542,96328 +36542,95555 +35506,95555 +35506,95187 +34352,95187 +34352,95215 +33048,95215 +33048,94803 +31894,94803 +31894,93668 +31053,93668 +31053,93616 +29747,93616 +29747,92979 +28703,92979 +28703,92337 +27668,92337 +27668,92289 +26305,92289 +26305,91414 +25397,91414 +25397,90393 +24597,90393 +24597,89829 +23521,89829 +23521,88999 +22623,88999 +22623,88490 +21498,88490 +21498,87464 +20759,87464 +20759,87311 +19326,87311 +19326,86286 +18596,86286 +18596,85462 +17702,85462 +17702,84371 +17064,84371 +17064,83881 +15846,83881 +15846,83261 +14727,83261 +14727,82278 +13983,82278 +13983,80939 +13667,80939 +13667,80408 +12401,80408 +12401,79459 +11625,79459 +11625,78483 +10883,78483 +10883,77211 +10568,77211 +10568,76057 +10115,76057 +10115,75452 +8799,75452 +8799,74402 +8164,74402 +8164,73202 +7800,73202 +7800,72060 +7352,72060 +7352,70898 +6961,70898 +6961,69773 +6506,69773 +6506,68930 +5395,68930 +5395,67756 +5026,67756 +5026,66423 +5105,66423 +5105,65220 +4882,65220 +4882,64148 +4266,64148 +4266,62985 +3923,62985 +3923,61919 +3194,61919 +3194,60554 +3675,60554 +3675,59474 +2942,59474 +2942,58214 +3088,58214 +3088,57150 +2042,57150 +2042,55879 +2316,55879 +2316,54706 +1807,54706 +1807,53447 +2203,53447 +2203,52232 +2206,52232 +2206,51032 +1605,51032 +1605,50179 +94768,50179 +94768,48588 +1883,48588 +1883,47399 +2484,47399 +2484,46162 +2141,46162 +2141,44991 +2634,44991 +2634,43719 +2213,43719 +2213,42544 +2602,42544 +2602,41302 +2568,41302 +2568,40219 +3352,40219 +3352,38855 +2831,38855 +2831,37871 +3915,37871 +3915,36668 +4103,36668 +4103,35421 +4176,35421 +4176,34389 +4920,34389 +4920,32992 +4637,32992 +4637,32018 +5501,32018 +5501,31051 +6327,31051 +6327,29991 +6908,29991 +6908,28766 +7147,28766 +7147,27503 +7349,27503 +7349,26770 +8540,26770 +8540,25571 +8879,25571 +8879,24466 +9397,24466 +9397,23172 +9646,23172 +9646,22147 +10322,22147 +10322,21212 +11124,21212 +11124,20416 +12095,20416 +12095,19808 +13273,19808 +13273,18827 +13980,18827 +13980,17941 +14800,17941 +14800,17112 +15678,17112 +15678,15995 +16266,15995 +16266,14744 +16754,14744 +16754,14559 +18236,14559 +18236,13457 +18882,13457 +18882,12473 +19650,12473 +19650,12285 +21047,12285 +21047,11216 +21759,11216 +21759,10724 +22896,10724 +22896,9656 +23642,9656 +23642,8817 +24558,8817 +24558,8283 +25667,8283 +25667,7673 +26727,7673 +26727,7670 +28104,7670 +28104,7116 +29177,7116 +29177,5920 +29959,5920 +29959,5795 +31239,5795 +31239,5601 +32470,5601 +32470,4729 +33439,4729 +33439,4688 +34714,4688 +34714,4325 +35869,4325 +35869,3410 +36870,3410 +36870,3699 +38209,3699 +38209,3271 +39353,3271 +39353,2503 +40437,2503 +40437,2545 +41690,2545 +41690,2153 +42865,2153 +42865,2431 +44134,2431 +44134,2128 +45325,2128 +45325,1863 +46527,1863 +46527,1715 +47744,1715 +47744,2370 +48983,2370 +48983,2208 +50191,2208 +50191,1596 +51420,1596 +51420,1777 +52638,1777 +52638,2426 +53814,2426 +53814,2323 +55040,2323 +55040,1948 +56315,1948 +56315,3050 +57384,3050 +57384,2961 +58625,2961 +58625,3074 +59839,3074 +59839,3595 +60964,3595 +60964,3677 +62191,3677 +62191,3720 +63442,3720 +63442,4125 +64595,4125 +64595,4172 +65869,4172 +65869,5434 +66708,5434 +66708,5266 +68076,5266 +68076,5977 +69100,5977 +69100,6618 +70143,6618 +70143,6746 +71431,6746 +71431,7832 +72241,7832 +72241,8034 +73512,8034 +73512,9162 +74260,9162 +74260,9514 +75460,9514 +75460,9627 +76840,9627 +76840,10908 +77440,10908 +77440,11672 +78380,11672 +78380,11972 +79679,11972 +79679,13184 +80265,13184 +80265,13479 +81606,13479 +81606,14622 +82220,14622 +82220,15218 +83328,15218 +83328,15880 +84393,15880 +84393,16884 +85117,16884 +85117,17746 +85987,17746 +85987,18854 +86575,18854 +86575,19853 +87275,19853 +87275,20481 +88452,20481 +88452,21548 +89071,21548 +89071,22598 +89707,22598 +89707,23486 +90581,23486 +90581,24670 +91002,24670 +91002,25846 +91409,25846 +91409,26790 +92213,26790 +92213,27909 +92705,27909 +92705,29085 +93072,29085 +93072,30099 +93771,30099 +93771,31076 +94587,31076 +94587,32364 +94666,32364 +94666,33666 +94648,33666 +94648,34775 +95128,34775 +95128,35721 +96154,35721 +96154,36872 +96583,36872 +96583,38043 +96950,38043 +96950,39313 +96900,39313 +96900,40470 +97329,40470 +97329,41656 +97648,41656 +97648,42909 +97551,42909 +97551,44185 +97161,44185 +97161,45335 +97772,45335 +97772,46568 +97572,46568 +97572,47781 +97508,47781 +97508,48975 +97989,48975 +97989,50192 diff --git a/src/day09.scala b/src/day09.scala new file mode 100644 index 0000000..454cde9 --- /dev/null +++ b/src/day09.scala @@ -0,0 +1,57 @@ +package aoc +package day09 + +val dayNumber = "09" + +@main def part1: Unit = + println(part1(loadInput(dayNumber))) + +@main def part2: Unit = + println(part2(loadInput(dayNumber))) + +def part1(input: String): Long = + input + .split('\n').map { case s"$x,$y" => (x = x.toLong, y = y.toLong) } + .combinations(2) + .collect { case Array(a, b) => ((a.x - b.x + 1) * (a.y - b.y + 1)).abs } + .max + +def part2(input: String): Long = + + case class Point(x: Int, y: Int) + + case class Rectangle(xs: Range, ys: Range): + def area: Long = xs.size.toLong * ys.size.toLong + def dropEdges: Rectangle = Rectangle(xs.drop(1).dropRight(1), ys.drop(1).dropRight(1)) + def intersects(other: Rectangle): Boolean = + xs.intersects(other.xs) && ys.intersects(other.ys) + + object Rectangle: + def between(a: Point, b: Point): Rectangle = + val diffX = b.x - a.x + val diffY = b.y - a.y + Rectangle( + a.x to b.x by (if diffX == 0 then 1 else diffX.sign), + a.y to b.y by (if diffY == 0 then 1 else diffY.sign) + ) + + extension (r: Range) + def intersects(other: Range): Boolean = + r.nonEmpty && other.nonEmpty && + (r.contains(other.min) || other.contains(r.min)) + + val points = input.split('\n').collect { case s"$x,$y" => Point(x.toInt, y.toInt) }.toList + val edges = + (points.last :: points) + .sliding(2) + .collect { case List(a, b) => Rectangle.between(a, b) } + .toList + + val rectangles = points + .combinations(2) + .collect { case List(a, b) => Rectangle.between(a, b) } + .toList + + rectangles.collect { case r if !edges.exists(r.dropEdges.intersects) => r.area }.max + +end part2