Skip to content

Instantly share code, notes, and snippets.

@eugene70
Last active January 14, 2022 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugene70/f9395d4a2aa9f2adc9b84cddc8bb64c7 to your computer and use it in GitHub Desktop.
Save eugene70/f9395d4a2aa9f2adc9b84cddc8bb64c7 to your computer and use it in GitHub Desktop.
package aoc2021
import scala.annotation.tailrec
object Day05:
case class Point(x: Int, y: Int)
case class Line(start: Point, end: Point):
private def iter(start: Int, end: Int, count: Int): Seq[Int] =
if (start == end) Seq.fill(count)(start)
else Range.inclusive(start, end, if (start < end) 1 else -1)
def toPointSeq: Seq[Point] =
val count = Math.max(Math.abs(start.x - end.x), Math.abs(start.y - end.y)) + 1
iter(start.x, end.x, count)
.zip(iter(start.y, end.y, count))
.map((x, y) => Point(x, y))
type InputType = Seq[Line]
@main def runDay05(): Unit =
val testData = time("testReady", () => ready(testInput))
time("testPart1", () => part1(testData)) // should be 5
time("testPart2", () => part2(testData)) // should be 12
val data = time("ready", () => ready(input))
time("part1", () => part1(data))
time("part2", () => part2(data))
def ready(input: String): InputType =
input
.linesIterator
.map {
case s"$x1,$y1 -> $x2,$y2" => Line(Point(x1.toInt, y1.toInt), Point(x2.toInt, y2.toInt))
}
.toIndexedSeq
def part1(data: InputType): Int =
data
.filter(l => l.start.x == l.end.x || l.start.y == l.end.y)
.flatMap(_.toPointSeq)
.groupBy(p => p: Point)
.count((k, v) => v.length > 1)
def part2(data: InputType): Int =
data
.flatMap(_.toPointSeq)
.groupBy(p => p: Point)
.count((k, v) => v.length > 1)
private val testInput =
"""0,9 -> 5,9
|8,0 -> 0,8
|9,4 -> 3,4
|2,2 -> 2,1
|7,0 -> 7,4
|6,4 -> 2,0
|0,9 -> 2,9
|3,4 -> 1,4
|0,0 -> 8,8
|5,5 -> 8,2""".stripMargin
private val input =
"""973,543 -> 601,915
|758,846 -> 758,168
|511,976 -> 568,976
|858,536 -> 858,677
|256,238 -> 265,238
|475,919 -> 718,919
|973,912 -> 134,73
|984,159 -> 175,968
|166,641 -> 15,490
|504,798 -> 504,946
|717,796 -> 128,207
|302,713 -> 168,713
|830,964 -> 830,444
|151,288 -> 549,686
|530,908 -> 820,908
|540,394 -> 717,394
|581,614 -> 581,496
|111,593 -> 230,712
|938,884 -> 938,917
|923,71 -> 20,974
|530,677 -> 530,332
|144,723 -> 144,980
|700,376 -> 700,917
|501,95 -> 198,95
|251,900 -> 435,900
|912,931 -> 96,115
|962,140 -> 181,921
|104,244 -> 104,178
|504,865 -> 504,612
|301,284 -> 301,173
|973,980 -> 17,24
|886,166 -> 886,455
|380,150 -> 971,741
|57,405 -> 135,405
|515,569 -> 774,310
|227,233 -> 874,880
|75,33 -> 75,290
|487,434 -> 983,930
|17,279 -> 20,279
|719,838 -> 508,627
|262,254 -> 555,254
|516,626 -> 631,511
|352,305 -> 352,715
|876,827 -> 876,731
|372,85 -> 372,344
|619,773 -> 619,799
|885,627 -> 441,627
|779,731 -> 42,731
|806,756 -> 806,237
|378,540 -> 187,540
|840,296 -> 210,296
|250,234 -> 171,234
|249,100 -> 757,608
|787,23 -> 787,826
|690,175 -> 365,175
|238,166 -> 238,738
|52,350 -> 574,872
|297,518 -> 297,590
|133,263 -> 133,219
|438,955 -> 939,955
|621,500 -> 621,948
|93,39 -> 530,476
|988,866 -> 220,98
|483,30 -> 329,184
|205,937 -> 419,723
|314,910 -> 842,910
|472,944 -> 923,944
|487,700 -> 586,601
|59,17 -> 333,17
|553,307 -> 553,746
|810,37 -> 810,167
|898,662 -> 858,662
|191,837 -> 191,157
|83,950 -> 83,910
|229,513 -> 559,843
|825,192 -> 44,973
|37,80 -> 460,503
|648,357 -> 29,357
|603,592 -> 425,770
|137,141 -> 693,141
|179,69 -> 479,69
|851,164 -> 172,164
|916,143 -> 99,960
|908,20 -> 28,900
|178,404 -> 700,404
|122,43 -> 645,43
|548,495 -> 155,888
|216,725 -> 216,583
|790,97 -> 51,836
|98,214 -> 98,200
|90,464 -> 141,464
|32,905 -> 912,25
|253,743 -> 253,115
|521,826 -> 304,826
|733,392 -> 733,485
|315,95 -> 964,744
|386,749 -> 553,916
|698,985 -> 59,346
|380,969 -> 820,969
|134,641 -> 310,641
|66,899 -> 860,105
|827,304 -> 560,37
|824,213 -> 132,905
|93,138 -> 200,245
|54,450 -> 54,436
|163,276 -> 31,408
|959,333 -> 976,333
|499,129 -> 645,129
|924,321 -> 924,332
|629,166 -> 629,901
|505,769 -> 864,410
|80,360 -> 48,360
|22,971 -> 983,10
|500,87 -> 721,87
|227,737 -> 227,540
|698,177 -> 698,713
|20,106 -> 20,204
|187,176 -> 902,891
|803,806 -> 574,806
|413,785 -> 413,858
|736,728 -> 28,728
|158,870 -> 20,870
|78,475 -> 75,472
|481,434 -> 80,434
|975,208 -> 425,208
|59,639 -> 304,639
|287,919 -> 923,283
|940,933 -> 104,97
|699,429 -> 466,429
|240,988 -> 929,299
|970,873 -> 396,873
|441,585 -> 441,697
|337,259 -> 36,259
|45,759 -> 45,382
|220,468 -> 373,315
|10,262 -> 793,262
|753,268 -> 147,874
|664,560 -> 603,560
|974,81 -> 974,26
|575,331 -> 254,331
|754,378 -> 727,378
|354,394 -> 298,450
|108,814 -> 108,804
|848,375 -> 604,375
|697,530 -> 697,288
|977,54 -> 967,44
|241,754 -> 484,754
|793,745 -> 755,745
|974,415 -> 974,98
|677,367 -> 677,731
|69,744 -> 695,118
|50,39 -> 940,929
|414,721 -> 414,917
|870,587 -> 870,396
|881,759 -> 583,759
|679,798 -> 679,226
|826,917 -> 217,917
|896,919 -> 102,919
|528,881 -> 498,881
|15,89 -> 15,212
|887,280 -> 747,280
|763,303 -> 967,507
|904,964 -> 562,964
|157,752 -> 433,752
|752,199 -> 34,917
|60,970 -> 921,109
|934,434 -> 683,183
|748,427 -> 748,601
|20,47 -> 957,984
|71,691 -> 332,691
|782,637 -> 782,137
|483,623 -> 704,623
|11,965 -> 274,965
|285,172 -> 700,172
|613,845 -> 855,845
|955,296 -> 738,296
|596,750 -> 596,974
|332,851 -> 360,851
|176,388 -> 107,388
|116,328 -> 325,119
|932,588 -> 403,588
|247,420 -> 247,69
|52,965 -> 52,740
|541,579 -> 541,48
|321,530 -> 321,115
|124,33 -> 986,895
|575,950 -> 922,603
|231,814 -> 755,814
|940,445 -> 957,462
|365,743 -> 432,810
|488,286 -> 777,286
|988,905 -> 887,905
|715,846 -> 842,846
|481,894 -> 481,954
|297,868 -> 297,441
|368,928 -> 368,348
|951,981 -> 49,79
|46,743 -> 551,743
|900,241 -> 50,241
|441,942 -> 441,762
|488,58 -> 488,482
|945,187 -> 945,639
|263,779 -> 476,779
|593,290 -> 593,985
|781,498 -> 914,498
|351,557 -> 351,324
|809,966 -> 709,966
|912,545 -> 522,155
|985,681 -> 903,599
|837,881 -> 532,881
|728,485 -> 753,460
|79,680 -> 79,448
|917,769 -> 917,456
|135,845 -> 135,777
|460,922 -> 981,401
|516,830 -> 516,403
|230,419 -> 230,267
|770,297 -> 583,297
|577,782 -> 488,693
|118,728 -> 791,55
|983,192 -> 983,340
|307,252 -> 393,338
|83,345 -> 83,628
|318,461 -> 318,670
|809,385 -> 809,383
|145,124 -> 444,124
|906,892 -> 906,990
|295,22 -> 295,617
|828,312 -> 828,227
|546,132 -> 546,100
|398,750 -> 398,237
|27,770 -> 299,770
|863,517 -> 863,869
|700,806 -> 168,806
|279,262 -> 169,152
|954,285 -> 681,12
|896,102 -> 127,871
|527,532 -> 404,532
|853,397 -> 168,397
|642,273 -> 595,273
|422,123 -> 422,805
|879,789 -> 879,177
|233,171 -> 917,855
|82,148 -> 559,148
|95,969 -> 95,65
|876,779 -> 876,257
|753,35 -> 51,737
|421,756 -> 735,756
|637,602 -> 637,886
|275,689 -> 275,360
|714,77 -> 714,353
|617,645 -> 764,645
|131,702 -> 689,702
|752,795 -> 524,567
|606,716 -> 632,716
|261,700 -> 261,104
|196,894 -> 377,713
|812,626 -> 812,76
|739,368 -> 360,747
|976,646 -> 976,981
|459,948 -> 500,948
|154,669 -> 179,644
|410,896 -> 661,645
|312,187 -> 248,123
|507,655 -> 513,655
|649,726 -> 649,447
|874,19 -> 24,19
|951,64 -> 57,958
|29,60 -> 533,60
|559,262 -> 941,644
|665,319 -> 531,185
|835,601 -> 918,601
|269,556 -> 871,556
|410,708 -> 410,163
|271,105 -> 860,105
|143,427 -> 475,95
|716,656 -> 716,270
|111,771 -> 822,60
|170,775 -> 233,775
|730,588 -> 730,106
|592,161 -> 901,470
|486,802 -> 289,802
|113,770 -> 726,157
|127,98 -> 825,796
|392,957 -> 794,957
|529,793 -> 529,117
|347,196 -> 347,310
|893,613 -> 360,80
|961,362 -> 961,399
|577,52 -> 577,566
|792,578 -> 792,622
|35,883 -> 35,395
|499,779 -> 499,752
|921,359 -> 921,90
|557,224 -> 290,224
|863,190 -> 874,190
|100,190 -> 100,519
|909,38 -> 33,914
|44,421 -> 98,421
|731,787 -> 731,462
|722,28 -> 722,685
|913,538 -> 274,538
|149,648 -> 149,186
|302,594 -> 521,594
|927,333 -> 546,714
|503,42 -> 333,42
|474,260 -> 835,621
|825,363 -> 825,188
|335,140 -> 227,32
|914,613 -> 914,15
|955,734 -> 398,734
|506,913 -> 506,232
|612,271 -> 612,881
|155,856 -> 282,856
|207,820 -> 898,129
|329,463 -> 475,463
|371,719 -> 371,737
|103,503 -> 103,722
|846,779 -> 360,293
|597,247 -> 253,247
|305,912 -> 305,960
|283,277 -> 283,358
|369,327 -> 418,327
|107,190 -> 107,269
|469,247 -> 918,247
|944,213 -> 990,213
|930,158 -> 594,158
|459,844 -> 459,519
|493,387 -> 107,387
|514,173 -> 669,328
|818,876 -> 73,131
|405,783 -> 405,54
|583,151 -> 583,654
|211,462 -> 211,938
|639,224 -> 639,728
|561,33 -> 561,719
|607,244 -> 47,244
|772,61 -> 772,795
|777,920 -> 63,206
|121,281 -> 90,281
|668,99 -> 893,99
|352,620 -> 652,620
|589,50 -> 547,50
|845,252 -> 845,974
|666,870 -> 94,870
|724,524 -> 344,904
|569,40 -> 482,40
|224,619 -> 741,619
|935,110 -> 143,902
|107,37 -> 906,836
|308,298 -> 821,298
|983,619 -> 868,734
|659,166 -> 497,328
|60,423 -> 60,507
|483,283 -> 90,283
|568,309 -> 568,873
|262,461 -> 890,461
|328,633 -> 94,867
|892,195 -> 177,910
|310,874 -> 915,269
|647,54 -> 531,54
|749,523 -> 408,864
|597,882 -> 857,882
|756,498 -> 468,498
|671,248 -> 671,510
|676,855 -> 130,309
|46,18 -> 659,18
|144,164 -> 87,221
|412,774 -> 412,930
|67,368 -> 745,368
|246,704 -> 619,331
|714,113 -> 159,113
|203,109 -> 203,641
|418,708 -> 110,708
|619,546 -> 619,531
|617,957 -> 545,957
|630,423 -> 630,909
|584,379 -> 679,474
|572,918 -> 572,16
|101,712 -> 755,58
|167,317 -> 912,317
|836,130 -> 570,130
|759,847 -> 523,847
|901,40 -> 901,479
|908,157 -> 908,558
|351,252 -> 280,181
|648,730 -> 253,730
|330,984 -> 110,984
|717,552 -> 746,581
|206,831 -> 904,133
|684,232 -> 684,20
|889,73 -> 155,73
|896,728 -> 896,589
|55,983 -> 988,50
|342,746 -> 342,617
|138,683 -> 795,26
|968,226 -> 787,226
|311,713 -> 355,713
|201,789 -> 201,401
|909,623 -> 205,623
|540,130 -> 540,722
|695,511 -> 191,511
|221,320 -> 221,68
|75,118 -> 599,642
|982,977 -> 30,25
|534,598 -> 720,784
|355,579 -> 13,921
|137,516 -> 137,665
|510,244 -> 510,392
|845,52 -> 327,570
|167,100 -> 909,100
|573,133 -> 34,672
|371,341 -> 371,823
|959,981 -> 20,42
|143,793 -> 143,134
|815,798 -> 914,897
|286,212 -> 188,114
|28,987 -> 985,30
|652,108 -> 652,540
|978,565 -> 507,94
|817,166 -> 402,166
|250,723 -> 128,845
|676,274 -> 53,897
|981,985 -> 12,16
|682,195 -> 270,195
|409,113 -> 114,113
|585,83 -> 585,514
|897,978 -> 569,650
|158,420 -> 158,709
|603,335 -> 966,698
|484,53 -> 972,541
|149,128 -> 149,329
|555,926 -> 555,393
|143,849 -> 614,378
|816,79 -> 507,79
|525,502 -> 812,502
|736,509 -> 736,579
|612,179 -> 629,179
|821,70 -> 821,566
|665,90 -> 416,90
|63,80 -> 837,854
|151,873 -> 890,134
|698,204 -> 698,973
|936,693 -> 612,369
|369,628 -> 947,50
|308,407 -> 308,590
|367,156 -> 963,752
|949,943 -> 536,530
|589,553 -> 256,886
|348,171 -> 348,759
|546,488 -> 546,585
|351,486 -> 779,58
|952,45 -> 33,964
|187,896 -> 187,100
|725,679 -> 725,486
|756,147 -> 264,639
|281,205 -> 281,764
|630,221 -> 73,221
|919,953 -> 775,953
|552,821 -> 925,821
|294,854 -> 922,226
|772,642 -> 961,831
|705,159 -> 308,556
|107,983 -> 393,697
|403,277 -> 403,802
|719,824 -> 719,187
|947,956 -> 253,956
|453,135 -> 952,135
|905,491 -> 574,160
|498,225 -> 764,491
|741,111 -> 597,111
|881,130 -> 881,668
|308,616 -> 866,616
|172,434 -> 855,434
|218,412 -> 218,861
|293,318 -> 293,770
|712,513 -> 679,513
|100,586 -> 434,920
|338,407 -> 176,407
|481,534 -> 715,768
|805,127 -> 216,127
|406,425 -> 322,425
|154,769 -> 823,100
|813,508 -> 159,508
|286,637 -> 286,24
|55,836 -> 665,836
|571,857 -> 459,969
|414,977 -> 414,604
|133,884 -> 972,45
|383,522 -> 383,141
|889,14 -> 62,841
|122,49 -> 43,49
|544,846 -> 544,76
|948,19 -> 62,905
|798,577 -> 833,542
|973,267 -> 751,45
|620,648 -> 620,419
|102,813 -> 633,813
|255,263 -> 826,263
|231,394 -> 231,811""".stripMargin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment