Added day 11 mess (new2 works)
This commit is contained in:
parent
6693f6c637
commit
e532fa96f6
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
4
day11/input.txt
Normal file
4
day11/input.txt
Normal file
@ -0,0 +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 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_2.txt
Normal file
4
day11/input_2.txt
Normal file
@ -0,0 +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 second floor contains a polonium-compatible microchip and a promethium-compatible microchip.
|
||||
The third floor contains nothing relevant.
|
||||
The fourth floor contains nothing relevant.
|
15
day11/package.json
Normal file
15
day11/package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "day11",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "part1.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"js-combinatorics": "^0.5.2",
|
||||
"underscore": "^1.8.3"
|
||||
}
|
||||
}
|
151
day11/part1.js
Normal file
151
day11/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);
|
||||
}
|
125
day11/part1_new.js
Normal file
125
day11/part1_new.js
Normal file
@ -0,0 +1,125 @@
|
||||
var initialFloors = [];
|
||||
var initialLift = 0;
|
||||
var minFloor = 0;
|
||||
var _ = require("underscore");
|
||||
var combinatorics = require("js-combinatorics");
|
||||
var previous = [];
|
||||
|
||||
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] = [];
|
||||
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].push(["generator", itemParts[1]])
|
||||
}
|
||||
else if(item.indexOf("microchip") != -1){
|
||||
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
||||
initialFloors[floorNum].push(["microchip", itemParts[1]])
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function checkFloors(floors){
|
||||
for(var floor of 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;
|
||||
}
|
||||
|
||||
function getMinFloors(floors){
|
||||
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
|
||||
}
|
||||
|
||||
function possibleScenarios(scenario){
|
||||
var newScenarios = [];
|
||||
|
||||
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 + 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});
|
||||
}
|
||||
if((scenario.lift > minFloor)){
|
||||
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
||||
if(floors[scenario.lift][arrangement[0]][0] == "microchip"){
|
||||
floors[scenario.lift - 1].push(floors[scenario.lift][arrangement[0]]);
|
||||
delete floors[scenario.lift][arrangement[0]];
|
||||
if(arrangement.length == 2){
|
||||
if(floors[scenario.lift][arrangement[1]][0] == "microchip"){
|
||||
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});
|
||||
}
|
||||
}
|
||||
else{
|
||||
floors[scenario.lift] = floors[scenario.lift].filter((a)=>(a));
|
||||
newScenarios.push({lift: scenario.lift - 1, floors: floors});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return newScenarios;
|
||||
}
|
||||
|
||||
function nextSetOfScenarios(){
|
||||
var newScenarios = [];
|
||||
for(var scenario of scenarios){
|
||||
newScenarios = newScenarios.concat(possibleScenarios(scenario));
|
||||
}
|
||||
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
||||
console.log("newLength", newScenarios.length);
|
||||
minFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
|
||||
newScenarios = newScenarios.filter((scenario)=>(getMinFloors(scenario.floors) >= minFloor));
|
||||
scenario.floors.map((a)=>(a.sort()));
|
||||
newScenarios = _.uniq(newScenarios, (scenario)=>(JSON.stringify(scenario)));
|
||||
newScenarios = newScenarios.filter((scenario)=>(previous.indexOf(JSON.stringify(scenario)) == -1));
|
||||
newScenarios.map((scenario)=>(previous.push(JSON.stringify(scenario))))
|
||||
console.log("MIN", minFloor);
|
||||
if(minFloor == 3){
|
||||
console.log("SOLVED");
|
||||
}
|
||||
scenarios = newScenarios;
|
||||
//console.log(JSON.stringify(scenarios));
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
while(minFloor < 3){
|
||||
i++;
|
||||
nextSetOfScenarios();
|
||||
console.log(scenarios.length);
|
||||
}
|
||||
console.log(i);
|
118
day11/part1_new2.js
Normal file
118
day11/part1_new2.js
Normal file
@ -0,0 +1,118 @@
|
||||
var initialFloors = [];
|
||||
var initialLift = 0;
|
||||
var minFloor = 0;
|
||||
var _ = require("underscore");
|
||||
var combinatorics = require("js-combinatorics");
|
||||
var previous = [];
|
||||
|
||||
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] = [];
|
||||
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].push(["generator", itemParts[1]])
|
||||
}
|
||||
else if(item.indexOf("microchip") != -1){
|
||||
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
||||
initialFloors[floorNum].push(["microchip", itemParts[1]])
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function checkFloors(floors){
|
||||
for(var floor of 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;
|
||||
}
|
||||
|
||||
function getMinFloors(floors){
|
||||
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
|
||||
}
|
||||
|
||||
function possibleScenarios(scenario){
|
||||
var newScenarios = [];
|
||||
|
||||
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 + 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});
|
||||
}
|
||||
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});
|
||||
}
|
||||
});
|
||||
return newScenarios;
|
||||
}
|
||||
|
||||
function nextSetOfScenarios(){
|
||||
var newScenarios = [];
|
||||
for(var scenario of scenarios){
|
||||
newScenarios = newScenarios.concat(possibleScenarios(scenario));
|
||||
}
|
||||
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
||||
console.log("newLength", newScenarios.length);
|
||||
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));
|
||||
newScenarios.forEach((scenario)=>{scenario.floors.forEach((a)=>(a.sort()));})
|
||||
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("MIN", localMinFloor);
|
||||
if(localMinFloor == 3){
|
||||
console.log("SOLVED");
|
||||
}
|
||||
scenarios = newScenarios;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
while(minFloor < 3){
|
||||
i++;
|
||||
nextSetOfScenarios();
|
||||
console.log(scenarios.length);
|
||||
}
|
||||
console.log(i);
|
130
day11/part1_new3.js
Normal file
130
day11/part1_new3.js
Normal file
@ -0,0 +1,130 @@
|
||||
var initialFloors = [];
|
||||
var initialLift = 0;
|
||||
var minFloor = 0;
|
||||
var _ = require("underscore");
|
||||
var combinatorics = require("js-combinatorics");
|
||||
var previous = [];
|
||||
|
||||
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] = [];
|
||||
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].push(["generator", itemParts[1]])
|
||||
}
|
||||
else if(item.indexOf("microchip") != -1){
|
||||
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
||||
initialFloors[floorNum].push(["microchip", itemParts[1]])
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function checkFloors(floors){
|
||||
for(var floor of 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;
|
||||
}
|
||||
|
||||
function getMinFloors(floors){
|
||||
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
|
||||
}
|
||||
|
||||
function possibleScenarios(scenario){
|
||||
var newScenarios = [[], []];
|
||||
|
||||
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 + 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[0].push({lift: scenario.lift + 1, floors: floors});
|
||||
}
|
||||
if((scenario.lift > minFloor)){
|
||||
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
||||
var generator = (arrangement.map((a)=>(floors[scenario.lift][a][0])).indexOf("generator") > -1);
|
||||
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));
|
||||
if(generator){
|
||||
newScenarios[1].push({lift: scenario.lift - 1, floors: floors});
|
||||
}
|
||||
else{
|
||||
newScenarios[0].push({lift: scenario.lift - 1, floors: floors});
|
||||
}
|
||||
}
|
||||
});
|
||||
return newScenarios;
|
||||
}
|
||||
|
||||
function nextSetOfScenarios(){
|
||||
var newScenariosSets = [[], []];
|
||||
for(var scenario of scenarios){
|
||||
var possibleScenariosSet = possibleScenarios(scenario);
|
||||
newScenariosSets[0] = newScenariosSets[0].concat(possibleScenariosSet[0]);
|
||||
newScenariosSets[1] = newScenariosSets[1].concat(possibleScenariosSet[1]);
|
||||
}
|
||||
if(newScenariosSets[0].length > 0){
|
||||
var newScenarios = newScenariosSets[0];
|
||||
}
|
||||
else{
|
||||
console.log("falling back");
|
||||
var newScenarios = newScenariosSets[1];
|
||||
}
|
||||
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
||||
console.log("newLength", newScenarios.length);
|
||||
minFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
|
||||
newScenarios = newScenarios.filter((scenario)=>(getMinFloors(scenario.floors) >= minFloor));
|
||||
newScenarios = _.uniq(newScenarios, (scenario)=>(JSON.stringify(scenario)));
|
||||
newScenarios = newScenarios.filter((scenario)=>(previous.indexOf(JSON.stringify(scenario)) == -1));
|
||||
newScenarios.map((scenario)=>(previous.push(JSON.stringify(scenario))));
|
||||
console.log("MIN", minFloor);
|
||||
if(minFloor == 3){
|
||||
console.log("SOLVED");
|
||||
}
|
||||
scenarios = newScenarios;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
while(minFloor < 3){
|
||||
i++;
|
||||
nextSetOfScenarios();
|
||||
console.log(scenarios.length);
|
||||
}
|
||||
console.log(i);
|
126
day11/part1_new4_unopt.js
Normal file
126
day11/part1_new4_unopt.js
Normal file
@ -0,0 +1,126 @@
|
||||
var initialFloors = [];
|
||||
var initialLift = 0;
|
||||
var minFloor = 0;
|
||||
var _ = require("underscore");
|
||||
var combinatorics = require("js-combinatorics");
|
||||
var previous = [];
|
||||
|
||||
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] = [];
|
||||
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].push(["generator", itemParts[1]])
|
||||
}
|
||||
else if(item.indexOf("microchip") != -1){
|
||||
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
||||
initialFloors[floorNum].push(["microchip", itemParts[1]])
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function checkFloors(floors){
|
||||
for(var floor of 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;
|
||||
}
|
||||
|
||||
function getMinFloors(floors){
|
||||
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
|
||||
}
|
||||
|
||||
function possibleScenarios(scenario){
|
||||
var newScenarios = [[], []];
|
||||
|
||||
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 + 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[0].push({lift: scenario.lift + 1, floors: floors});
|
||||
}
|
||||
if((scenario.lift > minFloor)){
|
||||
var floors = JSON.parse(JSON.stringify(scenario.floors));
|
||||
var generator = (arrangement.map((a)=>(floors[scenario.lift][a][0])).indexOf("generator") > -1);
|
||||
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));
|
||||
if(generator){
|
||||
newScenarios[1].push({lift: scenario.lift - 1, floors: floors});
|
||||
}
|
||||
else{
|
||||
newScenarios[0].push({lift: scenario.lift - 1, floors: floors});
|
||||
}
|
||||
}
|
||||
});
|
||||
return newScenarios;
|
||||
}
|
||||
|
||||
function nextSetOfScenarios(){
|
||||
var newScenarios = [];
|
||||
for(var scenario of scenarios){
|
||||
var possibleScenariosSet = possibleScenarios(scenario);
|
||||
newScenariosSets[0] = newScenariosSets[0].concat(possibleScenariosSet[0]);
|
||||
newScenariosSets[1] = newScenariosSets[1].concat(possibleScenariosSet[1]);
|
||||
}
|
||||
if(newScenariosSets[0].length > 0){
|
||||
var newScenarios = newScenariosSets[0];
|
||||
}
|
||||
else{
|
||||
console.log("falling back");
|
||||
var newScenarios = newScenariosSets[1];
|
||||
}
|
||||
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
||||
console.log("newLength", newScenarios.length);
|
||||
minFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)))
|
||||
console.log("MIN", minFloor);
|
||||
if(minFloor == 3){
|
||||
console.log("SOLVED");
|
||||
}
|
||||
scenarios = newScenarios;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
while(minFloor < 3){
|
||||
i++;
|
||||
nextSetOfScenarios();
|
||||
console.log(scenarios.length);
|
||||
}
|
||||
console.log(i);
|
116
day11/part1_new5_unopt.js
Normal file
116
day11/part1_new5_unopt.js
Normal file
@ -0,0 +1,116 @@
|
||||
var initialFloors = [];
|
||||
var initialLift = 0;
|
||||
var minFloor = 0;
|
||||
var _ = require("underscore");
|
||||
var combinatorics = require("js-combinatorics");
|
||||
var previous = [];
|
||||
|
||||
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] = [];
|
||||
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].push(["generator", itemParts[1]])
|
||||
}
|
||||
else if(item.indexOf("microchip") != -1){
|
||||
var itemParts = item.match(/a ([^ ]+)-compatible microchip/);
|
||||
initialFloors[floorNum].push(["microchip", itemParts[1]])
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function checkFloors(floors){
|
||||
for(var floor of 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;
|
||||
}
|
||||
|
||||
function getMinFloors(floors){
|
||||
return floors.map((a)=>(Boolean(a.length))).indexOf(true);
|
||||
}
|
||||
|
||||
function possibleScenarios(scenario){
|
||||
var newScenarios = [];
|
||||
|
||||
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 + 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});
|
||||
}
|
||||
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});
|
||||
}
|
||||
});
|
||||
return newScenarios;
|
||||
}
|
||||
|
||||
function nextSetOfScenarios(){
|
||||
var newScenarios = [];
|
||||
for(var scenario of scenarios){
|
||||
newScenarios = newScenarios.concat(possibleScenarios(scenario));
|
||||
}
|
||||
console.log("new incorr", newScenarios.length);
|
||||
newScenarios = newScenarios.filter((scenario)=>(checkFloors(scenario.floors)));
|
||||
console.log("new corr", newScenarios.length);
|
||||
minFloor = newScenarios.map((scenario)=>(getMinFloors(scenario.floors))).reduce((a, b)=>(Math.max(a, b)));
|
||||
newScenarios.forEach((scenario)=>{scenario.floors.forEach((a)=>(a.sort()));})
|
||||
newScenarios = newScenarios.filter((scenario)=>(previous.indexOf(JSON.stringify(scenario)) == -1));
|
||||
newScenarios.map((scenario)=>(previous.push(JSON.stringify(scenario))));
|
||||
console.log("MIN", minFloor);
|
||||
if(minFloor == 3){
|
||||
console.log("SOLVED");
|
||||
}
|
||||
scenarios = newScenarios;
|
||||
//console.log(JSON.stringify(scenarios));
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
while(minFloor < 3){
|
||||
i++;
|
||||
nextSetOfScenarios();
|
||||
console.log(scenarios.length);
|
||||
}
|
||||
console.log(i);
|
Loading…
x
Reference in New Issue
Block a user