3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「積み木の水槽」をjavascriptで解いてみた

Posted at

何日か前に出題されていたものをjsでも書いてみました。

みなさんの回答を、投稿してからみようと思うので
同じや、さらに良いロジックがあるかもしれませんが、とりあえず。

左右から見える壁の高さと奥行きを調査し、見えない部分に水が入っていると考えています

water.js
var test = function (input, output) {

    // 0が入っているところで分けて別々に計算して足す
    var blocks = input.split('0')
      , water = 0;

    // 分けたブロックごとに計算
    blocks.forEach(function (data) {
        var block = data.split('')
            // 左影
          , max1 = 0
          , lsharp = block.reduce(function (x, y) {
                        max1 = Math.max(y * 1, max1);
                        x.push(max1);
                        return x;
                    }, [])
            // 右影
          , max2 = 0
          , rsharp = block.reduceRight(function (x, y) {
                        max2 = Math.max(y * 1, max2);
                        x.unshift(max2);
                        return x;
                    }, []);

        // 左影右影の低い方と実際のブロックの差分を算出すると水の量
        for (var i = 0, len = block.length; i < len; i++) {
            water += Math.min(lsharp[i], rsharp[i]) - block[i];
        }
    });
    console.log(water == output);
};

/*0*/ test( "83141310145169154671122", "24" );
/*1*/ test( "923111128", "45" );
/*2*/ test( "923101128", "1" );
/*3*/ test( "903111128", "9" );
/*4*/ test( "3", "0" );
/*5*/ test( "31", "0" );
/*6*/ test( "412", "1" );
/*7*/ test( "3124", "3" );
/*8*/ test( "11111", "0" );
/*9*/ test( "222111", "0" );
/*10*/ test( "335544", "0" );
/*11*/ test( "1223455321", "0" );
/*12*/ test( "000", "0" );
/*13*/ test( "000100020003121", "1" );
/*14*/ test( "1213141516171819181716151413121", "56" );
/*15*/ test( "712131415161718191817161514131216", "117" );
/*16*/ test( "712131405161718191817161514031216", "64" );
/*17*/ test( "03205301204342100", "1" );
/*18*/ test( "0912830485711120342", "18" );
/*19*/ test( "1113241120998943327631001", "20" );
/*20*/ test( "7688167781598943035023813337019904732", "41" );
/*21*/ test( "2032075902729233234129146823006063388", "79" );
/*22*/ test( "8323636570846582397534533", "44" );
/*23*/ test( "2142555257761672319599209190604843", "41" );
/*24*/ test( "06424633785085474133925235", "51" );
/*25*/ test( "503144400846933212134", "21" );
/*26*/ test( "1204706243676306476295999864", "21" );
/*27*/ test( "050527640248767717738306306596466224", "29" );
/*28*/ test( "5926294098216193922825", "65" );
/*29*/ test( "655589141599534035", "29" );
/*30*/ test( "7411279689677738", "34" );
/*31*/ test( "268131111165754619136819109839402", "102" );

次は出題と同時にチャレンジしてみたいと思います。

3
3
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?