day 14 part 1 and 2

This commit is contained in:
Tyrel Souza 2022-12-14 02:38:19 -05:00
parent 0a168874f0
commit 1741640930
6 changed files with 339 additions and 2 deletions

134
2022/full/day14.txt Normal file
View File

@ -0,0 +1,134 @@
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
494,16 -> 499,16
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
492,52 -> 496,52
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
510,123 -> 515,123
492,48 -> 496,48
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
501,54 -> 505,54
513,120 -> 518,120
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
483,54 -> 487,54
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
532,129 -> 537,129
501,16 -> 506,16
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
520,120 -> 525,120
489,50 -> 493,50
498,19 -> 503,19
505,151 -> 505,152 -> 518,152 -> 518,151
510,73 -> 514,73
516,77 -> 520,77
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
511,129 -> 516,129
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
524,123 -> 529,123
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
507,75 -> 511,75
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
514,149 -> 524,149 -> 524,148
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
504,129 -> 509,129
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
504,77 -> 508,77
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
516,117 -> 521,117
514,149 -> 524,149 -> 524,148
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
507,126 -> 512,126
513,75 -> 517,75
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
505,151 -> 505,152 -> 518,152 -> 518,151
510,77 -> 514,77
528,126 -> 533,126
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
518,129 -> 523,129
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
491,19 -> 496,19
505,19 -> 510,19
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
506,135 -> 517,135 -> 517,134
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
506,135 -> 517,135 -> 517,134
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
497,13 -> 502,13
489,54 -> 493,54
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
505,69 -> 505,70 -> 511,70 -> 511,69
487,45 -> 487,38 -> 487,45 -> 489,45 -> 489,42 -> 489,45 -> 491,45 -> 491,39 -> 491,45 -> 493,45 -> 493,39 -> 493,45
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
525,129 -> 530,129
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
505,151 -> 505,152 -> 518,152 -> 518,151
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
521,126 -> 526,126
512,106 -> 512,108 -> 505,108 -> 505,114 -> 518,114 -> 518,108 -> 516,108 -> 516,106
495,50 -> 499,50
495,54 -> 499,54
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
508,103 -> 508,93 -> 508,103 -> 510,103 -> 510,101 -> 510,103 -> 512,103 -> 512,96 -> 512,103
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
498,52 -> 502,52
517,123 -> 522,123
514,126 -> 519,126
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
495,22 -> 495,25 -> 492,25 -> 492,32 -> 501,32 -> 501,25 -> 499,25 -> 499,22
505,69 -> 505,70 -> 511,70 -> 511,69
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
512,90 -> 512,87 -> 512,90 -> 514,90 -> 514,86 -> 514,90 -> 516,90 -> 516,87 -> 516,90 -> 518,90 -> 518,82 -> 518,90 -> 520,90 -> 520,81 -> 520,90 -> 522,90 -> 522,86 -> 522,90 -> 524,90 -> 524,84 -> 524,90 -> 526,90 -> 526,82 -> 526,90 -> 528,90 -> 528,82 -> 528,90
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
520,165 -> 520,160 -> 520,165 -> 522,165 -> 522,163 -> 522,165 -> 524,165 -> 524,159 -> 524,165 -> 526,165 -> 526,159 -> 526,165 -> 528,165 -> 528,155 -> 528,165
505,69 -> 505,70 -> 511,70 -> 511,69
505,57 -> 505,59 -> 498,59 -> 498,66 -> 509,66 -> 509,59 -> 507,59 -> 507,57
502,138 -> 502,141 -> 498,141 -> 498,145 -> 514,145 -> 514,141 -> 507,141 -> 507,138
486,52 -> 490,52

146
2022/python/day14.py Normal file
View File

@ -0,0 +1,146 @@
import matrix
import shared
import itertools
import functools
SAND_START = [0,500]
def get_lines(rows):
_rows = []
for row in rows:
_row = []
for point in row.split(" -> "):
_row.append(list(reversed(list(map(int, point.split(","))))))
_rows.append(_row)
return _rows
def find_bounds(lines):
x = []
y = []
for line in lines:
x.extend(matrix.split_x_out(line))
y.extend(matrix.split_y_out(line))
return shared.minmax(y),shared.minmax(x)
def part1(rows):
def get_next(sand):
next_y = sand[0]+1
dl = sand[1]-1
d = sand[1]
dr = sand[1]+1
return next_y, (dl,d,dr)
lines = get_lines(rows)
(minY,maxY), (minX,maxX) = find_bounds(lines)
mx = matrix.matrix_of_size(maxX+2,maxY+2)
walls = []
for line in lines:
for x in range(len(line)-1):
coords = matrix.coords_between_points(line[x], line[x+1])
walls.extend(coords)
for y,x in walls:
mx[y][x] = "#"
mx[SAND_START[0]][SAND_START[1]] = "+"
sands = []
try:
for sand_grains in range(900): # produce Sand one unit at a time
last_grain = []
sand = SAND_START[:]
#print("starting sand", sand_grains+1, sand)
while True:
last_grain.append(sand[:])
next_y, (dl, d, dr) = get_next(sand)
if mx[next_y][d] == 0:
#print(mx[next_y][d],"moving_down", end=' ')
#print("moving down")
sand[0] = next_y
elif mx[next_y][dl] == 0:
#print(mx[next_y][dl], "moving left", end=' ')
#print("moving left")
sand[0] = next_y
sand[1] = dl
elif mx[next_y][dr] == 0:
#print(mx[next_y][dr], "moving right", end=' ')
#print("moving right")
sand[0] = next_y
sand[1] = dr
else:
mx[sand[0]][sand[1]] = "o"
#print(mx[next_y][dl], mx[next_y][d], mx[next_y][dr])
#print("STOPPING", end=' ')
break
#matrix.view_matrix(mx, 0,minX-1, maxY+1,maxX+2)
except IndexError:
sands.append(last_grain)
for y,x in sands[-1]:
mx[y][x] = "~"
#matrix.view_matrix(mx, 0,minX-1, maxY+1,maxX+2)
print("done", sand_grains)
def part2(rows):
def get_next(sand):
next_y = sand[0]+1
dl = sand[1]-1
d = sand[1]
dr = sand[1]+1
return next_y, (dl,d,dr)
def check_air(y,x):
if y == floor:
return False
if (y,x) in walls:
return False
return True
lines = get_lines(rows)
(minY,maxY), (minX,maxX) = find_bounds(lines)
walls = set()
for line in lines:
for x in range(len(line)-1):
coords = matrix.coords_between_points(line[x], line[x+1])
for c in coords:
walls.add(c)
for y,x in walls:
walls.add((y,x))
floor = maxY + 2
sand_grains = -1
while True:
sand_grains += 1
sand = [0,500]
if tuple(sand) in walls:
print("done", sand_grains)
break
while True:
next_y, (dl, d, dr) = get_next(sand)
if check_air(next_y, d):
sand[0] = next_y
elif check_air(next_y,dl):
sand[0] = next_y
sand[1] = dl
elif check_air(next_y, dr):
sand[0] = next_y
sand[1] = dr
else:
walls.add(tuple(sand))
break
#matrix.view_matrix(mx, 0,minX-1, maxY+3,maxX+2)
def main():
rows = [row for row in shared.load_rows(14)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())
if __name__ == "__main__":
main()

View File

@ -1,5 +1,6 @@
from copy import deepcopy
from collections import defaultdict
import math
from typing import List, Dict, Tuple
@ -7,6 +8,12 @@ split_word_to_chr_list = lambda y: [w for w in y]
split_word_to_int_list = lambda y: [int(w) for w in y]
split_line_to_int_list = lambda y: [int(w) for w in y.split(" ") if w]
def split_x_out(l):
return [x for _,x in l]
def split_y_out(l):
return [y for y,_ in l]
class colors:
# HEADER = '\033[95m'
@ -188,6 +195,39 @@ def line_of_sight(mx, row, col):
return los
def coords_between_points(point1, point2):
y1,x1 = point1
y2,x2 = point2
coords = []
x = 0
y = 0
if x2 < x1:
y = point1[0]
for _x in range(x2, x1+1):
coords.append((y,_x))
elif x1 < x2:
y = point1[0]
for _x in range(x1, x2+1):
coords.append((y,_x))
elif y2 < y1:
x = point1[1]
for _y in range(y2, y1+1):
coords.append((_y,x))
elif y1 < y2:
x = point1[1]
for _y in range(y1, y2+1):
coords.append((_y,x))
return coords
def get_size(matrix):
height = len(matrix)
width = len(matrix[0])
@ -241,13 +281,14 @@ def ppmx(*matrices, pad=True, space=True, zero="."):
"""
print a matrix of anything, Falsy values turns to `.` for clarity
"""
out = []
if len(matrices) > 1:
matrices = list(zip(*matrices))
for row in matrices:
r = []
for col in row:
r.append("".join([f"{x or zero}".rjust(3) for x in col]))
print(" ".join(r))
out.append(" ".join(r))
else:
for row in matrices:
for c in row:
@ -259,7 +300,17 @@ def ppmx(*matrices, pad=True, space=True, zero="."):
f = lambda x: f"{x or zero}"
if space:
f = lambda x: f"{x or zero} "
print("".join([f(x) for x in c]))
out.append("".join([f(x) for x in c]))
return "\n".join(out)
def view_matrix(matrix, y1,x1, y2,x2):
print(y1,y2)
print(x1,x2)
lines = ppmx(matrix, pad=0,space=0).split("\n")
for line in lines[y1:y2+1]:
print(line[x1:x2])
def highlight(matrix, red=[], green=[], blue=[], blink_green=[]):

View File

@ -5,6 +5,9 @@ from pathlib import Path
spl = lambda y: [int(w) for w in y]
def minmax(l):
return min(l),max(l)
def load_rows(day):
return [row for row in load(day)]

2
2022/samples/day14.txt Normal file
View File

@ -0,0 +1,2 @@
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9

View File

@ -5,3 +5,4 @@ numpy
pandas
scanf
scikit-image
dijkstar