119 lines
4.1 KiB
JavaScript
119 lines
4.1 KiB
JavaScript
|
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);
|