Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.27 KB | None | 0 0
  1. Day 2 Advent of Code: Part 1 and 2
  2.  
  3. Part 1:
  4. The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.
  5.  
  6. Part 2:
  7. It sounds like the goal is to find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.
  8.  
  9. my $P1_checksum = 0;
  10. my $P2_checksum = 0;
  11. for 'input.txt'.IO.lines -> $line {
  12.     my @row = $line.split(/\s/)>>.Int;
  13.  
  14.     $P1_checksum += ( @row.max() - @row.min() );
  15.  
  16.     my @combinations = @row.combinations(2);
  17.     for @combinations -> [$a, $b] {
  18.         if $a %% $b {
  19.             $P2_checksum += ($a / $b);
  20.             last;
  21.         } elsif $b %% $a {
  22.             $P2_checksum += ($b / $a);
  23.             last;
  24.         }
  25.     }
  26. }
  27.  
  28. say $P1_checksum, '|', $P2_checksum;
  29.  
  30. _______
  31. input.txt
  32. 86 440 233 83 393 420 228 491 159 13 110 135 97 238 92 396
  33. 3646 3952 3430 145 1574 2722 3565 125 3303 843 152 1095 3805 134 3873 3024
  34. 2150 257 237 2155 1115 150 502 255 1531 894 2309 1982 2418 206 307 2370
  35. 1224 343 1039 126 1221 937 136 1185 1194 1312 1217 929 124 1394 1337 168
  36. 1695 2288 224 2667 2483 3528 809 263 2364 514 3457 3180 2916 239 212 3017
  37. 827 3521 127 92 2328 3315 1179 3240 695 3144 3139 533 132 82 108 854
  38. 1522 2136 1252 1049 207 2821 2484 413 2166 1779 162 2154 158 2811 164 2632
  39. 95 579 1586 1700 79 1745 1105 89 1896 798 1511 1308 1674 701 60 2066
  40. 1210 325 98 56 1486 1668 64 1601 1934 1384 69 1725 992 619 84 167
  41. 4620 2358 2195 4312 168 1606 4050 102 2502 138 135 4175 1477 2277 2226 1286
  42. 5912 6261 3393 431 6285 3636 4836 180 6158 6270 209 3662 5545 204 6131 230
  43. 170 2056 2123 2220 2275 139 461 810 1429 124 1470 2085 141 1533 1831 518
  44. 193 281 2976 3009 626 152 1750 1185 3332 715 1861 186 1768 3396 201 3225
  45. 492 1179 154 1497 819 2809 2200 2324 157 2688 1518 168 2767 2369 2583 173
  46. 286 2076 243 939 399 451 231 2187 2295 453 1206 2468 2183 230 714 681
  47. 3111 2857 2312 3230 149 3082 408 1148 2428 134 147 620 128 157 492 2879
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement