Created
December 8, 2017 22:00
-
-
Save m-r-r/0fbff52d12923d13e2bbb5091823cfc4 to your computer and use it in GitHub Desktop.
Advent of code 2017 — Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://adventofcode.com/2017/day/2 | |
// Parse the input into a two-dimensional array of numbers | |
const parseSpreadsheetRow = input => input.split(/\s+/).map(n => parseInt(n)); | |
const parseSpreadsheet = input => input.split('\n').map(parseSpreadsheetRow); | |
// Find the highest and lowest values in a list | |
const max = values => | |
values.reduce((previous, current) => Math.max(previous, current)); | |
const min = values => | |
values.reduce((previous, current) => Math.min(previous, current)); | |
// Calculate the checksum of a line | |
const rowChecksum = values => max(values) - min(values); | |
// Parse the spreadsheet and calculate the checksum | |
const checksum = input => | |
parseSpreadsheet(input) | |
.map(rowChecksum) | |
.reduce((acc, i) => acc + i); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment