Skip to content

Instantly share code, notes, and snippets.

@soutar
Last active December 7, 2015 17:02
Show Gist options
  • Save soutar/35e2492f97beaa9ec8b8 to your computer and use it in GitHub Desktop.
Save soutar/35e2492f97beaa9ec8b8 to your computer and use it in GitHub Desktop.
Advent of Code
const input = "(((())))()((((((((())()(()))(()((((()(()(((()((()((()(()()()()()))(((()(()((((((((((())(()()((())()(((())))()(()(()((()(()))(()()()()((()((()(((()()(((((((()()())()((((()()(((((()(())()(())((())()()))()(((((((())(()())(()(((())(()))((())))(()((()())))()())((((())))(()(((((()(())(((()()((()((()((((((((((())(()())))))()))())()()((((()()()()()()((((((())())(((()())()((()()(((()()()))(((((()))(((()(()()()(()(()(((())()))(()(((()((())()(()())())))((()()()(()()(((()))(((()((((()(((((()()(()())((()())())(()((((((()(()()))((((()))))())((())()()((()(()))))((((((((()))(()()(((())())(())()((()()()()((()((()((()()(((())))(()((())()((((((((()((()(()()(((())())())))(())())))()((((()))))))())))()()))()())((()())()((()()()))(()()(((()(())((((())())((((((((()()()()())))()()()((((()()))))))()((((()(((()))(()()())))((()()(((()))()()())())(((())((()()(())()()()(((())))))()())((()))()))((())()()())()())()()(()))())))())()))(())((()(())))(()(())(()))))(()(())())(()(())(()(()))))((()())()))()((((()()))))())))()()())((())()((()()()))()(((()(()))))(())()()))(((()())))))))))(((())))()))())()))))()()(((())))))))()(()()(()))((()))))((())))((()((())))())))()()(()))())()(()((()())(()(()()())())(()()))()))))(()())()()))()()()()))(()(()(()))))))()(()))()))()()(()((())(()(())))()(((())(())())))))()(()(()))))()))(()()()(())()(()(())))()))))()()(((((())))))())()())())())()())()))))()))))))))())()()()()()()())))()))((())()))())))()((())()))))()))())))))))())()()()))()()(()((((()(((((((()(())((()())((()()))()))))(())))()()()(())((())()())))(())))(())))(((()()))()(())(((()(()))((())))())()))((((()))())()))))))))()(())())))(()))()(()()))())()()(())())))())()()(()())))()((()())(()(())(())))))))))))))(()))))()))))))()()())(()(((((()(()())))())()))(()))()))(()()))()())(()))())()(())((()()))))))())))())()(((())))(()(()))()()))()(()))))))((()())(()))))))()())))()()))))))))((((((((()()()(()))))))()())))())))()()((())()))((())(())))())())))()()()((()((()(())))())()(())))))))))()())))()()()()()()))()))((())())(()(()))))))(()()))()))(())))()))))))))))))(()))))))))()))))()))()())()))()()))))))()))))((()))))(()))())()(())))(()())((((()())))()))))(()))()(()()(())))))())))))()))))))())))())))))())))())())))())(()))))(())()(())))())()))((()()))))))())))((())))))))())))(())))))()()())))))())))))()))))))()))()()()(()(((()())())())(()))())))))((()(())(()))))))))(())))()()()())())(()))))()()()))()))())())())()(())))()(((()((((())))))))()))))))))))))))))))))((())()())(()))))()()))))))(()()(())())))())))((())))((())))))))))))))()))))()(()))))))())))))()))(()()())(()())))))))))()))))))(())))))()()))()())(((())))()))(()))))))))(())())))())))())())())()()))((())()(())()())()))()())(())(()))))()())))(()(((()))))))()(()())()()()))()))))))))()()()(())()())()(((((()))()())())(()))))()()()(())))())))()((()())))(()))())()(()())())(()))()()))((()()))((()()()()())))(())()))(()(())))((()()))))))))())))))))())()()))))))))))))))))(())()(())(())()())())()))()(()))))())())))))()())()(()))()()(())))(())())))))(()))))))))))))))())())(())(())))(((()))()))))())((())(()))())))))))())))))())))()))()))))))))))))())()))))()))))((()))(())))()(())))(())()))()))())))())))))))()(()())())))()()())))(())))))(()))))))))))))(()))()))()))())))(((()()()(())((()())))()())(((()))(())()))((()()()())))())(())(()))))()(((((())))(()))())())))))))((((()()()))())())()(()(()())))))))))()())())))(())))()())(((()(())())()()))())())))))))((()())((()()(()))(()(())))()))()))(()))(()))()()(()(((())((((()))()(()))((())()(()(()())()(()))()())))))(()))()))())()())))())))(())))((())(()())))))()))(())(()))()())()(()()((()(()))))))()(())(()())(())()))(((())()))(()()(()()()))))(()(())))()))))())))))())(()()()()()()(((())))(()()))()((())(((((()()())))(()))(()))()()))(((())())()(((()()()()))))(()))(())())))()())(()()())())))))))()))))((())))()())(()))(()(()))())))))())(())))))()()())())()))()()(())))(()))(())((((((())(()))(()))())()))(()()(())))()))(()()))()))()(())))(())))((()(()))(())()()())())))(((()()())(())()))))))()(((()(((((()()(((())(())))())()((()))))((()())()(())(((())))(((()((()(()(()))(()()))())(()))(())(())))()))))))((((()))()((((()(()))()))()()))))()(()(()))()(()((()(((()(()()(((()))))()(((()(()(()(((()(()())())()()(()(()())())(()((((())(()))()))(((((()()())(())()((()()())))()()(((()()))()((((((((()(())))())((()))))(())))(()))))((()((((()()(())(((((()))(((((((((((((()())))((((()(((()((())())()))((()))()(()()((()()()()(()()(()(()(((())()(()((((((()((()()((())()((((()((()()(()()())((()()()((()((())()(()(((()((())((((())(()))((()(()))(()())()((((((((()(((((((((((()))(()(((()(()()()((((())((())()())()))(())((())(()))(((()((()(())))(()))))((()()))))((((()(()(()())(()(())((((((((()((((()((()(((((()))())()(()))(()()((()(())(((((()(())()(((((()()))))))()(((())()(()()((((())()((())((()(((())(((()))((()()((((()(())))))((()((((()((()((()(((())((()))(((((((()(((()((((((((())()))((((())(((((()((((((((()(((()((()(((()()(((()((((((()()(()((((((((()()(()(()(())((((()())()))))(((()))((((())((((()())((()(())()((()((((((()((((((()(())))()())(((())())())()(())()(()())((()()((((())((((((())(()(((((()((((())()((((()(()(())(()())(((())()((())((((()))()((((((())(()(((()(((()((((((()(((()))(()()())())((()((()())()((((())(((()(()(((((((((())(())))()((()()()()(())((()))(((((((()(((((((((()(()))))(()((((((((()((((()((()()((((((()()(((((((()(()(())()(())((()()()((()(((((()())()(((((()())()()((()(()())(()()()(((()()(((((()((((((()()((()(()()()((((((((((((()((((((((()()(((()())))()(((()()(())())((((()((((()((((()()()(())(())((()(()(((((((((((((((()(())(())))))()()))((()(((()(())((()(((()(()()((((()()(((()(((()(((((()()((()(()(((()))((((((()((((((((()((()((())(((((()(((())(())())((()()))((((())()()((()(((()(((((()()(((()))(((()(()(((((((((((((()))((((((((()(((()))))())((((((((((((())((())((()())(((())((())(()((((((((((()(((())((()()(()((())(((((((((((()))((((((((((((()(()())((()((()((()(()(((()((((((((()()(()((()(()(((()))((()))(((((((((((((()(())((((((())(((()(())(()(()(()((()()))((((()((((()((((())))())((((()((((()))((((((()((((((()((()(((())))((())(()))(()((()((((()((()(((()()))((((()()()(((((((())(((())(()))())((((()())(((()(((((((((((()(()(()((()(((((((((((((((()()((((()((((((((()(((()()((()((((()))(((()(())((((((()((((())()((((()((()))(())()(()(((()((())())((((((()(()(())())(((())(()(()())(((((()((()((())()())(())))(((()(())))))))(((()(((()))()((()(((()()((()())()()))())))(((()))(()(((()(((((((((()(()(((((()()(((()())()()))))()(((()))(((()(()(()(()(()))()(())()))(()(((())))(()))))))))))(())((()((())((()(())()(())((()()((((()()((()()))((())(((()((()(())(())))()(()(((((()((()))())()(((((()()(((()(()((((((())(()))(())()))((()(()()))(())())()))(((())))(()((()(((())(())())))((()()((((((((((((((()((()(()()(()(((()))())()()((()()()(())(()))(()())(((())((())()(())()()(()()(())))((()(((()))))(((()()(()()))())((()((())()))((((()()()())((())))(((()(())(((((()(((((()((()(()((((()()(((()()()(((()())(((()()((((())(()))(((()))(())())((()))(((()((()))(((()()((())((()(((((()((((()()())((()))()((((()((()(()()()("
const directionActions = { '(': floor => floor + 1, ')': floor => floor - 1 }
const [endedOnFloor, enteredBasementAtPosition] = input
.split('')
.reduce(([currentFloor, enteredBasementAtPosition], direction, index) => {
let nextFloor = directionActions[direction](currentFloor);
enteredBasementAtPosition = (nextFloor === -1 && enteredBasementAtPosition === undefined)
? index + 1 : enteredBasementAtPosition;
return [nextFloor, enteredBasementAtPosition];
}, [0, undefined]);
console.log(endedOnFloor, enteredBasementAtPosition);
function calculatePaperArea (dimensions) {
let [length, width, height] = dimensions;
let sides = [2*length*width, 2*width*height, 2*height*length];
return sides.reduce((total, side) => total + side) + (Math.min(...sides) / 2)
}
function calculateRibbonLength (dimensions) {
let [length, width, height] = dimensions;
return Math.min(2*(length+width),2*(width+height), 2*(height+length)) + length*width*height;
}
let input = fetch('https://gist.githubusercontent.com/soutar/e1f6fda416525a61bf54/raw/3bbd99a7e26feb85c5637c994330d12dc32f5f80/input.txt')
.then(response => response.text())
.then(text => {
let boxStrings = text.split('\n');
let boxes = boxStrings.map(dimensions => dimensions.split('x').map(char => parseInt(char)));
let totalPaperArea = boxes.map(calculatePaperArea).reduce((total, item) => total + item);
let totalRibbonLength = boxes.map(calculateRibbonLength).reduce((total, item) => total + item);
console.log(totalPaperArea, totalRibbonLength);
});
const directions = {
"^": ([x, y]) => [x, y+1],
">": ([x, y]) => [x+1, y],
"v": ([x, y]) => [x, y-1],
"<": ([x, y]) => [x-1, y]
}
function processInstructions (instructions) {
return instructions.reduce((history, instruction) => {
let position = history[history.length - 1];
let next = directions[instruction](position);
return [...history, next];
}, [[0, 0]]);
}
let input = fetch('https://gist.githubusercontent.com/soutar/32d089fec2df4592123a/raw/fbab1e1e3a45482e8bf00479eeb250c0b0f04ba6/input.txt')
.then(response => response.text())
.then(text => {
let instructions = text.split('');
let santas = 2;
let paths = [...Array(santas)]
.map((_, santa) =>
instructions.filter((_, index) => index % santas === santa))
.map(processInstructions);
let houses = paths
.reduce((combined, current) => [...combined, ...current])
.filter(([x1, y1], index, path) =>
path.findIndex(([x2, y2]) => x1 === x2 && y1 === y2) === index)
.length;
console.log(houses);
});
import md5 from 'md5';
function findHashMatchingPattern (pattern = /.+/, secretKey = '', attempt = 1) {
let hash = md5(`${secretKey}${attempt}`);
return (pattern.exec(hash))
? [hash, attempt]
: findHashMatchingPattern(pattern, secretKey, attempt+1);
}
let [hash, attempt] = findHashMatchingPattern(/^000000/, 'iwrupvqb');
import fetch from 'node-fetch';
// part 1
// const niceCriteria = [
// string => string.split('').filter(character => ['a', 'e', 'i', 'o', 'u'].indexOf(character) > -1).length >= 3,
// string => /(.)\1+/.exec(string),
// string => ['ab', 'cd', 'pq', 'xy'].filter(substring => string.indexOf(substring) > -1).length === 0
// ];
// part 2
const niceCriteria = [
string => /(..).*\1/.exec(string),
string => /(.).\1/.exec(string)
]
function isNice (string) {
return niceCriteria.filter(test => test(string)).length === niceCriteria.length
}
fetch('http://gist.githubusercontent.com/soutar/ba2b749385d6c99ce2c4/raw/c96bf95d2c7779596d7020528d6f712f0c531638/input.txt')
.then(response => response.text())
.then(response => {
let strings = response.split('\n');
let niceStrings = strings.filter(isNice);
console.log(niceStrings.length);
});
import fetch from 'node-fetch';
function parseInstruction (instruction, actions) {
let [_, action, startx, starty, endx, endy] = instruction.match(instructionPattern);
return {
action: actions[action],
start: [startx, starty],
end: [endx, endy]
}
}
function applyModification (grid, modification) {
let { action, start, end } = modification;
let [startx, starty] = start;
let [endx, endy] = end;
for (var x = parseInt(startx, 10); x <= endx; x++) {
for (var y = parseInt(starty, 10); y <= endy; y++) {
grid[x][y] = action(grid[x][y]);
}
}
return grid;
}
const instructionPattern = /^([^0-9]+)\s([0-9]{1,3}),([0-9]{1,3}) through ([0-9]{1,3}),([0-9]{1,3})$/;
const height = 1000;
const width = 1000;
// part 1
// const LIGHT_OFF = false;
// const LIGHT_ON = true;
//
// const actions = {
// 'turn off': () => LIGHT_OFF,
// 'turn on': () => LIGHT_ON,
// 'toggle': (value) => !value
// }
// part 2
const LIGHT_OFF = 0;
const LIGHT_ON = 1;
const actions = {
'turn off': (value) => Math.max(value - 1, LIGHT_OFF),
'turn on': (value) => value + 1,
'toggle': (value) => value + 2
}
const grid = [...Array(width)].map(column => [...Array(height)].map(node => LIGHT_OFF));
fetch('http://gist.githubusercontent.com/soutar/4fadd8451585cc6e03ab/raw/49ee684dd6cd825fabf6d2c1b0cf76248b71699d/input.txt')
.then(response => response.text())
.then(response => {
let instructions = response.split('\n');
let newGrid = instructions
.map(instruction => parseInstruction(instruction, actions))
.reduce(applyModification, grid);
// // part 1
// let onLights = newGrid.reduce(
// (on, column) => on + column.filter(node => node).length,
// 0
// );
//
// console.log(onLights);
// part 2
let totalLuminosity = newGrid.reduce(
(luminosity, column) => luminosity + column.reduce(
(luminosity, node) => luminosity + node, 0
), 0
);
console.log(totalLuminosity);
});
import fetch from 'node-fetch';
const OPERATIONS = {
'AND': (x, y) => x & y,
'OR': (x, y) => x | y,
'NOT': (value) => ~ value,
'LSHIFT': (x, y) => x << y,
'RSHIFT': (x, y) => x >> y
}
function parseInput (input) {
let components = input.split(' ');
let first;
let operator;
let second;
switch (components.length) {
case 1:
return { components: [components] }
break;
case 2:
[operator, first] = components;
return { components: [first], operation: OPERATIONS[operator] };
break;
case 3:
[first, operator, second] = components;
return { components: [first, second], operation: OPERATIONS[operator] };
break;
}
}
function parseCircuitDefinition (circuitDefinition) {
return circuitDefinition.split('\n').reduce((circuit, wire) => {
let [input, output] = wire.split('->').map(string => string.trim());
circuit[output] = parseInput(input);
return circuit;
}, {});
}
function readSignal (circuit, wire, state = {}) {
let input = circuit[wire];
let { operation } = input;
if (state[wire]) {
return state[wire];
}
let components = input.components.map(component => {
return (!isNaN(component))
? parseInt(component)
: readSignal(circuit, component, state);
});
let result = (operation)
? operation(...components)
: components[0];
return state[wire] = wrap(result, 0, 65536);
}
function wrap (number, min, max) {
let range = max - min;
return ((number - min) % range < 0)
? number + range
: number;
}
fetch('https://gist.githubusercontent.com/soutar/60af14354c560946f992/raw/ae38f4a978c2d46578820595e0c77c168934958c/input.txt')
.then(response => response.text())
.then(response => {
let circuit = parseCircuitDefinition(response);
let signal = readSignal(circuit, 'a');
console.log(signal);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment