forked from bibimission/Advent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.js
87 lines (73 loc) · 1.73 KB
/
day14.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var fs = require('fs');
var instructions = [];
calculate();
function calculate(){
var fileName = 'input.txt';
var file = fs.readFileSync(fileName);
var lines = file.toString().split('\r\n');
lines.forEach(function(line){
var tokens = line.split("=>");
var ingredients = tokens[0].split(',');
var resultat = tokens[1].split(' ');
var recette = new Recette();
recette.result = parseIngredient(resultat);
ingredients.forEach(function(ingr){
recette.ingredients.push(parseIngredient(ingr.split(" ")));
});
instructions.push(recette);
});
//console.log(instructions);
var recettesBase = [];
instructions.forEach(function(el){
var ingr = el.ingredients.forEach(function(e){
if(e.nom=="ORE"){
recettesBase.push(el);
}
});
});
//console.log(recettesBase);
var recetteFinale = instructions.filter(function(el){
return el.result.nom == "FUEL";
});
console.log(findPath(recetteFinale));
}
function findPath(recette){
var viendage=[];
console.log(recette);
if(recette.ingredients.length>0){
console.log(recette);
recette.ingredients.forEach(function(ingr){
var recettes = [];
instructions.forEach(function(el){
console.log(el);
if(el.result.nom==ingr.nom){
recettes.push(el);
}
});
console.log(recettes);
if(recettes!=null){
recettes.forEach(function(rec){
viendage.concat(findPath(rec));
});
}
});
}
return viendage;
}
function Recette(){
this.ingredients=[];
this.result=null;
}
function Ingredient(nom){
this.nom=nom;
this.quantite=0;
}
function parseIngredient(tokens){
tokens = tokens.filter(function (el) {
return el != '';
});
var result=new Ingredient(null);
result.quantite=parseInt(tokens[0],10);
result.nom = tokens[1];
return result;
}