Skip to main content

Advent of code - day 11 part 2 - javascript solution

 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 test_multiply = 1;

    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;

            test_multiply *= parseInt(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<10000;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 = op % test_multiply;

                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 input1 =`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 input = `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

Popular posts from this blog

Export Doc,Access,Image,CSV,Excel,Pdf,XML,HTML,Text,Print of Gridview in Asp.net

First import itextsharp.dll in the solution and use three namespces (basically for pdf) using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser;          Then Use the following code :- on each button  click  protected void Page_Load(object sender, EventArgs e)         {             if (!Page.IsPostBack)             {                 BindGridDetails(GridView1);             }         }                 protected DataTable BindGridDetails(GridView GridView1)         {             DataTable dt = new DataTable();             dt.Columns.Add("Student_ID", typeof(Int32));             dt.Columns.Add("Student_Name", typeof(string));             dt.Columns.Add("Education", typeof(string));             dt.Columns.Add("City", typeof(string));             DataRow dtrow = dt.NewRow();             dtrow["Student_ID"] = 1;             dtrow["Student_Name"] = "Musakkhir";   

Ip Sec Internet Security Basic Structure

The IPsec suite is an  open standard . IPsec uses the following  protocols  to perform various functions: Authentication Headers (AH)  provide connectionless  integrity  and data origin  authentication  for IP  datagrams  and provides protection against  replay attacks . Encapsulating Security Payloads (ESP)  provide  confidentiality , data-origin  authentication , connectionless  integrity , an anti-replay service (a form of partial sequence integrity), and limited traffic-flow confidentiality. Security Associations (SA)  provide the bundle of algorithms and data that provide the parameters necessary to AH and/or ESP operations. The  Internet Security Association and Key Management Protocol  (ISAKMP) provides a framework for authentication and key exchange, with actual authenticated keying material provided either by manual configuration with pre-shared keys,  Internet Key Exchange  (IKE and IKEv2),  Kerberized Internet Negotiation of Keys  (KINK), or IPSECKEY  DNS records .

Advent of code 2022 day 22 part 1

  function main(input, input1) {     let grid = input.split( '\n' );     grid.shift();     // only in big input     for ( let i = 0 ; i < 100 ; i++) {         grid[i] = '                                                  ' + grid[i];     }     //console.log(grid[100])     grid = grid.map(x => x.split( '' ))     // find first left top allowed     let temppos = - 1 ;     grid[ 0 ].forEach((element, i) => {         if (temppos == - 1 && element == '.' ) {             temppos = i;         }     });     let initpos = new pos(temppos, 0 )     // console.log(initpos)     // make grid equal shape     let maxlength = 0 ;     grid.forEach(el => {         if (el.length > maxlength) {             maxlength = el.length         }     })     console.log(maxlength)     grid.forEach(el => {         if (el.length < maxlength) {             let diff = maxlength - el.length;             while (diff > 0 ) {                 di