Skip to content

Instantly share code, notes, and snippets.

@bigomega
Last active December 20, 2015 12:56
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 bigomega/e6fcc249f9465295df1a to your computer and use it in GitHub Desktop.
Save bigomega/e6fcc249f9465295df1a to your computer and use it in GitHub Desktop.
Advent of Code problem solutions
// - If a is not found
// - iterate through the input
// - If it output doesn't exist
// - Check if inputs have value
// - If so calculate output and add value
// - Else add the equation
// - If the output has value(number), ignore
// - If the output has equation, try to calculate
// - Reapeat
// Recusive solution
// Find a's inputs
// - If varaible
// - Find it's inputs. (recursive)
var fs = require('fs');
var input = '\n'+fs.readFileSync('input7.txt', 'utf8')+'\n';
var hash = {};
function find_value(name){
if(hash[name])
return hash[name];
if(!isNaN(name))
return +name;
// console.log(hash)
var rx = new RegExp('\n(.*) -> '+name+'\n', 'g').exec(input);
if(!rx)
throw(name+' Not Found');
// console.log(rx[1],'->',name);
rx = rx[1].split(' ');
// assignment
var ret;
if(rx.length == 1)
return find_value(rx[0]);
// NOT
else if(rx.length == 2)
ret = ~ find_value(rx[1]);
else if(rx[1] == 'AND')
ret = find_value(rx[0]) & find_value(rx[2]);
else if(rx[1] == 'OR')
ret = find_value(rx[0]) | find_value(rx[2]);
else if(rx[1] == 'LSHIFT')
ret = find_value(rx[0]) << find_value(rx[2]);
else if(rx[1] == 'RSHIFT')
ret = find_value(rx[0]) >> find_value(rx[2]);
hash[name] = ret;
console.log(name, ret);
return ret;
};
console.log(find_value('a'));
var fs = require('fs');
var input = fs.readFileSync('input8.txt', 'utf8');
var out = input.split('-').reduce(function(m, st){
m.c += st.length;
m.s += st.replace(/\\\\|\\x..|\\"/g, 'x').length - 2;
m.e += st.replace(/\\/g, '\\\\').replace(/"/g, '\\"').length + 2;
return m;
}, {c:0,s:0, e:0});
console.log(out);
var fs = require('fs');
var inp = fs.readFileSync('inp', 'utf8');
var citiesMap = {'AlphaCentauri':1, 'Snowdin':1, 'Tambi':1, 'Faerun':1, 'Norrath':1, 'Straylight':1, 'Tristram':1, 'Arbre':1};
// var cities = Object.keys(citiesMap);
function length(c1, c2){
var rx;
if(inp.indexOf(c1+' to '+c2) > -1)
return +(new RegExp(c1+' to '+c2+' = (\\d+)', 'g')).exec(inp)[1];
return +(new RegExp(c2+' to '+c1+' = (\\d+)', 'g')).exec(inp)[1];
}
function next(map, fromCity, dpt){
var min = 0;
var cities = Object.keys(map);
for (var i = cities.length - 1; i >= 0; i--) {
var toCity = cities[i];
if(map[toCity]){
var newMap = Object.assign({}, map);
delete newMap[toCity];
var dist = fromCity ? length(fromCity, toCity) : 0;
var totalDist = next(newMap, cities[i], dpt+1) + dist;
console.log((new Array(dpt)).join(' - '), toCity, totalDist);
min = Math.max(min, totalDist)
}
};
return !cities.length ? 0 : min;
};
var ans = next(citiesMap, 0,0);
console.log(ans);
var inp = '1321131112';
function next(prev, c){
var rx = prev.match(/(.)\1*/g);
var newVal = rx.reduce(function(m, v){return m + v.length + v[0];}, '');
return c > 1 ? next(newVal, c-1) : newVal;
};
var ans = next(inp, 50);
console.log(ans.length);
var inp = 'hxbxxyzz';
var pass = inp;
function increment(str){
// - convert to base 26 and add 1 and convert back
// Give the size of input, manual is easier
if(!str) return '';
var i = str.length - 1;
if(str.charCodeAt(i) == 122)
return increment(str.slice(0,i)) + 'a' +str.slice(i+1);
return str.slice(0,i)+String.fromCharCode(str.charCodeAt(i) + 1)+str.slice(i+1);
};
console.log(pass)
do{
pass = increment(pass);
console.log(pass)
c1 = (Array(pass.length - 2).join('a').split('')).some(function(v,i){
return pass.charCodeAt(i+1) == pass.charCodeAt(i) + 1 && pass.charCodeAt(i+2) == pass.charCodeAt(i) + 2;
});
c2 = !Boolean(pass.match(/[iol]/));
c3 = Boolean(pass.match(/(.)\1.*(.)\2/g));
}while(!c1 || !c2 || !c3);
console.log(pass);
var fs = require('fs');
var inp = fs.readFileSync('inp', 'utf8');
var json = JSON.parse(inp);
function iterate(val){
if(typeof val == 'object'){
if(val instanceof Array)
val.forEach(iterate);
else{
var keys = Object.keys(val);
var red = keys.some(function(k){return val[k] == 'red';});
if(red)
keys.forEach(function(k){delete val[k];})
else
keys.forEach(function(k){iterate(val[k]);})
}
}
};
iterate(json);
inp = JSON.stringify(json);
// inp = inp.replace(/\{[^\}^\{]*:"red"[^\}]*\}/g, '')
ans = inp.match(/(-?\d)+/g).reduce(function(m,v){return m+ +v}, 0);
console.log(ans);
var fs = require('fs');
var inp = fs.readFileSync('inp', 'utf8');
var hash = inp.split('\n').reduce(function(m, v){
var rx = /^(.*) would (gain|lose) (\d+) happiness units by sitting next to (.*)\./.exec(v);
var p1 = rx[1],
p2 = rx[4],
value = (rx[2] == 'gain' || -1) * rx[3];
if(!m[p1]) m[p1] = {};
if(!m[p2]) m[p2] = {};
m[p1][p2] = value;
return m;
}, {});
Alice would gain 2 happiness units by sitting next to Bob.
Alice would gain 26 happiness units by sitting next to Carol.
Alice would lose 82 happiness units by sitting next to David.
Alice would lose 75 happiness units by sitting next to Eric.
Alice would gain 42 happiness units by sitting next to Frank.
Alice would gain 38 happiness units by sitting next to George.
Alice would gain 39 happiness units by sitting next to Mallory.
Bob would gain 40 happiness units by sitting next to Alice.
Bob would lose 61 happiness units by sitting next to Carol.
Bob would lose 15 happiness units by sitting next to David.
Bob would gain 63 happiness units by sitting next to Eric.
Bob would gain 41 happiness units by sitting next to Frank.
Bob would gain 30 happiness units by sitting next to George.
Bob would gain 87 happiness units by sitting next to Mallory.
Carol would lose 35 happiness units by sitting next to Alice.
Carol would lose 99 happiness units by sitting next to Bob.
Carol would lose 51 happiness units by sitting next to David.
Carol would gain 95 happiness units by sitting next to Eric.
Carol would gain 90 happiness units by sitting next to Frank.
Carol would lose 16 happiness units by sitting next to George.
Carol would gain 94 happiness units by sitting next to Mallory.
David would gain 36 happiness units by sitting next to Alice.
David would lose 18 happiness units by sitting next to Bob.
David would lose 65 happiness units by sitting next to Carol.
David would lose 18 happiness units by sitting next to Eric.
David would lose 22 happiness units by sitting next to Frank.
David would gain 2 happiness units by sitting next to George.
David would gain 42 happiness units by sitting next to Mallory.
Eric would lose 65 happiness units by sitting next to Alice.
Eric would gain 24 happiness units by sitting next to Bob.
Eric would gain 100 happiness units by sitting next to Carol.
Eric would gain 51 happiness units by sitting next to David.
Eric would gain 21 happiness units by sitting next to Frank.
Eric would gain 55 happiness units by sitting next to George.
Eric would lose 44 happiness units by sitting next to Mallory.
Frank would lose 48 happiness units by sitting next to Alice.
Frank would gain 91 happiness units by sitting next to Bob.
Frank would gain 8 happiness units by sitting next to Carol.
Frank would lose 66 happiness units by sitting next to David.
Frank would gain 97 happiness units by sitting next to Eric.
Frank would lose 9 happiness units by sitting next to George.
Frank would lose 92 happiness units by sitting next to Mallory.
George would lose 44 happiness units by sitting next to Alice.
George would lose 25 happiness units by sitting next to Bob.
George would gain 17 happiness units by sitting next to Carol.
George would gain 92 happiness units by sitting next to David.
George would lose 92 happiness units by sitting next to Eric.
George would gain 18 happiness units by sitting next to Frank.
George would gain 97 happiness units by sitting next to Mallory.
Mallory would gain 92 happiness units by sitting next to Alice.
Mallory would lose 96 happiness units by sitting next to Bob.
Mallory would lose 51 happiness units by sitting next to Carol.
Mallory would lose 81 happiness units by sitting next to David.
Mallory would gain 31 happiness units by sitting next to Eric.
Mallory would lose 73 happiness units by sitting next to Frank.
Mallory would lose 89 happiness units by sitting next to George.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment