paizaプログラミング問題をやってみた：Bランク

## はじめに

少し前から自己研鑽のためにLeetCodeという競技プログラミングの問題を解いています。
paiza×Qiita記事投稿キャンペーンを機にpaizaの問題を解いていこうと思い、コードを公開します。

前回はCランクの問題だったので、今回はBランクの問題です。
言語はJavaScript（TypeScriptがなかった...）を選択しています。

Dランクはこちら

https://qiita.com/tkmsk/items/146653302d1b75156238

Cランクはこちら

https://qiita.com/tkmsk/items/d8a1cbf58968db96e7fb

## 3Dプリンタ

```javascript
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const xyz = lines[0].split(' ');
  const X = Number(xyz[0]);
  const Y = Number(xyz[1]);
  const Z = Number(xyz[2]);
  const output = new Array(Z);
  
  for (let z=0; z<Z; z++) {
    let row = new Array(Y).fill('.');
    for (let x=0; x<X; x++) {
      const line = lines[x + z*(X+1) + 1];
      for (let y=0; y<Y; y++) {
        if (line.charAt(y) === '#') {
          row[y] = '#';
        }
      }
    }
    output[Z-z-1] = row.join('');
  }
  
  for (let z=0; z<Z; z++) {
    console.log(output[z]);
  }
});
```

## 神経衰弱

```javascript
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const hwn = lines[0].split(' ');
  const H = Number(hwn[0]);
  const W = Number(hwn[1]);
  const N = Number(hwn[2]);
  
  const table = new Array(H);
  for (let h=0; h<H; h++) {
    const line = lines[h+1].split(' ');
    const row = line.map((elem) => Number(elem));
    table[h] = row;
  }
  
  const L = Number(lines[H+1]);
  const points = new Array(N).fill(0);
  let player = 0;
  for (let l=0; l<L; l++) {
    const line = lines[l+H+2].split(' ').map((elem) => Number(elem));
    const c1 = table[line[0] - 1][line[1] - 1];
    const c2 = table[line[2] - 1][line[3] - 1];
    if (c1 === c2) {
      points[player] += 2;
    } else {
      player = (player + 1) % N;
    }
  }
  
  for (let n=0; n<N; n++) {
    console.log(points[n]);
  }
});
```

## みんなでしりとり

```javascript
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const nkm = lines[0].split(' ').map((elem) => Number(elem));
  const N = nkm[0];
  const K = nkm[1];
  const M = nkm[2];
  
  const words = new Array(K);
  for (let k=0; k<K; k++) {
    words[k] = lines[k + 1];
  }
  
  const players = new Array(N);
  for (let n=0; n<N; n++) {
    players[n] = n;
  }
  
  let beforeCommentEnd = 'z';
  for (let m=0; m<M; m++) {
    const player = players.shift();
    
    const comment = lines[m + K + 1];
    const commentEnd = comment.charAt(comment.length - 1);
    const commentIndex = words.indexOf(comment);
    if (commentIndex === -1) {
        beforeCommentEnd = 'z';
    } else {
      words.splice(commentIndex, 1);
      if (((beforeCommentEnd !== 'z') && (beforeCommentEnd !== comment.charAt(0)))
      || (commentEnd === 'z')) {
        beforeCommentEnd = 'z';
      } else {
        players.push(player);
        beforeCommentEnd = commentEnd;
      }
    }
  }
  
  console.log(players.length);
  players.sort((a, b) => a - b).forEach((player) => { console.log(player + 1); });
});
```

## 長テーブルのうなぎ屋

```javascript
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const nm = lines[0].split(' ').map((elem) => Number(elem));
  const N = nm[0];
  const M = nm[1];
  
  const seats = new Array(N).fill(0);
  for (let m=0; m<M; m++) {
    const group = lines[m + 1].split(' ').map((elem) => Number(elem));
    let sitted = 0;
    for (let b=0; b<group[0]; b++) {
      sitted += seats[(group[1] - 1 + b) % N];
    }
    
    if (sitted === 0) {
      for (let b=0; b<group[0]; b++) {
        seats[(group[1] - 1 + b) % N] = 1;
      }
    }
  }
  
  console.log(seats.reduce((acc, cur)=> acc + cur, 0));
});
```

## 名刺バインダー管理

```javascript
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const nm = lines[0].split(' ').map((elem) => Number(elem));
  const N = nm[0];
  const M = nm[1];
  
  const page = Math.floor((M-1) / N) + 1;
  const pocket = (M - 1) % N + 1;
  
  let number;
  if ((page % 2) === 1) {
    number = 2 * N * Math.floor((page+1)/2) - (pocket - 1);
  } else {
    number = M - 2 * (pocket-1) - 1;
  }
  
  console.log(number);
});
```

## さいごに

だんだんと複雑になってきました。
ここまでくると、言語毎に書きやすいところと書きにくいところが出てくる印象です。