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

show image in asp.net form from database

In order to show image from database to webform first add an image control in webpage.   <asp:Image ID="imgStudentImage" runat="server" Height="157px" Width="160px" /> Then add new webform to read image from database for example displayimage.aspx and in code behind model write the following code. public string str = //your string     public SqlConnection conn;     public SqlCommand sql = new SqlCommand();     public SqlDataReader dr;     public string strSQL;     protected void Page_Load(object sender, EventArgs e)     {         conn = new SqlConnection(str);         conn.Open();         long id = Convert.ToInt64(Request.QueryString["mId"]);         strSQL = "";         strSQL = strSQL + " SELECT snap from S...

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)         {   ...

Snake Xenzia

Hello friends did you remember the GAME you play on nokia 1100 , SNAKE XENZIA .Yeah the same you are thinking ,a snake eating food and go longer and longer till it dies. Why to play it on mobile when it is available on your pc ??? It is made in .net framework 4.0 .Just install the setup and play SNAKE XENZIA . Have Fun !! DOWNLOAD IT   HERE  . Related articles Rattlesnake Dreams "Classsic Snake Game In C"? Resident Evil 6 gamescom demo takes in a giant snake battle Snakes & ladders