Skip to content

Instantly share code, notes, and snippets.

@humancatfood
Last active December 13, 2017 03:18
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 humancatfood/5d9d26c7750c282329d56ffc58deea47 to your computer and use it in GitHub Desktop.
Save humancatfood/5d9d26c7750c282329d56ffc58deea47 to your computer and use it in GitHub Desktop.
Advent Of Code 2017 - 02(2) http://adventofcode.com/2017/day/2
const { max, min } = Math;
const input = `
157 564 120 495 194 520 510 618 244 443 471 473 612 149 506 138
1469 670 47 604 1500 238 1304 1426 54 749 1218 1409 60 51 1436 598
578 184 2760 3057 994 167 2149 191 2913 2404 213 1025 1815 588 2421 3138
935 850 726 155 178 170 275 791 1028 75 781 138 176 621 773 688
212 977 297 645 229 194 207 640 804 509 833 726 197 825 242 743
131 43 324 319 64 376 231 146 382 162 464 314 178 353 123 446
551 121 127 155 1197 288 1412 1285 557 137 145 1651 1549 1217 681 1649
1723 1789 5525 4890 3368 188 3369 4842 3259 2502 4825 163 146 2941 126 5594
311 2420 185 211 2659 2568 2461 231 2599 1369 821 506 2227 180 220 1372
197 4490 141 249 3615 3314 789 4407 169 352 4383 5070 5173 3115 132 3513
4228 2875 3717 504 114 2679 165 3568 3002 116 756 151 4027 261 4813 2760
651 3194 2975 2591 1019 835 3007 248 3028 1382 282 3242 296 270 3224 3304
1858 1650 1720 1848 95 313 500 1776 207 1186 72 259 281 1620 79 77
3841 3217 440 3481 3643 940 3794 4536 1994 4040 3527 202 193 1961 230 217
2837 2747 2856 426 72 78 2361 96 2784 2780 98 2041 2444 1267 2167 2480
411 178 4263 4690 3653 162 3201 4702 3129 2685 3716 147 3790 4888 79 165
`;
const output1 = input
// remove trailing whitespaces
.trim()
// split into rows
.split('\n')
// split rows into columns
.map(row => row.split('\t'))
// for each row ..
.map(row => row
// .. and each number `x` in that row ..
.map((x, i, row) => row
// .. extract a tail of numbers following that `x`
.slice(i + 1)
// for each entry `y` of each tail get the result of dividing `x` and `y` (the larger by the smaller),
// IFF that division gives an even result
.map(y => max(x, y) % min(x, y) === 0 ? max(x, y) / min(x, y) : 0)))
// then reduce everything back together
.reduce((rows, row) => rows.concat(row))
.reduce((cols, col) => cols.concat(col))
.reduce((a, b) => a + b)
// here's the same thing again but with mapping and reducing combined, to save iterations
const output2 = input
.trim()
.split('\n')
.map(row => row.split('\t'))
.reduce((rows, row) => rows + row
.reduce((cols, x, i, arr) => cols + arr
.slice(i+1)
.reduce((col, y) => col + (max(x,y) % min(x,y) === 0 ? max(x,y) / min(x,y) : 0), 0), 0), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment