function processData(input) { const [, [steps]] = input.split('\n').map( (line) => line.split(' ') ); let elevation = 0; let valleys = 0; for (const step of steps) { let prevElevation = elevation; elevation += (step == 'U' ? 1 : -1); if (elevation == 0 && prevElevation < 0) valleys++; } console.log(valleys); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });