Started day 11 parallel rewrite
This commit is contained in:
parent
d9d37754fd
commit
a6aaafb7ff
@ -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 second floor contains a polonium-compatible microchip and a promethium-compatible microchip.
|
||||||
The third floor contains nothing relevant.
|
The third floor contains nothing relevant.
|
||||||
The fourth floor contains nothing relevant.
|
The fourth floor contains nothing relevant.
|
||||||
|
4
day11/input_test.txt
Normal file
4
day11/input_test.txt
Normal 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
151
day11/old/part1.js
Normal 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);
|
||||||
|
}
|
@ -10,6 +10,8 @@
|
|||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"js-combinatorics": "^0.5.2",
|
"js-combinatorics": "^0.5.2",
|
||||||
|
"paralleljs": "^0.2.1",
|
||||||
|
"pararr": "0.0.3",
|
||||||
"underscore": "^1.8.3"
|
"underscore": "^1.8.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ var initialFloors = [];
|
|||||||
var initialLift = 0;
|
var initialLift = 0;
|
||||||
var minFloor = 0;
|
var minFloor = 0;
|
||||||
var _ = require("underscore");
|
var _ = require("underscore");
|
||||||
var combinatorics = require("js-combinatorics");
|
|
||||||
var previous = [];
|
var previous = [];
|
||||||
|
var p = require("pararr");
|
||||||
|
|
||||||
var scenarios = [
|
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)=>{
|
input.split("\n").filter((a)=>(a)).forEach((line)=>{
|
||||||
var floorNums = ["first", "second", "third", "fourth"];
|
var floorNums = ["first", "second", "third", "fourth"];
|
||||||
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
|
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
|
||||||
@ -49,8 +49,9 @@ function getMinFloors(floors){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function possibleScenarios(scenario){
|
function possibleScenarios(scenario){
|
||||||
var newScenarios = [];
|
var combinatorics = require("js-combinatorics");
|
||||||
|
|
||||||
|
var newScenarios = [];
|
||||||
var ids = scenario.floors[scenario.lift].map((a, i)=>(i));
|
var ids = scenario.floors[scenario.lift].map((a, i)=>(i));
|
||||||
var arrangements = [];
|
var arrangements = [];
|
||||||
if(ids.length >= 1){
|
if(ids.length >= 1){
|
||||||
@ -71,7 +72,7 @@ function possibleScenarios(scenario){
|
|||||||
floors[scenario.lift] = floors[scenario.lift].filter((a)=>(a));
|
floors[scenario.lift] = floors[scenario.lift].filter((a)=>(a));
|
||||||
newScenarios.push({lift: scenario.lift + 1, floors: floors});
|
newScenarios.push({lift: scenario.lift + 1, floors: floors});
|
||||||
}
|
}
|
||||||
if((scenario.lift > minFloor)){
|
if((scenario.lift > 0)){
|
||||||
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
||||||
floors[scenario.lift - 1].push(floors[scenario.lift][arrangement[0]]);
|
floors[scenario.lift - 1].push(floors[scenario.lift][arrangement[0]]);
|
||||||
delete floors[scenario.lift][arrangement[0]];
|
delete floors[scenario.lift][arrangement[0]];
|
||||||
@ -86,33 +87,95 @@ function possibleScenarios(scenario){
|
|||||||
return newScenarios;
|
return newScenarios;
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextSetOfScenarios(){
|
function nextSetOfScenarios(scenarios, callback){
|
||||||
var newScenarios = [];
|
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){
|
for(var scenario of scenarios){
|
||||||
newScenarios = newScenarios.concat(possibleScenarios(scenario));
|
newScenarios = newScenarios.concat(possibleScenarios(scenario));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("removing invalid");
|
||||||
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
||||||
console.log("newLength", newScenarios.length);
|
console.log("newLength", newScenarios.length);
|
||||||
|
console.log("calc min");
|
||||||
localMinFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
|
localMinFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
|
||||||
//console.log(newScenarios);
|
console.log("sorting");
|
||||||
//newScenarios = newScenarios.filter((scenario)=>(getMinFloors(scenario.floors) == localMinFloor));
|
|
||||||
newScenarios.forEach((scenario)=>{scenario.floors.forEach((a)=>(a.sort()));})
|
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 = _.uniq(newScenarios, (scenario)=>(JSON.stringify(scenario)));
|
||||||
newScenarios = newScenarios.filter((scenario)=>(previous.indexOf(JSON.stringify(scenario)) == -1));
|
console.log("removing previous");
|
||||||
//previous = [];
|
newScenarios = _.difference(newScenarios, previous);
|
||||||
newScenarios.map((scenario)=>(previous.push(JSON.stringify(scenario))));
|
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);
|
console.log("MIN", localMinFloor);
|
||||||
if(localMinFloor == 3){
|
if(localMinFloor == 3){
|
||||||
console.log("SOLVED");
|
console.log("SOLVED");
|
||||||
}
|
}
|
||||||
scenarios = newScenarios;
|
scenarios = newScenarios;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
var i = 0;
|
var i = 0;
|
||||||
|
|
||||||
while(minFloor < 3){
|
while(minFloor < 3){
|
||||||
i++;
|
i++;
|
||||||
nextSetOfScenarios();
|
nextSetOfScenarios();
|
||||||
|
console.log("it", i);
|
||||||
console.log(scenarios.length);
|
console.log(scenarios.length);
|
||||||
}
|
}
|
||||||
console.log(i);
|
console.log(i);
|
||||||
|
*/
|
||||||
|
|
||||||
|
var i = 0;
|
||||||
|
|
||||||
|
var callback = (scenarios)=>{
|
||||||
|
if(i < 10){
|
||||||
|
nextSetOfScenarios(scenarios, callback);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
console.log(possibilities);
|
||||||
|
p.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextSetOfScenarios(scenarios, callback);
|
164
day11/part1.js
164
day11/part1.js
@ -2,6 +2,8 @@ var initialFloors = [];
|
|||||||
var initialLift = 0;
|
var initialLift = 0;
|
||||||
var minFloor = 0;
|
var minFloor = 0;
|
||||||
var _ = require("underscore");
|
var _ = require("underscore");
|
||||||
|
var combinatorics = require("js-combinatorics");
|
||||||
|
var previous = [];
|
||||||
|
|
||||||
var scenarios = [
|
var scenarios = [
|
||||||
{
|
{
|
||||||
@ -15,25 +17,25 @@ input.split("\n").filter((a)=>(a)).forEach((line)=>{
|
|||||||
var floorNums = ["first", "second", "third", "fourth"];
|
var floorNums = ["first", "second", "third", "fourth"];
|
||||||
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
|
var lineParts = line.match(/The ([^ ]+) floor contains (.*)./);
|
||||||
var floorNum = floorNums.indexOf(lineParts[1]);
|
var floorNum = floorNums.indexOf(lineParts[1]);
|
||||||
initialFloors[floorNum] = {chips: [], generators: []};
|
initialFloors[floorNum] = [];
|
||||||
var items = lineParts[2].replace("and ", ", ").split(", ").filter((a)=>(a));
|
var items = lineParts[2].replace("and ", ", ").split(", ").filter((a)=>(a));
|
||||||
items.forEach((item)=>{
|
items.forEach((item)=>{
|
||||||
if(item.indexOf("generator") != -1){
|
if(item.indexOf("generator") != -1){
|
||||||
var itemParts = item.match(/a ([^ ]+) generator/);
|
var itemParts = item.match(/a ([^ ]+) generator/);
|
||||||
initialFloors[floorNum].generators.push(itemParts[1]);
|
initialFloors[floorNum].push(["generator", itemParts[1]])
|
||||||
}
|
}
|
||||||
else if(item.indexOf("microchip") != -1){
|
else if(item.indexOf("microchip") != -1){
|
||||||
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
||||||
initialFloors[floorNum].chips.push(itemParts[1]);
|
initialFloors[floorNum].push(["microchip", itemParts[1]])
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function checkFloors(floors){
|
function checkFloors(floors){
|
||||||
for(var floor of floors){
|
for(var floor of floors){
|
||||||
if(floor.generators.length > 0){
|
if(floor.filter((a)=>(a[0] == "generator")).length > 0){
|
||||||
for(var elem of floor.chips){
|
for(var element of floor.filter((a)=>(a[0] == "microchip")).map((a)=>(a[1]))){
|
||||||
if(floor.generators.indexOf(elem) == -1){
|
if(floor.filter((a)=>(a[0] == "generator")).map((a)=>(a[1])).indexOf(element) == -1){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,110 +44,84 @@ function checkFloors(floors){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function possibleMoves(scenario){
|
function getMinFloors(floors){
|
||||||
var newScenarios = [];
|
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
|
||||||
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){
|
function possibleScenarios(scenario){
|
||||||
/*
|
var newScenarios = [];
|
||||||
if(scenario.lift > 0){
|
|
||||||
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
var ids = scenario.floors[scenario.lift].map((a, i)=>(i));
|
||||||
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
|
var arrangements = [];
|
||||||
floors[scenario.lift].generators = floors[scenario.lift].generators.filter((a)=>(a != generator));
|
if(ids.length >= 1){
|
||||||
floors[scenario.lift - 1].chips.push(chip);
|
arrangements = arrangements.concat(combinatorics.combination(ids, 1).toArray());
|
||||||
floors[scenario.lift - 1].generators.push(generator);
|
|
||||||
newScenarios.push({lift: scenario.lift - 1, floors: floors});
|
|
||||||
}
|
}
|
||||||
*/
|
if(ids.length >= 2){
|
||||||
|
arrangements = arrangements.concat(combinatorics.combination(ids, 2).toArray());
|
||||||
|
}
|
||||||
|
arrangements.forEach((arrangement)=>{
|
||||||
if(scenario.lift < 3){
|
if(scenario.lift < 3){
|
||||||
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
||||||
floors[scenario.lift].chips = floors[scenario.lift].chips.filter((a)=>(a != chip));
|
floors[scenario.lift + 1].push(floors[scenario.lift][arrangement[0]]);
|
||||||
floors[scenario.lift].generators = floors[scenario.lift].generators.filter((a)=>(a != generator));
|
delete floors[scenario.lift][arrangement[0]];
|
||||||
floors[scenario.lift + 1].chips.push(chip);
|
if(arrangement.length == 2){
|
||||||
floors[scenario.lift + 1].generators.push(generator);
|
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});
|
newScenarios.push({lift: scenario.lift + 1, floors: floors});
|
||||||
}
|
}
|
||||||
}
|
if((scenario.lift > minFloor)){
|
||||||
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));
|
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].push(floors[scenario.lift][arrangement[0]]);
|
||||||
floors[scenario.lift - 1].chips.push(chip);
|
delete floors[scenario.lift][arrangement[0]];
|
||||||
floors[scenario.lift - 1].chips.push(chip2);
|
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});
|
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;
|
return newScenarios;
|
||||||
}
|
}
|
||||||
|
|
||||||
function newPossibleScenarios(){
|
function nextSetOfScenarios(){
|
||||||
allNewScenarios = [];
|
var newScenarios = [];
|
||||||
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){
|
for(var scenario of scenarios){
|
||||||
var scenarioMin = 5;
|
newScenarios = newScenarios.concat(possibleScenarios(scenario));
|
||||||
for(var i in scenario.floors){
|
|
||||||
if((scenario.floors[i].chips.length > 0) || (scenario.floors[i].generators.length > 0)){
|
|
||||||
if(i < scenarioMin){
|
|
||||||
scenarioMin = i;
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
if((scenarioMin > min) && (scenarioMin != 5)){
|
|
||||||
min = scenarioMin
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return min;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var i = 0;
|
||||||
|
|
||||||
//console.log(JSON.stringify(possibleMoves(scenarios[0])));
|
while(minFloor < 3){
|
||||||
for(var i = 0; i < 8; i++){
|
i++;
|
||||||
scenarios = newPossibleScenarios();
|
nextSetOfScenarios();
|
||||||
console.log(JSON.stringify(scenarios));
|
console.log("it", i);
|
||||||
minFloor = getMinFloor()
|
console.log(scenarios.length);
|
||||||
console.log(minFloor);
|
|
||||||
}
|
}
|
||||||
|
console.log(i);
|
||||||
|
10
day11/test.js
Normal file
10
day11/test.js
Normal 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();
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user