Started day 11 parallel rewrite

This commit is contained in:
Tim Stallard 2016-12-13 23:09:27 +00:00
parent d9d37754fd
commit a6aaafb7ff
11 changed files with 316 additions and 110 deletions

View File

@ -1,4 +1,4 @@
The first floor contains a polonium generator, a thulium generator, a thulium-compatible microchip, a promethium generator, a ruthenium generator, a ruthenium-compatible microchip, a cobalt generator, and a cobalt-compatible microchip.
The first floor contains a polonium generator, a thulium generator, a thulium-compatible microchip, a promethium generator, a ruthenium generator, a ruthenium-compatible microchip, a cobalt generator, a elerium generator, a elerium-compatible microchip, a dilithium generator, a dilithium-compatible microchip, and a cobalt-compatible microchip.
The second floor contains a polonium-compatible microchip and a promethium-compatible microchip.
The third floor contains nothing relevant.
The fourth floor contains nothing relevant.

4
day11/input_test.txt Normal file
View File

@ -0,0 +1,4 @@
The first floor contains a thulium-compatible microchip.
The second floor contains nothing relevant.
The third floor contains nothing relevant.
The fourth floor contains nothing relevant.

151
day11/old/part1.js Normal file
View File

@ -0,0 +1,151 @@
var initialFloors = [];
var initialLift = 0;
var minFloor = 0;
var _ = require("underscore");
var scenarios = [
{
floors: initialFloors,
lift: initialLift
}
];
var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
input.split("\n").filter((a)=>(a)).forEach((line)=>{
var floorNums = ["first", "second", "third", "fourth"];
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
var floorNum = floorNums.indexOf(lineParts[1]);
initialFloors[floorNum] = {chips: [], generators: []};
var items = lineParts[2].replace("and ", ", ").split(", ").filter((a)=>(a));
items.forEach((item)=>{
if(item.indexOf("generator") != -1){
var itemParts = item.match(/a ([^ ]+) generator/);
initialFloors[floorNum].generators.push(itemParts[1]);
}
else if(item.indexOf("microchip") != -1){
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
initialFloors[floorNum].chips.push(itemParts[1]);
}
});
});
function checkFloors(floors){
for(var floor of floors){
if(floor.generators.length > 0){
for(var elem of floor.chips){
if(floor.generators.indexOf(elem) == -1){
return false;
}
}
}
}
return true;
}
function possibleMoves(scenario){
var newScenarios = [];
for(var chip of scenario.floors[scenario.lift].chips){
if(scenario.lift > minFloor){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift - 1].chips.push(chip);
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
if(scenario.lift < 3){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift + 1].chips.push(chip);
newScenarios.push({lift: scenario.lift + 1, floors: floors});
}
for(var generator of scenario.floors[scenario.lift].generators){
/*
if(scenario.lift > 0){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift].generators = floors[scenario.lift].generators.filter((a)=>(a != generator));
floors[scenario.lift - 1].chips.push(chip);
floors[scenario.lift - 1].generators.push(generator);
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
*/
if(scenario.lift < 3){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift].generators = floors[scenario.lift].generators.filter((a)=>(a != generator));
floors[scenario.lift + 1].chips.push(chip);
floors[scenario.lift + 1].generators.push(generator);
newScenarios.push({lift: scenario.lift + 1, floors: floors});
}
}
for(var chip2 of scenario.floors[scenario.lift].chips.filter((a)=>(a != chip))){
if(scenario.lift > minFloor){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip)).filter((a)=>(a != chip2));
floors[scenario.lift - 1].chips.push(chip);
floors[scenario.lift - 1].chips.push(chip2);
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
if(scenario.lift < 3){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip)).filter((a)=>(a != chip2));
floors[scenario.lift + 1].chips.push(chip);
floors[scenario.lift + 1].chips.push(chip2);
newScenarios.push({lift: scenario.lift + 1, floors: floors});
}
}
}
return newScenarios;
}
function newPossibleScenarios(){
allNewScenarios = [];
scenarios.forEach((scenario)=>{
allNewScenarios = allNewScenarios.concat(possibleMoves(scenario).filter((a)=>(checkFloors(a.floors))));
});
return allNewScenarios;
}
var i = 1;
var solved = false;
/*
while(!solved){
scenarios = newPossibleScenarios();
console.log(JSON.stringify(scenarios));
i++
console.log(i);
minFloor = getMinFloor();
console.log(minFloor);
if(minFloor == 3){
console.log("SOLVED");
solved = true;
}
}
*/
function getMinFloor(){
var min = 0;
for(var scenario of scenarios){
var scenarioMin = 5;
for(var i in scenario.floors){
if((scenario.floors[i].chips.length > 0) || (scenario.floors[i].generators.length > 0)){
if(i < scenarioMin){
scenarioMin = i;
}
}
}
if((scenarioMin > min) && (scenarioMin != 5)){
min = scenarioMin
}
}
return min;
}
//console.log(JSON.stringify(possibleMoves(scenarios[0])));
for(var i = 0; i < 8; i++){
scenarios = newPossibleScenarios();
console.log(JSON.stringify(scenarios));
minFloor = getMinFloor()
console.log(minFloor);
}

View File

@ -10,6 +10,8 @@
"license": "UNLICENSED",
"dependencies": {
"js-combinatorics": "^0.5.2",
"paralleljs": "^0.2.1",
"pararr": "0.0.3",
"underscore": "^1.8.3"
}
}

View File

@ -2,8 +2,8 @@ var initialFloors = [];
var initialLift = 0;
var minFloor = 0;
var _ = require("underscore");
var combinatorics = require("js-combinatorics");
var previous = [];
var p = require("pararr");
var scenarios = [
{
@ -12,7 +12,7 @@ var scenarios = [
}
];
var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
var input = require("fs").readFileSync("input_2.txt").toString().replace(/\r/g, "");
input.split("\n").filter((a)=>(a)).forEach((line)=>{
var floorNums = ["first", "second", "third", "fourth"];
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
@ -49,8 +49,9 @@ function getMinFloors(floors){
}
function possibleScenarios(scenario){
var newScenarios = [];
var combinatorics = require("js-combinatorics");
var newScenarios = [];
var ids = scenario.floors[scenario.lift].map((a, i)=>(i));
var arrangements = [];
if(ids.length >= 1){
@ -71,7 +72,7 @@ function possibleScenarios(scenario){
floors[scenario.lift] = floors[scenario.lift].filter((a)=>(a));
newScenarios.push({lift: scenario.lift + 1, floors: floors});
}
if((scenario.lift > minFloor)){
if((scenario.lift > 0)){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift - 1].push(floors[scenario.lift][arrangement[0]]);
delete floors[scenario.lift][arrangement[0]];
@ -86,33 +87,95 @@ function possibleScenarios(scenario){
return newScenarios;
}
function nextSetOfScenarios(){
var newScenarios = [];
function nextSetOfScenarios(scenarios, callback){
scenarioCheckFloors = (scenario)=>{
for(var floor of scenario.floors){
if(floor.filter((a)=>(a[0] == "generator")).length > 0){
for(var element of floor.filter((a)=>(a[0] == "microchip")).map((a)=>(a[1]))){
if(floor.filter((a)=>(a[0] == "generator")).map((a)=>(a[1])).indexOf(element) == -1){
return false;
}
}
}
}
return true;
};
scenarioSort = (scenario)=>{
scenario.floors.forEach((a)=>(a.sort()));
return scenario;
}
console.log("start", scenarios.length);
p.map(scenarios, possibleScenarios, (err, scenarios)=>{
scenarios = scenarios.reduce((a, b)=>(a.concat(b)), []);
console.log("possibilities", scenarios.length);
p.filter(scenarios, scenarioCheckFloors, (err, scenarios)=>{
console.log("valid", scenarios.length);
p.map(scenarios, scenarioSort, (err, scenarios)=>{
console.log("sorted", scenarios.length);
callback(scenarios);
});
});
})
//p.map(possibleScenarios).reduce(merge).spawn(forceMerge).then(callback);
/*
for(var scenario of scenarios){
newScenarios = newScenarios.concat(possibleScenarios(scenario));
}
console.log("removing invalid");
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
console.log("newLength", newScenarios.length);
console.log("calc min");
localMinFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
//console.log(newScenarios);
//newScenarios = newScenarios.filter((scenario)=>(getMinFloors(scenario.floors) == localMinFloor));
console.log("sorting");
newScenarios.forEach((scenario)=>{scenario.floors.forEach((a)=>(a.sort()));})
console.log("to JSON");
newScenarios = newScenarios.map((scenario)=>(JSON.stringify(scenario)));
console.log("dedupe");
newScenarios = _.uniq(newScenarios, (scenario)=>(JSON.stringify(scenario)));
newScenarios = newScenarios.filter((scenario)=>(previous.indexOf(JSON.stringify(scenario)) == -1));
//previous = [];
newScenarios.map((scenario)=>(previous.push(JSON.stringify(scenario))));
console.log("removing previous");
newScenarios = _.difference(newScenarios, previous);
console.log("adding to previous");
newScenarios.forEach((scenario)=>(previous.push(scenario)));
console.log("from JSON");
newScenarios = newScenarios.map((scenario)=>(JSON.parse(scenario)));
console.log("MIN", localMinFloor);
if(localMinFloor == 3){
console.log("SOLVED");
}
scenarios = newScenarios;
*/
}
/*
var i = 0;
while(minFloor < 3){
i++;
nextSetOfScenarios();
console.log("it", i);
console.log(scenarios.length);
}
console.log(i);
*/
var i = 0;
var callback = (scenarios)=>{
if(i < 10){
nextSetOfScenarios(scenarios, callback);
}
else{
console.log(possibilities);
p.destroy();
}
}
nextSetOfScenarios(scenarios, callback);

View File

@ -2,6 +2,8 @@ var initialFloors = [];
var initialLift = 0;
var minFloor = 0;
var _ = require("underscore");
var combinatorics = require("js-combinatorics");
var previous = [];
var scenarios = [
{
@ -15,25 +17,25 @@ input.split("\n").filter((a)=>(a)).forEach((line)=>{
var floorNums = ["first", "second", "third", "fourth"];
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
var floorNum = floorNums.indexOf(lineParts[1]);
initialFloors[floorNum] = {chips: [], generators: []};
initialFloors[floorNum] = [];
var items = lineParts[2].replace("and ", ", ").split(", ").filter((a)=>(a));
items.forEach((item)=>{
if(item.indexOf("generator") != -1){
var itemParts = item.match(/a ([^ ]+) generator/);
initialFloors[floorNum].generators.push(itemParts[1]);
initialFloors[floorNum].push(["generator", itemParts[1]])
}
else if(item.indexOf("microchip") != -1){
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
initialFloors[floorNum].chips.push(itemParts[1]);
initialFloors[floorNum].push(["microchip", itemParts[1]])
}
});
});
function checkFloors(floors){
for(var floor of floors){
if(floor.generators.length > 0){
for(var elem of floor.chips){
if(floor.generators.indexOf(elem) == -1){
if(floor.filter((a)=>(a[0] == "generator")).length > 0){
for(var element of floor.filter((a)=>(a[0] == "microchip")).map((a)=>(a[1]))){
if(floor.filter((a)=>(a[0] == "generator")).map((a)=>(a[1])).indexOf(element) == -1){
return false;
}
}
@ -42,110 +44,84 @@ function checkFloors(floors){
return true;
}
function possibleMoves(scenario){
function getMinFloors(floors){
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
}
function possibleScenarios(scenario){
var newScenarios = [];
for(var chip of scenario.floors[scenario.lift].chips){
if(scenario.lift > minFloor){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift - 1].chips.push(chip);
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
var ids = scenario.floors[scenario.lift].map((a, i)=>(i));
var arrangements = [];
if(ids.length >= 1){
arrangements = arrangements.concat(combinatorics.combination(ids, 1).toArray());
}
if(ids.length >= 2){
arrangements = arrangements.concat(combinatorics.combination(ids, 2).toArray());
}
arrangements.forEach((arrangement)=>{
if(scenario.lift < 3){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift + 1].chips.push(chip);
floors[scenario.lift + 1].push(floors[scenario.lift][arrangement[0]]);
delete floors[scenario.lift][arrangement[0]];
if(arrangement.length == 2){
floors[scenario.lift + 1].push(floors[scenario.lift][arrangement[1]]);
delete floors[scenario.lift][arrangement[1]];
}
floors[scenario.lift] = floors[scenario.lift].filter((a)=>(a));
newScenarios.push({lift: scenario.lift + 1, floors: floors});
}
for(var generator of scenario.floors[scenario.lift].generators){
/*
if(scenario.lift > 0){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift].generators = floors[scenario.lift].generators.filter((a)=>(a != generator));
floors[scenario.lift - 1].chips.push(chip);
floors[scenario.lift - 1].generators.push(generator);
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
*/
if(scenario.lift < 3){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
floors[scenario.lift].generators = floors[scenario.lift].generators.filter((a)=>(a != generator));
floors[scenario.lift + 1].chips.push(chip);
floors[scenario.lift + 1].generators.push(generator);
newScenarios.push({lift: scenario.lift + 1, floors: floors});
if((scenario.lift > minFloor)){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift - 1].push(floors[scenario.lift][arrangement[0]]);
delete floors[scenario.lift][arrangement[0]];
if(arrangement.length == 2){
floors[scenario.lift - 1].push(floors[scenario.lift][arrangement[1]]);
delete floors[scenario.lift][arrangement[1]];
}
floors[scenario.lift] = floors[scenario.lift].filter((a)=>(a));
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
for(var chip2 of scenario.floors[scenario.lift].chips.filter((a)=>(a != chip))){
if(scenario.lift > minFloor){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip)).filter((a)=>(a != chip2));
floors[scenario.lift - 1].chips.push(chip);
floors[scenario.lift - 1].chips.push(chip2);
newScenarios.push({lift: scenario.lift - 1, floors: floors});
}
if(scenario.lift < 3){
var floors = JSON.parse(JSON.stringify(scenario.floors));
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip)).filter((a)=>(a != chip2));
floors[scenario.lift + 1].chips.push(chip);
floors[scenario.lift + 1].chips.push(chip2);
newScenarios.push({lift: scenario.lift + 1, floors: floors});
}
}
}
});
return newScenarios;
}
function newPossibleScenarios(){
allNewScenarios = [];
scenarios.forEach((scenario)=>{
allNewScenarios = allNewScenarios.concat(possibleMoves(scenario).filter((a)=>(checkFloors(a.floors))));
});
return allNewScenarios;
}
var i = 1;
var solved = false;
/*
while(!solved){
scenarios = newPossibleScenarios();
console.log(JSON.stringify(scenarios));
i++
console.log(i);
minFloor = getMinFloor();
console.log(minFloor);
if(minFloor == 3){
console.log("SOLVED");
solved = true;
}
}
*/
function getMinFloor(){
var min = 0;
function nextSetOfScenarios(){
var newScenarios = [];
for(var scenario of scenarios){
var scenarioMin = 5;
for(var i in scenario.floors){
if((scenario.floors[i].chips.length > 0) || (scenario.floors[i].generators.length > 0)){
if(i < scenarioMin){
scenarioMin = i;
}
}
}
if((scenarioMin > min) && (scenarioMin != 5)){
min = scenarioMin
}
newScenarios = newScenarios.concat(possibleScenarios(scenario));
}
return min;
console.log("removing invalid");
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
console.log("newLength", newScenarios.length);
console.log("calc min");
localMinFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
console.log("sorting");
newScenarios.forEach((scenario)=>{scenario.floors.forEach((a)=>(a.sort()));})
console.log("to JSON");
newScenarios = newScenarios.map((scenario)=>(JSON.stringify(scenario)));
console.log("dedupe");
newScenarios = _.uniq(newScenarios, (scenario)=>(JSON.stringify(scenario)));
console.log("removing previous");
newScenarios = _.difference(newScenarios, previous);
console.log("adding to previous");
newScenarios.forEach((scenario)=>(previous.push(scenario)));
console.log("from JSON");
newScenarios = newScenarios.map((scenario)=>(JSON.parse(scenario)));
console.log("MIN", localMinFloor);
if(localMinFloor == 3){
console.log("SOLVED");
}
scenarios = newScenarios;
}
var i = 0;
//console.log(JSON.stringify(possibleMoves(scenarios[0])));
for(var i = 0; i < 8; i++){
scenarios = newPossibleScenarios();
console.log(JSON.stringify(scenarios));
minFloor = getMinFloor()
console.log(minFloor);
while(minFloor < 3){
i++;
nextSetOfScenarios();
console.log("it", i);
console.log(scenarios.length);
}
console.log(i);

10
day11/test.js Normal file
View File

@ -0,0 +1,10 @@
var p = require("pararr");
//var p = new Parallel([1, 2]);
square = (num)=>([1, Math.pow(num, 2)]);
merge = ([a, b])=>(a.concat(b));
p.map([1, 2, 3], square, (err, data)=>{
console.log(data);
p.destroy();
});