Skip to content

Instantly share code, notes, and snippets.

@m-r-r
Created December 8, 2017 22:00
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 m-r-r/0fbff52d12923d13e2bbb5091823cfc4 to your computer and use it in GitHub Desktop.
Save m-r-r/0fbff52d12923d13e2bbb5091823cfc4 to your computer and use it in GitHub Desktop.
Advent of code 2017 — Day 2
// 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