Logo FazeCT
Table of Contents

Day 6: Trash Compactor

Part 1

After helping the Elves in the kitchen, you were taking a break and helping them re-enact a movie scene when you over-enthusiastically jumped into the garbage chute!
 
A brief fall later, you find yourself in a garbage smasher. Unfortunately, the door's been magnetically sealed.
 
As you try to find a way out, you are approached by a family of cephalopods! They're pretty sure they can get the door open, but it will take some time. While you wait, they're curious if you can help the youngest cephalopod with her math homework.
 
Cephalopod math doesn't look that different from normal math. The math worksheet (your puzzle input) consists of a list of problems; each problem has a group of numbers that need to be either added (+) or multiplied (*) together.
 
However, the problems are arranged a little strangely; they seem to be presented next to each other in a very long horizontal list. For example:
 
123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  
 
Each problem's numbers are arranged vertically; at the bottom of the problem is the symbol for the operation that needs to be performed. Problems are separated by a full column of only spaces. The left/right alignment of numbers within each problem can be ignored.
 
So, this worksheet contains four problems:
- 123 * 45 * 6 = 33210
- 328 + 64 + 98 = 490
- 51 * 387 * 215 = 4243455
- 64 + 23 + 314 = 401
 
To check their work, cephalopod students are given the grand total of adding together all of the answers to the individual problems. In this worksheet, the grand total is 33210 + 490 + 4243455 + 401 = 4277556.
 
Of course, the actual worksheet is much wider. You'll need to make sure to unroll it completely so that you can read the problems clearly.
 
Solve the problems on the math worksheet. What is the grand total found by adding together all of the answers to the individual problems?
6/problem/input.txt
49 9493  847 816 6  7131 86  51 323 87  9 71 1789 356  163  7 94 88 3751 314 916 1  42   57  1 234  6 734 762  92  74 18 1  17 417  16 53 733 691  735 141 2584 32 42  42  86 53 6657 812 64 261 583 39 44 325 36 413 77 6792 99 478 68  88  654 134   9 834 7516 2457    9   86 793 974 871 4   646 345 45 465 988 74 1  58  73  2 83 617 5144 66 71 37   85 53  44 2474 314  3 558  3  5636  32 93 86 68 3    3 79  8   1  49  3 22  171 76 56 45 12 9  858 27 494 816 98 71 289 98  244 9326   75 6   4  6 64  66 492 385    4 6219 6   19  6 25 4169 383 58   46 65 276 33 5839 569 79 74 6526 98 85 996 587 46 133 17 92  86 3737 473 455 552 5  953 11 93 84 873 342 72 693 69 5318 96 24 9173 4414 73   5 82 927  2 84 28   9 67 7  63 42 971 735  2 731 48 529 741 71 99 65 784 135 84 77 83  349 428 41  635 78 44    5 319 4  262   9 548 788 294 94  5966  6 366 1925 19 76  83 237 462 3275 2   35   1331   4 75 34   69 64 455 781 26 5439  68 65 379 14  56    87  79 32  55 353 813 9974 283 64 6   64 1876 47 9     17 14 17   86    9 927 6   53  534 277 7426 8534 2  176 57 52 41 39 66 465  7 56 55  9444  93 751 51  9931 253 792 698 49  98 35 17 129 45 2145  5 29 7    31 515 237 62  36  39 7  473 212 41  73 254 94 7151  64 93 29 5  868 3  5   62 8  913 16 8  13  9  86 49   5 78 57 98  7   35   9  45  34  5 41 49 18 64 347 439   9 72 99 83 922 37 945 6938 26 35 563 37  8 7243 819  2 181  77   51 7598 82   3  986 93 496  91 77   92 6757 32 21 868  6  21  9 15   2244 396  44 827 383 91   92 949 89 12  6   8765 624 4125 6995 85   5 59 76 211 29 25 96  85 8683 77  8 72  33 2789 368 265 8943 5394  7    8 136 19 9929  75 61 491 43 781  914 36 1284 32   3  18 235 589 386 4273 91  7 55 643   8 26   72  9 1   5    94 66    5 5   283 4  61   47 347  8 96 938 2926 2   47  8 64 648 215  28 5464   6 8    8  666 9  541 9315 47 5  45 782 61 141 124 571 99    33 26  8 625 5   8    57 5   844 14 93 5853 11  87 897 9367 82  17 143 5  84    2 24 93   929 21 18 3  379  78 2   464   3 871 815 46 297 91   9   1164 14 95 424  2 19 874 9695 11 715 512 281  58 598 3615   9 94  993 66 16 2    2  17 331 63 916 172 24 3  4423   78  8  48 252 835 21 4876 74 369  4 961 94 35   198 58 6  91 9498   13   4 748   89 448 192 548 589 85  38   8 45 85  612 65  21 55 626 45  251 347 2573 74  21 43 8  361 966 1391  81 18 29 61  1 2   46 59  898 3121 972 6257 484  15 926 37  7 697 7143 1  38 81 351 98 45  462 3193 822 49 82 6   331 52 84 674 51 1376 4  15 76 7  845   69  461 994 2568 643  7 916 691 3162 8  57 85 59  22   8  86 715 28  4  86 41 43 666 9    731 713 9452   27 98 858  434 3268 42   7 847 84 8    79 59 825 56   59 88 32 526 29     5 6749 9  945 82 9  95    7 61   66 2988 67 14 53 73  736 841 538 768 816 538 66 8  11 24  876  4 76 87 57  244   51 6  43 776 9   5 67 69  9  235 23 924 952 42 3273 1341  5 53  1599 46 53 64 86 73  459 146 363 466  4  9 777  1 17 958 885  123 93  8 84   64 46  8378 272  917 99 1   2355   1 978 11  16 9223   3 93 656 3234 26 89  3 1711 281 795 832 62   44   81 413 49 59   8 81 98  286 63   7   47  32 333 1   29 1543 81  465 18 582 7   2 33 398    7 632 6   75 8634 97  54 287  27 41  5 829 19 37 34 18 47  74 37 79  95  4  34 682 5357 48 818 166 41  1 122 58  982   2 75 25 776 446   1 97 91  57 294  79 49 49    14 35  28 58 57  1   15 96 268   5 8    16 97 24 62  84 6382 565 62  7 31 56 95 45  51 19 77   8 55 239  9 2  2    37  51 91 12 68 78  9437 78  3 33 22   3 9  92 2751 7462  6 5298  85 7495 796  8  76  36 921  2 45 195 44 288 42 784 17 24  8 894 619 2276  4 8849 572 19 33 4  165  27 67   519 3365 8   18   35 7  9342   4 74   25 15 6878   5   8 46 26  114 87 83 73 2  45   99 593 6   68 788 3464 815 5325 5  7279 98 47  24 97 92 7339 6  72 44 924  6   1 22 871 69 9   45 923 868 499 9    4 34   92  8 233 479 24 52
83 9643  262 113 91 3284 778 71 966 43 91 33 7759 156  265 25 54 79 4869 691 537 57 9341 35 25 242  2 269 956 118  27 23 1  82 698  15 15 652 1446 367 776 6562 54 19  98  89 25 8683 767 29 553 626 65 39 436 53 615 81 2956 44 546 547 55 4511 555 687 915 6713 6749   27  431 245 24  935 99  783 629 17 626 674 11 38 999 44 22 81 196 7938 51 55 54   46 84  83 7591 553  9 199  4  5294  75 68 93 15 61  28 582 78  95 81  5 46  91  67 71 87 96 67 383 88 731 471 33 59 766 38  927 5321   26 2   2  2 75  57 825 529  887 8711 66  73  8 85 1268 534 24   97 44 954 39 571  541 26 49 845  85 34 869 479 69 717 26 78  31  665 668 558  94 95 317 11 39 13 249 675 74 278 65 12   85 41 321  4787 91   2 34 611 42 71 24 892 38 5  36 69 579 872 19 288 88 874 113 79 87 78 831 568 27 21 58  621 435 39  534 55 16   91 112 5  984 346 182 642 877 155 7673 18 523  494 61 23  96 925 437 8675 655 7658 8946  57 77 934  21 26 882 331 98 3858 691 91 587 621 3591 445  69 45  77 871 882 6713 942 43 56  88 5784 21 82  5183 95 12   72  869 162 22  55  698 72   261 7696 38 155 1  74 34 88 53  63  5 99 937 1834 616 943 372 326  885 344 951 136 41 93 16 774 74 893   8 25 46  166 667 358 61 328 292 16 719 599 23  63 958 22 1136 652 28 36 6  941 88 1   87 52 785 51 14 76  2 324 46  21 98 12 312 75  28   2  696 47  4 53 77 62 56 569 354   6 81 11 62 276  5 125 7492 36 63 232 33 22 6868 446 41  19  74 3464 6226 723  9 2989 32 2561 98 96  835 5984 53 67 636 74 466 74 18   3835 997  65 122 74  66 8554 583 93 599 98  9284 871 4862 4159 43  44 25 31 796 98 35 191 21 9238 41 37 27  23 6245 132 933 7352 3527  5  727  89 15 2218 534 99 953 54 192  755 84 9455 34  55  68 744 984 473 285  26  7 83 664  44 71 1353 72 85  82   98 26   74 3   223 57 58  232 288  9 99 72   773 928 65  3 98 432 1121 24 6275  57 779  44 818 65 99  9413 66 9  47 568 19 386 654 796 86    57 58 27 364 642 2    59 416 496 47 61 1419 76  64 284 5426 27  23 547 25 488  72 97 2183 221 59 14 7  788  76 45  854 283 58  252 33 446 884  85  7287 18 85 886  3 93  79 9117 73 692 794 977  67 739 6671 784 657 899 4  32 63 757  47 911 48 329 634 77 12 6878 5868 58 483 719 17  31 2296 68 84  54 673 41 385  883 33 6  54   82  994  63 876  569 481 753 896 985 11  76  29 84 93  991 234 94 88 915 59 8889 298 5969 49 837 89 72 739 235 568  227 91 89 36 82 45  23 93  296 6225 143  885 446 946 238 81  6 215 8857 1  76 12 21  58 77   19 4193 782 81 74 48  342 6  46 661 85 3698 5  71 33 44 93   264  132 258 7798 451  5 757 881 2545 1  97 94 497 427  35 24 667 723 57 33 19 26 319 24   651 39  5255 9689 56 5239 789 5533 78  41 573 82 46  176 46 746 862  56 82 51 143 44    15 8641 9  635 66 68 235   4 47   19 5863 98 37 29 92 6863 953 293 481 312 639 97 8  95 75  881 35 82 52 842 582   42 52 94 231 32  7 47 12  99  12 79 256 614  2 5231 4367  8 225 5843 79 18 98 47 38  786 322 452 139  6  8 521  6 22 637 364  196 21  5 523  84 635 7486 316  197 72 865 8186  89 344 92  98 7982  87 96 622 7772 74 48 55 226  177 433 823 8679 16  774 328 29 354  7 67 261 899 5478 88  665 96 236 431  2 8555 139 629 31 787 7  44 54 775 4474 739 78  96  455 83  68 343  99  7 58 388 65 18 9  52 76  12 77 691 41  5  42 278 1391 85 343 233 29 22 531 134 779  19 4  18 161 259 676 64 271 42 544 741 52 61   632 28 154 65 325 18  41 75 414  31 8   519 79 42 957 27 5192 917 77 94 86 61 53 97  81 78 42  36 55 82  89 34 7542 57  34 62 65 44 47  1455 14 79  2 82  22 1  97 1432  515  2 8486  46 5644 556 67  71 962 674 67 58 231 13 794 27 439 69 25  4 456 415 297  69 4251  83 25 88 3  973 959 16   943 1289 39  66   39 93  777 768 511 847 6  3859   1  23 85 411 326 56 43 78 95 31  215 881 69  76 468 1663 424 642  14  177 64 46  78 42 29 9132 3  81 2  629 19   4 95 676 87 8   46 679  18 767 17  74 52  666  9 851 516 26 36
16 959  5389 422 48 7167 663  8 722 14 26 85 8579 924 6241 48 31 94 1794  54 693 67 5158 5  14 428  5 56  924 251  31 99 4  13 925 769 7  693 4785 993  73 279  25 28 189 962 56 7533 748 48 299 752 44 89 272 32  41 56 5597 48 89  753 67 7348  28 664 927 918     1 7578 1553 316 89  973 762  24  61 31 361 198 77 92 525 4  48 41 729 8579 72 95 2974 83  9 622 8773 62  54 925  28 4544 128 62 74 5  976 79 785 932 36 41  8 54  3   64  6 57 46 58 868 49 91  457 7  22 122 21  429 782  4796 55 87 37 79 779 673 331  841 2286 39  32 95 18 5696  46 5781 68 39 865 59 26   975 29 11 85   94 81 381 71  63 722 37 295 36  969 62  321  27 48  33 55 94 1  335 695 21 8   98 38   34 28 21   9987 86 748 38 825 27 22 86 166  9 39 36 14 531 596 71 23  23 21  894 91 77 83 398 172 8  21 435 87  481 46  25  85 5545 57 489 43 535 582 883 447 95  548 2325 43 763  129 67 36  2  132 369  175 267 7141   69 166 1  9526 85 55 536 94  19 9144 377  7 838 788 1552 818  67 52  23  52 12  3535  43 78 725 84 7563  2 276 8861 17 777 198 2817 545 79  19 9965 37     9 3746 36 975 2  4   2 28 61  96  6 17 915   98 392  88 314 647  289 65   41 232  7 18 81 112 69 81   49 88 545 842 657 11  76 612 321 17  29 249 18 468 912 96 4656 971 63 29 92 646 24 8  236 98 875 72 44 9  55 683 6  353 54 85 891 235 7938 3  542 68 67 18 98 85 92 432  54 137 41 48 29 24   1 122 7866 41 47 35   7 54  163  96 83  35 117 2365 8961 791 53 6847 68 2271  6 45 4872 9232 49 27 273 81 927 44 6448  541 561 369 441 18   4 1164 393  6 871 344 56   427 7185 8111  1  16 91 36 546 73 93 179 69 3567 84 66 96  93 1218 637 613 9426  796 97 2132  99 42 9764 786 21  65 52 764 9274 23  882 65 614 664 454 35   83 337  25 47 25 245 667 21 6687 48 615 349  83 99 6873 32  155 84 3  1191  44  2 66 71   866 849 67 67 53 692 9933 17 4371  47 482  94 77  68 12   421 41 27 16  93 5   79 772  16 46   618 14 37 728 342 6145 83 438 278 34 44 293  58  72  95   15 96 314 97  87 1542 82 36 9554 863 41 36 14 447 279 962 958 356 3   636  2  48 7865 558   55 77 87 452 13 66  75  825 37 797 644 9359 65 942 8986 783 298 666 2  17 24 194 986 994 5  588 24  13 95  619 8933 27 957 65  85  64 8746 58 14  16 726 81 541  597 69 9   6   64 8475 831 267 5191 94  866  69  32 52  97 789 92 86  351 697 54 71 124 46 3485 298  964 42 949 84 38 161  19 484  185 47 23 97 84 355  1 34 5773 62    49  393 77  728 38  11 42 648 471  6  99 52 86  76 883  64 446  751 82 77 947 379 8  68 235 36 1913 88 28 28 69 67   851 9527 21  4313  34 23 952 153 14   21 52 71 574 2343 19 51 88  631 97 97 45 74 139 9686 564 1    768 8916 24 3665 382 4416 82 136 382 17 899 928 38 437 5262 96 48 47 335 791  693 2182 2  331 23 91 353  97 1    97 1328 65 96 55  8 4687 983 354 621 14  813 35 17 41 897 949 74 25 56 687 213 6179 67 94 269 92  7 56 118 77  54 49 787 98   4 242  8479 19 122  319 39 41 78 87 231 592 568 363 615 62 17 25   6 74 885 594  148  9 13 313  76 191  277 892  96  89 196 176  494 618 278 67 66   226 1  174  271 18 8  42 89   53  33  446 3623 91 4656 873 33 826 12 39 254 622 5238 441 891 2  814 845  4 6697 256 469 92  53 25 87 86 569 9549 312 792 2   515 356 87 666 285  8 64 927  7 32 6  39 12 497 52 367 72 84  53 228  692 84  18 838 48 73 268 125 69  869 3   3 916 564 439  5 959 23 281 283 37 555  144 51 499 93 431 271 34 73 426 123 37 4365 26 14 167 58 9727 856 34 91 54 62 97 692 11 42 32 169 66 44  29 43 9583 486 15 88 6  38 938 537   1 83  2 69 943 4  55 948   646 72 324  769 2444 166 45 976 757 998 96 77   3 76 18  33 731 86 38 51 58  133 473  98 466   86 91 46 87 575 548 278  583  532 195 279  55 82  159 355 354 459 4  866   14 562 35 456 633 87 19 94 85 95 2836 686 21  32 886  693 714 655  68  855  6 66 593 95 61 654  4  84 2   96 82  91 94 531 26 4  327  79  96 741 33 581 14  279 68 543 632 79 57
83 5    2222   2 47  518 117  3   9 96 87 28 1732 92  4877 81 71 65    4   8 528 66 5889 8  57 793 56 4   5   821 183 25 35 34 996 451 7  3   8336 326  37 13   6  27 719 863 7  817   86 56 77   92 86 7  65  41   2 67  711  1 76  635 6  6363   4 469 847 83      7 4168 4219 485 9   736 919  93  12 5  517 773 34 72 533 6  34 52  85 76   7  73 9559 4   7 694 35   78  28 9213 38 23   554 44  6 4  376 68 372 118 35 98 13 638 8   51  8 54 34 86 915 15 25  373 4   8 359 91 8583 14   4463 16 79 54 36 314  29 842 8229 1    233 64 12 56 9494   9 8724 24 1  698 8  76   836 83  4 97   46 1  758 2   1  148  6 652  7  915 15    2  74 17   7  5 78 5    4 31  29 6   11 8    54 21 75     35 11 773  9 164 94 14 59 947  1 96 9  89 258 863 87 9   47 11  275 57 63 14 379 52  8  96 273 1     9 882 8   35 9624 26 485 68 889 486 464 823 5   687   87 41 374   88 67 467 7  892 87    76 298 6368    3 399 7  2815 65 42 8   42  71  989 266  1  34 125 3961 788 576 593 12   3 74  2684   7 34 642 97 94    3 686 8742 23 922 532 9954   6 751 68 8887 77     4 995  31  41 5  7   9  3 92  58 71 2  255    5 564   3 523 57   765 3     9 763  4 5  77 976 59 33   68 73 741 921 746 41  53 726 522 61  85 24  39 159  78 24 9871 533 38 51 76 9   36 68 651 82  78 99 84 2  99 137 3  769 88 4  988 668 6663 96 552 61 77 83 69 52 93 625   7 549 3  11 66 63   8 175    9 72  6 1    8 18    5   5 79  47 649 8416  661 622 53 1225 72 6986  3 24 8259 8585 6  4   19 82 827 36 4449   94 811 262 818 13   9 9759 57   7 444 947 32    94 146  192   3 776 54 85 7   91 43 729 26   81 68 25 389 52 2    66  331  944   34 47 3695   6 5   756 591 95   1 91 294 5551 47   46 18 277 891 334 4    28 332  53 18 83   4 353 78 1259 59 897 118 745 2  5824 48 9654 38 2  7523  13 91 17 47    89 198 31 27 38 123 4573 29 9331 276 8546 12 2   91 79    48  1 64 73  74 4    4 78   83 2112 458 76 32 847 285 1154 13 522  96 21 85 46    5 726   4   45 89 141 18  82 6148 63  2 1367 967 31 84 55 493 127 472 669 626 7   17   5  75 9283 577    7 42 52   1 22 75   1  727 66 62  353 4621 34 337  713 431 251 95  1  78 66 511 555 169 2  467 78  19 31   53 1664 28 447 66  43   6 33    2 54  19 983 66 7969 96  8  33  7   93 8465 831 912 4218 2   854  48  32 53 746 358 27 22 9842 563 11 42 86  1  1358 424    8 4  858 73 44 45   91 25   836 28 99 7  93 284  5 5  3725 87    15   63 55  474 1   29 91 69  4    56 35 79 92  79 233   5 49    36 81 27 129  19 2  15 944 78  782 19 4   3 38 8   4192 3997 7   927    8 13 539  19 78   19 88 65 218 6271 32  2 54  777 51 14 37 14 25  6275 23  1      9 7836 24 6159  91 5729  3 474  88 4  419 646 22   4 9738 3   1 28 628 858 5226   36 38  46 25 97 7529 42 4  5994 7767 9  84 71  8 1679 71  893 31  55  158 27 18  7 689  57 45 45  6 259  14 3141 19  5 9   83 49 98 346 79  27 55  84 8    7 41   6332 91 338  726 38  8  5 42 821 249   6 415 363 52 46 14  76  4   1  99 7435  4 63 3487 23 859   45 3376 8   92 781 541  993 571 492  4 89   279 9    9  715 3  3  94 2    98  23  383 4173 71 6897 622 8  511 31 55 781 113 7761 965 664 5  22  538  4  274 285 635 31  67 53 32 78 59  4319 936 258 9    45 676 99 641 258  7 97 949  8 52 1  41  2 751 1  866 5  96 857 22    12 15   3 845 46 43 698 862 9   869 9   3 141 49  975  6 422 56 557 834 94 9728 644 85 955  9 755 297 72  8 729 871 52 1861 58 17 639 61  284  22 83 52 71 3  14 136 65 26  4 568 8  8   92 52 7419 183 47  2 8  52 736 15    2 88  6 88 493 12 61 31     41 97 8    651   86   4 88 658 367 262 59 64   5 4  2   84 999 52 89 48 19   83 324  38 3      1 47 9  98   8 275 9763 914  662 915 5395 51 93   36 298 284 625 1  943  562 521 22 845 411  6 13  9 58 63 4555 35  644 99 225   12 113 18   94   88  1 7  251 52 98 448  55 59 6   48 53 456 52 493 79 75 857  33  75 68  61 358 17 8381 34 782 91  37 43
*  +    +    *   *  +    *   *  *   *  *  *  +    *   +    +  *  *  +    *   +   *  +    *  *  +   *  +   *   *   *   *  *  *  +   *   *  *   +    +   *   +    *  +  +   *   +  +    *   *  *   *   *  *  *   *  *   *  +    +  *   +   *  +    +   *   *   +    +    +    +    *   *   +   *   +   +   +  +   +   +  +  +   +  +  +  +   +    +  *  +    *  *  *   +    *   *  +    *  +    *   +  *  +  *   *  *   +   *  *  *  +   +   *  *  +  *  +  *   *  +   *   +  +  *   *  +    +    +    +  *  *  *  *   *   *   +    +    *   *  +  *  +    +   +    +  *  *   +  +    +   +  *  +    +  *  *   *   *  *   +  *   *  +    +   *   *   *  +   *  +  *  *   *   *  *   *  +    +  *  +    +    *  +   *  +   *  +  *  *   *  *  +  +  *   *   *  *   *  *   *   *  *  *  +   *   *  +  +   +   +   *   +   +  +    +  *   *  *   +   +   +   *   +   +    +  *   +    +  *   +  +   *   +    *   +    +    +   +  +    *  *  +   *   *  +    *   +  *   *   +    +   *   +   *  *   *   +    *   +  +   *  +    +  *   +    +  +   +   +    +   *   +  +    *   +    +    +  *   +  +  *  +  *  +   *  *  +   +    +   +   *   +    +   +   *   +   +  *  +  +   +  +    *  +  *   +   *   *   *  +   +   *  +   +   +  *   *   *  +    +   +  +  *  *   *  +  *   +  *   +  +  +  *  *   *  +   *  *  *   *   +    +  +   *  +  +  *  +  +  *   +   +   *  *  *  +   *  *   +    *  *  *   +  *  +    +   +  *   *   +    +    *   *  +    *  +    +  *  +    +    +  *  *   *  *   *  +    +    +   *   *   *   *  +    +   *  *   *   +    *   +    +    +  +   *  +  *   +  +  *   *  +    +  +  *   *  +    *   +   +    +    *  +    +   *  +    *   *  *   *  *   +    +  +    *  *   +   +   *   *   +    +  +  +  +   *   *  +    +  *   *   *   *  +    *  +    +  *  +    +   *  +  *   +    *   +  *  *  *   +    +  +    *   +    +  *   +  *   +    *  *  *  +   *  *   +   *   +    +   *  *  *   +   +    +  *   *   *  *  +    *  *   +   +    *  +   +   +  +    *  *  +    +   *  +  +  *   +   +   +   *   *   +   *  *   +    *   +    *  +  *   *  +  +   +    *  +   *   +    *  *   +    *   +   +   *  +  +  +   +   *   *  *   *   *  *  +    +    +  *   *   +   +  +    *  *   *  +   +  +    *   +  *  +  +    +    +   *   +    +   *   *   +   +  +   *   *  *  +    *   *  *  +   *  +    +   +    *  +   *  +  *   +   +    +   +  *  +  +  +   *  *  +    +    *   +    *   *   +   *  +  +   +    +  +  *  *   +  *   *   +    +   +  *  *   *   +  +  +   *  +    +  +  *  *  *   +    +    +   +    *   *  *   *   +    *  +  *  *   +    +  *  +   +   +  *  *  *  *   +    +   *   +    +    *  +    +   +    +  *   *   *  *   *   +  *   +    *  +  *  *   *   +    +    *  *   *  *  +    *  *  +    +    *  *  *  *  +    *   *   +   *   *   *  *  +  *   +   +  *  +  *   *   +    +  *  +   *  +  +  +   *  *   *  *   +   *  +    +    *  *   +    *  *  *  +  +   *   +   *   +   +  *  *   *  *  +   *   +    +  +  +    *  *   +    +    *   *  *   +    *   *   *   *  +    +   *  +   +    *  *  *  +    *   +   +   +    *  +    +   *  +   +  *  +   *   +    *   *   *  +   *   +  +    *   +   +  *   *  +  *  *   +    *   *   +  +    +   *  +   +   *  *  *   +  +  *  +  *  *   +  *   *  +  +   *   +    +  *   *   *  +  *   *   *   *   *  *  +   *   *   *  *   *  +   *   *  +    *   *  *   *  *   *   +  *  *   *   *  +    *  +  *   *  +    +   *  +  *  +  +  *   *  +  *  *   +  *   *  +  +    *   *  +  *  *  +   +    +  *  *  *  *   +  *  +    +    +  +    +   +    +   +  *   *   +   *  *  +   *  +   *  +   *  +  *  *   *   +    *  +    +   *  +  *  +   *   +    *   +    *   +    *  *  +    *   +   *   +  +    *   +   *  *   +   *  +  +  *  *  +    +   +   +  *   +    +   +    +  +    *  *  +   *  +  +    *  *  *  *   +  *   +  +   *  *  *   *   +   *   *  +   +  +    *  *   *   *  + 

Yet another integer conversion problem.

For each line of numbers, we attempt to convert all digit strings into integers by skipping spaces and calling our atoi helper.

To make the process easier, the original atoi helper is modified so that now it pushes the converted integer into a buffer in .bss, then returns the position of the next byte (which is definitely not a digit).

6/solution/modified_atoi.asm
; ----------------------------------------------------------------------
; Component: Modded atoi (String to Int) conversion
; Args: rdi
; Ret: rax = terminated position; insert int into numbers
; Terminate if any byte is not in range [0-9]
 
modified_atoi:
    push r15 
 
    xor rax, rax
 
.convert:
    movzx rsi, byte [rdi]
    test rsi, rsi 
    je .atoi_done
 
    cmp rsi, 10
    je .atoi_done
 
    cmp rsi, '0'
    jl .atoi_done 
 
    cmp rsi, '9'
    jg .atoi_done 
 
    sub rsi, '0'
    imul rax, 10
    add rax, rsi 
 
    inc rdi 
    jmp .convert
 
.atoi_done:
    mov r15, qword [numbers_cnt]
    mov qword [numbers + r15 * 8], rax
    inc qword [numbers_cnt]
 
    mov rax, rdi
    pop r15    
    ret 

For the last line of input (the line with operations), we loop through each operation, determine if it is + or * then apply the corresponding operation.

Due to all of the numbers being saved in one lengthy buffer numbers, we apply one of these formulas for the matching operation:

i=0n1numbers[j+im] \sum_{i=0}^{n - 1} numbers[j + i \cdot m] i=0n1numbers[j+im] \prod_{i=0}^{n - 1} numbers[j + i \cdot m]

with nn being the amount of lines of numbers, mm being the amount of operands within one operation and jj refers to the index of the current operation.

6/solution/solver.asm
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi 
; Ret: Sum of the equations
 
solver:
    push r8
    push r13
    push r14 
    push r15
    
    mov rax, qword [numbers_cnt]
    mov rbx, qword [total_line]
    xor rdx, rdx
    div rbx
    mov rbx, rax
 
    xor r13, r13
    mov r14, rdi
 
.operation_loop:
    movzx rax, byte [r14]
    cmp rax, 0 
    je .done_solver
 
    cmp rax, '+'
    jne .multiply
 
    xor r15, r15
    xor r8, r8 
 
.add_loop:
    cmp r8, qword [total_line]
    je .end_operation
 
    mov rax, r8 
    mul rbx
    add rax, r13
 
    add r15, qword [numbers + rax * 8]
 
    inc r8
    jmp .add_loop
 
.multiply:
    cmp rax, '*'
    jne .continue_operation_loop
 
    mov r15, 1
    xor r8, r8 
 
.mul_loop:
    cmp r8, qword [total_line]
    je .end_operation
 
    mov rax, r8
    mul rbx
    mov rcx, rax
    add rcx, r13
 
    mov rax, r15 
    mul qword [numbers + rcx * 8]
    mov r15, rax
 
    inc r8
    jmp .mul_loop
 
.end_operation:
    add qword [result], r15 
    inc r13
    
.continue_operation_loop:
    inc r14
    jmp .operation_loop
 
.done_solver:
    pop r15
    pop r14
    pop r13
    pop r8
    ret

With everything implemented, here is the solution for the first part.

6/solution/solve.asm
section .data
    filename db "../problem/input.txt", 0
 
section .bss
    input resb 100000
    numbers resq 5000
    numbers_cnt resq 1
    total_line resq 1 
 
    output resb 20
    result resq 1
 
section .text
    global _start
 
_start:
    ; Open input file 
    mov rax, 2
    mov rdi, filename 
    xor rsi, rsi
    xor rdx, rdx
    syscall
    mov rbx, rax
 
    ; Read input file 
    xor rax, rax
    mov rdi, rbx 
    mov rsi, input
    mov rdx, 100000
    syscall
 
    ; Close the file
    mov rax, 3
    mov rdi, rbx
    syscall
 
    ; Split the lines and process them one by one
    mov r8, input
    mov r9, input 
    mov qword [result], 0
    mov qword [numbers_cnt], 0
    mov qword [total_line], 0
 
reader:
    movzx rax, byte [r9]
    test rax, rax 
    je process_last_line
 
    cmp rax, 10
    je process_line 
 
    inc r9 
    jmp reader
 
process_line:
    add qword [total_line], 1
    mov rdi, r8
    call parser 
 
    inc r9
    mov r8, r9
    jmp reader
 
process_last_line:
    cmp r8, r9
    je print_output
 
    mov rdi, r8
    call solver
 
print_output:
    ; Print result to stdout 
    mov rdi, qword [result] 
    call print_int
 
    ; Exit the program
    mov rax, 60
    xor rdi, rdi
    syscall
 
; ----------------------------------------------------------------------
; Component: Modded atoi (String to Int) conversion
; Args: rdi
; Ret: rax = terminated position; insert int into numbers
; Terminate if any byte is not in range [0-9]
 
modified_atoi:
    push r15 
 
    xor rax, rax
 
.convert:
    movzx rsi, byte [rdi]
    test rsi, rsi 
    je .atoi_done
 
    cmp rsi, 10
    je .atoi_done
 
    cmp rsi, '0'
    jl .atoi_done 
 
    cmp rsi, '9'
    jg .atoi_done 
 
    sub rsi, '0'
    imul rax, 10
    add rax, rsi 
 
    inc rdi 
    jmp .convert
 
.atoi_done:
    mov r15, qword [numbers_cnt]
    mov qword [numbers + r15 * 8], rax
    inc qword [numbers_cnt]
 
    mov rax, rdi
    pop r15    
    ret 
 
; ----------------------------------------------------------------------
; Component: Print Integer
; Args: rdi
 
print_int:
    mov rax, rdi
    mov rcx, output
    add rcx, 19
    mov byte [rcx], 10
    mov rbx, 10
 
.loop:
    dec rcx
    xor rdx, rdx
    div rbx
    add dl, '0'
    mov [rcx], dl
    test rax, rax
    jnz .loop
 
    ; Calculate length
    mov rdx, output
    add rdx, 20
    sub rdx, rcx
 
    ; Print
    mov rax, 1
    mov rdi, 1
    mov rsi, rcx
    syscall
    ret
 
; ----------------------------------------------------------------------
; Component: Parser
; Args: rdi
; Ret:
 
parser:
    push r15
    
    mov r15, rdi 
 
.parse_loop:
    movzx rax, byte [r15]
    cmp rax, 10
    je .done_parser
 
    cmp rax, ' '
    je .continue_parse_loop
 
    mov rdi, r15 
    call modified_atoi
    mov r15, rax
    jmp .parse_loop
 
.continue_parse_loop:
    inc r15
    jmp .parse_loop
 
.done_parser:
    pop r15
    ret
 
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi 
; Ret: Sum of the equations
 
solver:
    push r8
    push r13
    push r14 
    push r15
    
    mov rax, qword [numbers_cnt]
    mov rbx, qword [total_line]
    xor rdx, rdx
    div rbx
    mov rbx, rax
 
    xor r13, r13
    mov r14, rdi
 
.operation_loop:
    movzx rax, byte [r14]
    cmp rax, 0 
    je .done_solver
 
    cmp rax, '+'
    jne .multiply
 
    xor r15, r15
    xor r8, r8 
 
.add_loop:
    cmp r8, qword [total_line]
    je .end_operation
 
    mov rax, r8 
    mul rbx
    add rax, r13
 
    add r15, qword [numbers + rax * 8]
 
    inc r8
    jmp .add_loop
 
.multiply:
    cmp rax, '*'
    jne .continue_operation_loop
 
    mov r15, 1
    xor r8, r8 
 
.mul_loop:
    cmp r8, qword [total_line]
    je .end_operation
 
    mov rax, r8
    mul rbx
    mov rcx, rax
    add rcx, r13
 
    mov rax, r15 
    mul qword [numbers + rcx * 8]
    mov r15, rax
 
    inc r8
    jmp .mul_loop
 
.end_operation:
    add qword [result], r15 
    inc r13
    
.continue_operation_loop:
    inc r14
    jmp .operation_loop
 
.done_solver:
    pop r15
    pop r14
    pop r13
    pop r8
    ret

The answer for the first part is 5524274308182.

Part 2

The big cephalopods come back to check on how things are going. When they see that your grand total doesn't match the one expected by the worksheet, they realize they forgot to explain how to read cephalopod math.
 
Cephalopod math is written right-to-left in columns. Each number is given in its own column, with the most significant digit at the top and the least significant digit at the bottom. (Problems are still separated with a column consisting only of spaces, and the symbol at the bottom of the problem is still the operator to use.)
 
Here's the example worksheet again:
123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  
 
Reading the problems right-to-left one column at a time, the problems are now quite different:
- The rightmost problem is 4 + 431 + 623 = 1058
- The second problem from the right is 175 * 581 * 32 = 3253600
- The third problem from the right is 8 + 248 + 369 = 625
- Finally, the leftmost problem is 356 * 24 * 1 = 8544
 
Now, the grand total is 1058 + 3253600 + 625 + 8544 = 3263827.
 
Solve the problems on the math worksheet again. What is the grand total found by adding together all of the answers to the individual problems?

Now, we have to transpose the grid of numbers 90 degrees counter-clockwise. That is, we should obtain the following result for the given example:

  4
431
623
 
175
581
 32
 
8
248
369
 
356
24
1  

The easiest way to do this is to read each line of numbers and write it vertically, in reversed order.

We introduce a new .bss buffer input_transposed to contain the relocated characters.

section .bss
    input_transposed resb 100000

For each line of input, we write its characters (both non-digits and digits) into the new buffer using the following formula:

input_transposed[i+j(m+1)]=line[i][n1j] input\_transposed[i + j \cdot (m + 1)] = line[i][n - 1 - j]

with nn being the amount of lines of numbers, mm being the height of the numbers grid before transposition. To reserve spaces for the newlines to be added, we have to use m+1m + 1 instead of just mm in the formula.

After transposing the whole numbers grid, we call modified_atoi on each line of input to convert the strings into integers.

We also modify modified_atoi again to find the first digit by skipping all the initial spaces before the conversion. Naturally, all only-space lines will result in 0 being added to numbers list.

6/solution/modified_atoi.asm
; ----------------------------------------------------------------------
; Component: Modded atoi (String to Int) conversion
; Args: rdi
; Ret: rax = terminated position; insert int into numbers
; Ret: insert int into numbers, skip spaces while finding the number; insert int into numbers ​
; Terminate if any byte is not in range [0-9]
 
modified_atoi:
    push r15 
 
    xor rax, rax
 
.convert:
    movzx rsi, byte [rdi]
    test rsi, rsi 
    je .atoi_done
 
    cmp rsi, 10
    je .atoi_done
 
    cmp rsi, 0
    je .atoi_done​

    cmp rsi, 32
    je .next_digit​

    cmp rsi, '0'
    jl .atoi_done 
 
    cmp rsi, '9'
    jg .atoi_done 
 
    sub rsi, '0'
    imul rax, 10
    add rax, rsi 
 
.next_digit:
    inc rdi 
    jmp .convert
 
.atoi_done:
    mov r15, qword [numbers_cnt]
    mov qword [numbers + r15 * 8], rax
    inc qword [numbers_cnt]
 
    mov rax, rdi
    pop r15    
    ret 

Now, traversing the operation list to get the result of each operation. We have to be cautious that the operations are done from right to left, hence we either have to traverse the operation list in reverse, or the number list in reverse.

I chose the second option, and here is the final solution for the second part.

6/solution/solve.asm
section .data
    file_in db "../problem/input.txt", 0
 
section .bss
    input resb 100000
    input_transposed resb 100000
 
    numbers resq 5000
    numbers_cnt resq 1
 
    file_size resq 1
    total_line resq 1
    height resq 1
 
    output resb 20
    result resq 1
 
section .text
    global _start
 
_start:
    ; Open input file 
    mov rax, 2
    mov rdi, file_in 
    xor rsi, rsi
    xor rdx, rdx
    syscall
    mov rbx, rax
 
    ; Read input file 
    xor rax, rax
    mov rdi, rbx 
    mov rsi, input
    mov rdx, 100000
    syscall
    mov qword [file_size], rax 
 
    ; Close the file
    mov rax, 3
    mov rdi, rbx
    syscall
 
    ; Split the lines and process them one by one
    mov r8, input
    mov r9, input 
    mov qword [total_line], 0
 
reader:
    movzx rax, byte [r9]
    test rax, rax 
    je process_last_transpose
 
    cmp rax, 10
    je process_transpose
 
    inc r9 
    jmp reader
 
process_transpose:
    mov rax, qword [file_size]
    inc rax
    mov rcx, r9 
    sub rcx, r8 
    xor rdx, rdx
    div rcx 
 
    mov qword [height], rax
 
    xor r14, r14
    mov r15, r9 
 
transpose_loop:
    dec r15
    mov rax, qword [height]
    mul r14
    add rax, qword [total_line]
 
    movzx r13, byte [r15]
    mov byte [input_transposed + rax], r13b
 
    inc r14
    cmp r15, r8
    jne transpose_loop
 
    add qword [total_line], 1
    inc r9
    mov r8, r9 
    jmp reader
 
process_last_transpose:
    cmp r8, r9 
    je print_output
 
    push r8
    push r9
 
    xor r14, r14
    mov r15, r9 
    dec r15
 
add_newline_loop:
    mov rax, qword [height]
    mul r14
    add rax, qword [total_line]
 
    mov byte [input_transposed + rax], 10
 
    inc r14
    dec r15
    cmp r15, r8
    jne add_newline_loop
 
    add qword [total_line], 1
 
    ; Now split the lines in transposed buffer and convert them to integer one by one
    mov r8, input_transposed
    mov r9, input_transposed 
    mov qword [result], 0
 
transposed_reader:
    movzx rax, byte [r9]
    test rax, rax 
    je process_last_line
 
    cmp rax, 10
    je process_line 
 
    inc r9 
    jmp transposed_reader
 
process_line:
    mov rdi, r8
    call modified_atoi
 
    inc r9
    mov r8, r9
    jmp transposed_reader
 
process_last_line:
    cmp r8, r9 
    je solve_part
 
    mov rdi, r8 
    call modified_atoi
 
solve_part:
    pop r9 
    pop r8 
 
    mov rdi, r8
    call solver
 
print_output:
    ; Print result to stdout 
    mov rdi, qword [result] 
    call print_int
 
    ; Exit the program
    mov rax, 60
    xor rdi, rdi
    syscall
 
; ----------------------------------------------------------------------
; Component: Modded atoi (String to Int) conversion
; Args: rdi
; Ret: insert int into numbers, skip spaces while finding the number; insert int into numbers
; Terminate if any byte is not in range [0-9]
 
modified_atoi:
    push r15 
 
    xor rax, rax
 
.convert:
    movzx rsi, byte [rdi]
    test rsi, rsi 
    je .atoi_done
 
    cmp rsi, 10
    je .atoi_done
 
    cmp rsi, 0 
    je .atoi_done
 
    cmp rsi, 32 
    je .next_digit
 
    cmp rsi, '0'
    jl .atoi_done 
 
    cmp rsi, '9'
    jg .atoi_done 
 
    sub rsi, '0'
    imul rax, 10
    add rax, rsi 
 
.next_digit:
    inc rdi 
    jmp .convert
 
.atoi_done:
    mov r15, qword [numbers_cnt]
    mov qword [numbers + r15 * 8], rax
    inc qword [numbers_cnt]
 
    pop r15    
    ret 
 
; ----------------------------------------------------------------------
; Component: Print Integer
; Args: rdi
 
print_int:
    mov rax, rdi
    mov rcx, output
    add rcx, 19
    mov byte [rcx], 10
    mov rbx, 10
 
.loop:
    dec rcx
    xor rdx, rdx
    div rbx
    add dl, '0'
    mov [rcx], dl
    test rax, rax
    jnz .loop
 
    ; Calculate length
    mov rdx, output
    add rdx, 20
    sub rdx, rcx
 
    ; Print
    mov rax, 1
    mov rdi, 1
    mov rsi, rcx
    syscall
    ret
 
; ----------------------------------------------------------------------
; Component: Solver
; Args: rdi
; Ret: Sum of the equations
 
solver:
    push r8
    push r13
    push r14 
    push r15
    
    mov r13, qword [numbers_cnt]
    dec r13
    mov r14, rdi
 
.operation_loop:
    movzx rax, byte [r14]
    cmp rax, 0 
    je .done_solver
 
    cmp rax, '+'
    jne .multiply
 
    xor r15, r15
 
.add_loop:
    mov r8, qword [numbers + r13 * 8]
    cmp r8, 0
    je .end_operation
 
    add r15, r8
 
    cmp r13, 0 
    je .end_operation
 
    dec r13
    jmp .add_loop
 
.multiply:
    cmp rax, '*'
    jne .continue_operation_loop
 
    mov r15, 1
 
.mul_loop:
    mov r8, qword [numbers + r13 * 8]
    cmp r8, 0
    je .end_operation
 
    mov rax, r15 
    mul r8
    mov r15, rax
 
    cmp r13, 0 
    je .end_operation
    
    dec r13
    jmp .mul_loop
 
.end_operation:
    add qword [result], r15 
    dec r13
    
.continue_operation_loop:
    inc r14
    jmp .operation_loop
 
.done_solver:
    pop r15
    pop r14
    pop r13
    pop r8
    ret

The final result of this challenge is 8843673199391.