Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { PassThrough } = require('stream')
- const fs = require('fs')
- const NUMBER_BASE = 512
- const NUMBER_BASE_MAX = 511
- const NUMBER_BASE_POW = 9
- const MAX_NUM_LENGTH = 4
- class Week3Task1 {
- run () {
- return EdxIo.create()
- .then((io) => { this.io = io })
- .then(this.buildArray.bind(this))
- .then(this.digitSort.bind(this))
- .then(this.writeOutput.bind(this))
- .then(() => this.io.close())
- }
- countSort (bufferArr, counters, digit) {
- counters.fill(0)
- const itemShift = NUMBER_BASE_POW * digit
- for (let i = 0; i < this.arr.length; i++) {
- counters[(this.arr[i] >> itemShift) & NUMBER_BASE_MAX]++
- }
- let prevCounters = counters[0]
- for (let i = 1; i < NUMBER_BASE; i++) {
- if (counters[i] > 0) {
- counters[i] += prevCounters
- prevCounters = counters[i]
- }
- }
- for (let i = this.arr.length - 1; i >= 0; i--) {
- const value = this.arr[i]
- bufferArr[--counters[(value >> itemShift) & NUMBER_BASE_MAX]] = value
- }
- }
- digitSort () {
- let bufferArr = new Array(this.arr.length)
- const counters = new Array(NUMBER_BASE)
- for (let digit = 0; digit < MAX_NUM_LENGTH; digit++) {
- this.countSort(bufferArr, counters, digit)
- const arrTmp = this.arr
- this.arr = bufferArr
- bufferArr = arrTmp
- }
- }
- writeOutput () {
- let tenthSum = 0
- for (let i = 0; i < this.arr.length; i += 10) {
- tenthSum += this.arr[i]
- }
- this.io.write(tenthSum)
- }
- buildArray () {
- const firstLength = this.io.nextInt()
- const secondLength = this.io.nextInt()
- const firstArr = Array(firstLength)
- for (let i = 0; i < firstLength; i++) {
- firstArr[i] = this.io.nextInt()
- }
- this.arr = Array(firstLength * secondLength)
- let offset = 0
- let value
- for (let j = 0; j < secondLength; j++) {
- value = this.io.nextInt()
- for (let i = 0; i < firstLength; i++) {
- this.arr[i + offset] = firstArr[i] * value
- }
- offset += firstLength
- }
- }
- }
- // ---------------------------------- EdxIO ----------------------------------
- class EdxIo {
- constructor() {
- this._writeStream = new PassThrough()
- this._fsWriteStream = fs.createWriteStream('./output.txt')
- this._writeStream.pipe(this._fsWriteStream)
- this._readBytes = Array(256)
- this._readStream = fs.createReadStream('./input.txt')
- this._isReadable = new Promise((resolve) => {
- this._readStream.once('readable', () => resolve(this))
- })
- this.BYTE_SPACE = 32
- this.BYTE_RETURN = 13
- this.BYTE_NEWLINE = 10
- }
- write(value = '') {
- this._writeStream.push(value.toString())
- }
- writeLn(value = '') {
- this._writeStream.push(value.toString() + '\n')
- }
- nextInt() {
- return parseInt(this.nextToken(), 10)
- }
- nextFloat() {
- return parseFloat(this.nextToken())
- }
- nextToken() {
- let byteBuffer = this._getNextByte()
- let added = 0
- while (byteBuffer !== null) {
- switch (byteBuffer[0]) {
- case this.BYTE_NEWLINE:
- case this.BYTE_RETURN:
- case this.BYTE_SPACE:
- if (added > 0) {
- byteBuffer = null
- continue
- }
- break
- default:
- this._readBytes[added++] = byteBuffer[0]
- }
- byteBuffer = this._getNextByte()
- }
- return (added > 0)
- ? String.fromCharCode.apply(String, this._readBytes.slice(0, added))
- : undefined
- }
- nextChar() {
- let byteBuffer = this._getNextByte()
- while (byteBuffer !== null) {
- switch (byteBuffer[0]) {
- case this.BYTE_NEWLINE:
- case this.BYTE_RETURN:
- case this.BYTE_SPACE:
- break
- default:
- return String.fromCharCode(byteBuffer[0])
- }
- byteBuffer = this._getNextByte()
- }
- }
- close() {
- this._readStream.close()
- this._writeStream.push(null)
- this._fsWriteStream.close()
- return Promise.all([
- new Promise(resolve => this._readStream.on('close', resolve)),
- new Promise(resolve => this._fsWriteStream.on('close', resolve))
- ])
- }
- _getNextByte() {
- return this._readStream.read(1)
- }
- }
- EdxIo.create = () => new EdxIo()._isReadable
- // --------------------------------- / EdxIO ---------------------------------
- new Week3Task1().run().catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement