Last active
December 8, 2015 23:51
-
-
Save afahy/2109c9f21c6e06703b2f to your computer and use it in GitHub Desktop.
Advent of code solutions
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
// solves http://adventofcode.com/day/1/ | |
const floors = (input) => (input.match(/\(/g) || []).length - (input.match(/\)/g) || []).length; |
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
// solves http://adventofcode.com/day/2/ | |
const paper = (data) => { | |
const presents = data.split('\n').filter(line => line !== '').map(present => present.split('x').map(Number)); | |
const areas = presents.map(present => { | |
const sides = [present[0] * present[1], present[1] * present[2], present[0] * present[2]]; // this line could definitely be refactored | |
const slack = Math.min.apply(null, sides); | |
return sides.map(n => n * 2).reduce((p, n) => p + n) + slack; | |
}); | |
return areas.reduce((p, n) => p + n); | |
}; |
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
// solves http://adventofcode.com/day/3/ | |
const directions = { | |
">": [0,1], | |
"<": [0,-1], | |
"^": [-1,0], | |
"v": [1,0] | |
}; | |
const step = (origin, direction) => origin.slice().map((coord, i) => coord + directions[direction][i]); | |
const delivery = (directions) => { | |
let location = [0,0]; | |
let houses = {"[0,0]": 1}; | |
directions.split('').forEach(direction => { | |
location = step(location, direction); | |
houses[JSON.stringify(location)] = 1; | |
}); | |
return Object.keys(houses); | |
}; |
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
// solves http://adventofcode.com/day/4/ | |
const crypto = require('crypto'); | |
const mine = (input) => { | |
let num = 0; | |
let hash = ''; | |
while (hash = crypto.createHash('md5').update(`${input}${num}`).digest("hex")) { | |
if (hash.indexOf('00000') === 0) { | |
break; | |
} | |
num = num + 1; | |
} | |
return num; | |
}; | |
console.log(mine(process.argv[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
// solves http://adventofcode.com/day/5/ | |
const fs = require('fs'); | |
const test_has_three_vowels = /[aeiou]/ig; | |
const test_has_multiples = /([a-z])\1/i; | |
const exclusions = ['ab', 'cd', 'pq', 'xy']; | |
const get_nice = (input) => { | |
return input.split('\n').filter(str => { | |
if (exclusions.filter(x => str.indexOf(x) > -1 ).length) { return false; } | |
const vowels = str.match(test_has_three_vowels); | |
return (vowels && vowels.length > 2) && test_has_multiples.test(str); | |
}).length; | |
}; | |
fs.readFile(process.argv[2], 'utf8', function(err, data) { | |
console.log(get_nice(data)); | |
}); |
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
// solves http://adventofcode.com/day/6/ | |
const fs = require('fs'); | |
const turnOn = (coords, lights) => { | |
lights[coords] = true; | |
}; | |
const turnOff = (coords, lights) => { | |
delete lights[coords]; | |
}; | |
const toggle = (coords, lights) => { | |
if (lights[coords]) { | |
delete lights[coords]; | |
} else { | |
lights[coords] = true; | |
} | |
}; | |
const match_coords = /(\d+,\d+)/g; | |
const get_lights = (input) => { | |
let lights = {}; | |
input.split('\n').forEach(str => { | |
if (str.length) { | |
const [start, end] = str.match(match_coords).map(c => c.split(',').map(Number)); | |
const callback = (str.indexOf(' on ') > -1) ? turnOn : (str.indexOf(' off ') > -1) ? turnOff : toggle; | |
for (let [x_pos, x_end] = [start[0], end[0]]; x_pos <= x_end; x_pos++) { | |
for (let [y_pos, y_end] = [start[1], end[1]]; y_pos <= y_end; y_pos++) { | |
callback(`${x_pos},${y_pos}`, lights); | |
} | |
} | |
} | |
}); | |
return Object.keys(lights).length; | |
}; | |
fs.readFile(process.argv[2], 'utf8', function(err, data) { | |
console.log(get_lights(data)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment