adventofcode-2016/day11/part1.js

152 lines
5.0 KiB
JavaScript

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);
}