class monkey{
constructor(){
this.id = -1;
this.items = new Array();
this.operation = new Array();
this.test=-1;
this.trueThrow= -1;
this.falseThrow = -1;
this.count = 0;
}
}
function calculateResult(input)
{
let m={};
//console.log(input)
let input_a = input.split('\n')
//console.log(input_a)
let mm;
input_a.forEach((value)=>{
value= value.trim();
let first = value.split(' ')[0];
let second = value.split(' ')[1];
let third = value.split(' ')[1];
if(first == 'Monkey') // depicting monkey
{
mm = new monkey();
let mon_items = (value.split(' ')[1]).replace(':','')
mm.id=mon_items;
}
else if(first == 'Starting')
{
let mon_items = value.split(': ')[1].split(', ').map((v)=> {return parseInt(v)})
mm.items = [...mm.items,...mon_items];
}
else if(first == 'Operation:')
{
let mon_items = value.split('= old ')[1].split(' ')
mm.operation = [...mm.operation,...mon_items];
}
else if(first == 'Test:')
{
let mon_items = value.split('divisible by ')[1]
mm.test = mon_items;
}
else if(third == 'true:')
{
let mon_items = value.split('throw to monkey ')[1]
mm.trueThrow = mon_items;
}
else if(third == 'false:')
{
let mon_items = value.split('throw to monkey ')[1]
mm.falseThrow = mon_items;
m[mm.id] = mm;
}
})
//aa(m)
// parsing done,, now processing
for(let q=0;q<1;q++){
for(let y=0;y<Object.keys(m).length;y++)
{
let v = Object.keys(m)[y];
let lm = m[v];
let o1 = lm.operation[0];
let o2 = lm.operation[1];
let tt = parseInt(lm.test);
lm.items.forEach((v1)=>{
let op = o1 == '*' ? parseInt(v1) * (o2 == 'old' ? parseInt(v1) : parseInt(o2)) : parseInt(v1)+parseInt(o2);
op = Math.floor(op/3);
if(op % parseInt(tt)==0)
{
m[lm.trueThrow].items.push(op)
lm.items = lm.items.filter(x=>x != v1);
lm.count++;
}
else{
m[lm.falseThrow].items.push(op)
lm.items = lm.items.filter(x=>x!= v1);
lm.count++;
}
})
}
}
aa(m)
let t =[];
for(let y=0;y<Object.keys(m).length;y++)
{
let v = Object.keys(m)[y];
let lm = m[v];
t.push(lm.count)
}
aa(t)
t.sort((x,y)=> {return y-x});
let final =t[0]*t[1];
return final;
}
function aa(a)
{
console.log(a);
}
let input =`Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1`;
let input1 = `Monkey 0:
Starting items: 72, 64, 51, 57, 93, 97, 68
Operation: new = old * 19
Test: divisible by 17
If true: throw to monkey 4
If false: throw to monkey 7
Monkey 1:
Starting items: 62
Operation: new = old * 11
Test: divisible by 3
If true: throw to monkey 3
If false: throw to monkey 2
Monkey 2:
Starting items: 57, 94, 69, 79, 72
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 0
If false: throw to monkey 4
Monkey 3:
Starting items: 80, 64, 92, 93, 64, 56
Operation: new = old + 5
Test: divisible by 7
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 4:
Starting items: 70, 88, 95, 99, 78, 72, 65, 94
Operation: new = old + 7
Test: divisible by 2
If true: throw to monkey 7
If false: throw to monkey 5
Monkey 5:
Starting items: 57, 95, 81, 61
Operation: new = old * old
Test: divisible by 5
If true: throw to monkey 1
If false: throw to monkey 6
Monkey 6:
Starting items: 79, 99
Operation: new = old + 2
Test: divisible by 11
If true: throw to monkey 3
If false: throw to monkey 1
Monkey 7:
Starting items: 68, 98, 62
Operation: new = old + 3
Test: divisible by 13
If true: throw to monkey 5
If false: throw to monkey 6`;
console.log(calculateResult(input)); // 10605
Comments
Post a Comment