Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission: JavaScript (Single-Process) #2

Open
mykeels opened this issue Sep 12, 2018 · 0 comments
Open

Submission: JavaScript (Single-Process) #2

mykeels opened this issue Sep 12, 2018 · 0 comments

Comments

@mykeels
Copy link
Collaborator

mykeels commented Sep 12, 2018

  • Time ~5137ms
  • Hardware:
      Processor Name: Intel Core i5
      Processor Speed: 2.3 GHz
      Number of Processors: 1
      Total Number of Cores: 2
      L2 Cache (per Core): 256 KB
      L3 Cache: 4 MB
      Memory: 16 GB
  • Program:
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const basePath = '<path-to-files-directory>'

const getFiles = () => {
    return new Promise((resolve, reject) => {
        fs.readdir(basePath, (err, folders) => {
            if (err) {
                reject(err)
            }
            else {
                resolve(
                    Promise.all(folders.map(dir => {
                        const dirPath = path.join(basePath, dir)
                        return new Promise((resolve, reject) => {
                            fs.readdir(dirPath, (err, files) => {
                                if (err) {
                                    reject(err)
                                }
                                else {
                                    resolve(files.map(file => path.join(dirPath, file)))
                                }
                            })
                        })
                    })).then(files => files.reduce((a, b) => a.concat(b)))
                )
            }
        })
    })
}

console.time('total')
let total = 0
getFiles()
    .then(function* (files) {
        for (let file of files) {
            yield new Promise((resolve, reject) => {
                const lineReader = readline.createInterface({
                    input: fs.createReadStream(file)
                })
                
                let fileSum = 0
    
                lineReader.on('line', line => {
                    let lineSum = 0
                    let current = 0
                    for(let i = 0; i < line.length; i++) {
                        let char = line[i]
                        if (char != ',' ) {
                            if (current === 0) {
                                current = +char
                            }
                            else {
                                current = (current * 10) + (+char)
                            }
                        }
                        if ((char === ',') || (i === (line.length - 1))) {
                            lineSum += current
                            current = 0
                        }
                    }
                    fileSum += lineSum
                })
    
                lineReader.on('close', () => {
                    total += fileSum
                    resolve(fileSum)
                })
            })
        }
    })
    .then(sums => {
        return Promise.all(sums)
    })
    .then(() => {
        console.log(total)
        console.timeEnd('total')
    })
    .catch(err => {
        console.error(err)
    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant