Last active
December 31, 2015 00:17
-
-
Save thewarpaint/c02d13c6f09dd27b6117 to your computer and use it in GitHub Desktop.
Advent of code
This file contains hidden or 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
function calcFloor(input) { | |
var up = 0, | |
down = 0, | |
basement = 0, | |
i; | |
for(i=0; i<input.length; i++) { | |
input[i] === '(' ? up++ : down++; | |
if(!basement) { | |
if(down > up) { | |
basement = i + 1; | |
} | |
} | |
} | |
return { | |
floor: up - down, | |
basement: basement | |
}; | |
} |
This file contains hidden or 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
// Regex approach, works for the first part only | |
function getBalance(string) { | |
var regex = /-?\d+/g, | |
result, | |
balance = 0; | |
while ((result = regex.exec(string)) !== null) { | |
balance += parseInt(result[0], 10); | |
} | |
return balance; | |
} |
This file contains hidden or 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
function calcArea(input) { | |
var result = { | |
paper: 0, | |
ribbon: 0 | |
}; | |
input.forEach(function (dimensions) { | |
var sides = dimensions.split('x').map(Number).sort(), | |
areas = [ | |
sides[0] * sides[1], | |
sides[0] * sides[2], | |
sides[1] * sides[2] | |
], | |
perimeters = [ | |
2 * (sides[0] + sides[1]), | |
2 * (sides[0] + sides[2]), | |
2 * (sides[1] + sides[2]) | |
]; | |
result.paper += Math.min.apply(null, areas) + (2 * areas[0]) + (2 * areas[1]) + (2 * areas[2]); | |
result.ribbon += Math.min.apply(null, perimeters) + (sides[0] * sides[1] * sides[2]); | |
}); | |
return result; | |
} |
This file contains hidden or 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
function getUniqueHouses(directions) { | |
var grid = { | |
'0,0': 1 | |
}, | |
moves = { | |
'^': [0, 1], | |
'>': [1, 0], | |
'v': [0, -1], | |
'<': [-1, 0] | |
}, | |
current = [0, 0], | |
curHash, | |
move, | |
i; | |
for(i = 0; i < directions.length; i++) { | |
move = moves[directions[i]]; | |
current[0] += move[0]; | |
current[1] += move[1]; | |
curHash = current.join(','); | |
if(!grid[curHash]) { | |
grid[curHash] = 0; | |
} | |
grid[curHash]++; | |
} | |
console.log(grid); | |
return Object.keys(grid).length; | |
} | |
function getTwoUniqueHouses(directions) { | |
var moves = { | |
'^': [0, 1], | |
'>': [1, 0], | |
'v': [0, -1], | |
'<': [-1, 0] | |
}, | |
grid = { | |
'0,0': 1 | |
}, | |
positions = { | |
santa: [0, 0], | |
robot: [0, 0] | |
}, | |
curHash, | |
move, | |
current = 'santa', | |
i; | |
for(i = 0; i < directions.length; i++) { | |
move = moves[directions[i]]; | |
positions[current][0] += move[0]; | |
positions[current][1] += move[1]; | |
curHash = positions[current].join(','); | |
if(!grid[curHash]) { | |
grid[curHash] = 0; | |
} | |
grid[curHash]++; | |
current = (current === 'santa' ? 'robot' : 'santa'); | |
} | |
console.log(grid); | |
return Object.keys(grid).length; | |
} |
This file contains hidden or 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
// Node.js | |
var md5 = require('md5'), | |
secret = 'iwrupvqb', | |
prefix = '000000', // 000000 for the second one | |
answer = 0, | |
hash; | |
for(;;) { | |
hash = md5(secret + answer); | |
if(hash.indexOf(prefix) === 0) { | |
console.log(answer); | |
break; | |
} | |
answer++; | |
} |
This file contains hidden or 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
function isNiceString(string) { | |
var vowels = ['a', 'e', 'i', 'o', 'u'], | |
badStrings = ['ab', 'cd', 'pq', 'xy'], | |
vowelCount = 0, | |
substring, | |
hasTwoInARow = false, | |
i; | |
for(i = 0; i < string.length; i++) { | |
if(i !== string.length - 1) { | |
if(string[i] === string[i + 1]) { | |
hasTwoInARow = true; | |
} | |
if(badStrings.indexOf(string[i] + string[i + 1]) !== -1) { | |
return false; | |
} | |
} | |
if(vowels.indexOf(string[i]) != -1) { | |
vowelCount++; | |
} | |
} | |
return hasTwoInARow && vowelCount >= 3; | |
} | |
function countNiceString(input) { | |
var count = 0; | |
input.forEach(function (string) { | |
if(isNiceString(string)) { | |
count++; | |
} | |
}); | |
return count; | |
} |
This file contains hidden or 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
// Incomplete, ended up doing this "manually" with find and replace in Atom :/ | |
function getEscapedLength(string) { | |
return string.replace(/\\\\/g, '\\').replace(/\\"/g, '"').replace(/\\x[0-9a-f]{1,2}/g, '&').length - 2; | |
} | |
function getLengthDifference(input) { | |
var unescaped = 0, | |
escaped = 0; | |
input.forEach(function (string) { | |
unescaped += string.length; | |
escaped += getEscapedLength(string); | |
}); | |
return unescaped - escaped; | |
} |
This file contains hidden or 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
function lookAndSay(sequence, steps) { | |
var newSequence, | |
i, | |
j; | |
for(i = 0; i < steps; i++) { | |
newSequence = []; | |
for(j = 0; j < sequence.length; j++) { | |
if(newSequence.length === 0 || sequence[j] !== newSequence[newSequence.length - 1][0]) { | |
newSequence.push([sequence[j], 0]); | |
} | |
newSequence[newSequence.length - 1][1]++; | |
} | |
sequence = newSequence.reduce(function (prev, curr) { return prev + curr[1].toString() + curr[0].toString() }, ''); | |
} | |
return sequence; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment