-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchutes-and-ladders.js
48 lines (43 loc) · 1.03 KB
/
chutes-and-ladders.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const
Solver = require('./Solver'),
{createInterface: lines} = require('readline'),
{PerformanceObserver: Observer, performance: marker} = require('perf_hooks');
const obs = new Observer((items) => {
const item = items.getEntries()[0];
console.log(item.name, '=', item.duration);
marker.clearMarks();
});
let
unsolvable = 0,
rollsSum = 0,
solver = null,
warps = 0;
obs.observe({entryTypes: ['measure']});
marker.mark('Start');
lines(process.stdin).on('line', line => {
if (!solver) {
const [width, height, w] = line.split(' ');
if (w > 0) {
warps = w;
solver = new Solver(width, height);
}
}
else {
const [type, bottom, top] = line.split(' ');
solver.addWarp(bottom, top, type === 'C');
if (!--warps) {
const rolls = solver.solve();
solver = null;
if (~rolls)
rollsSum += rolls;
else
++unsolvable;
}
}
})
.on('close', () => {
marker.mark('End');
marker.measure('Time elapsed', 'Start', 'End');
console.log(`Unsolvable boards = ${unsolvable}\nSum of rolls = ${rollsSum}`);
});