Ga naar hoofdinhoud

Read a text file with Node JS to get input from Advent of Code problems

· 3 minuten leestijd
Mitchell Mudd

Thumbnail Advent of code is a coding challenge that happens every year in December. This is my first year taking part and I found it a bit clunky to copy and paste the input into a string within my JavaScript file.

The solution: use the Node File System module in synchronous mode 🙂 Using fs.readFileSync may not be best practice on normal project (you’re blocking the program from moving until the file is processed), but for advent of code it’s really nice to simply get your input.

You’ll need Node for the code below to work. I’ve linked tutorials for Mac and Linux on how to install Node. For Windows I’ve linked the download page of NodeJS.

Without further ado let’s read some text files! 🤓🗂

Installing Node

Mac

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-and-create-a-local-development-environment-on-macos

Ubuntu Linux

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04

Windows

https://nodejs.dev/en/download/

STEP 1: Create .txt file

Place the input.txt file into the directory you plan on working out of

Image displaying the text file input that I have

STEP 2: Import Node File System Module

Import the fs module from Node as shown using the Ecmascript Module Syntax

const fs = require('fs');

STEP 3: Create a function to read your text files

function readFile(filePath) {
try {
const data = fs.readFileSync(filePath);
return data.toString();
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}

STEP 4: Call your function

Call the function and store the input in a variable

const adventOfCodeInput = readFile('./day3/day3Input.txt');
// When working with this I HAD to had to specify the file path as shown above
// ./subdirectory/file where ./ is actually the very root of the directory

// FIRST 17 LINES OF OUTPUT
console.log(adventOfCodeInput)
`fzmmmfwDWFzlQQqjCQjDGnqq
tJHJvLJVVttNsvTtTvgHHSVwCsQRQQZCZZMqQMQBnqBMQs
rpFtPrzrMFZvZTLPpglhdssFshssJhsggG
pTtLtBMjZLfHvNWqcBHf
PZnSjnnsVfjfLLff
crvccpglrtHfNbzbHLzmGN
lptTLFFpdgvttFWltTclplDvhJMSMPhJPhChShPBZBMJhPMT
mPSPdnhznPdhSmPGchJdFDtBhghgFgWpFBQhTBMg
rHNNvbqHHHwZwMGMWtgtQMZQFQ
wRqqRffvvNHwrHqrNqLvCrqvmPnzdsGJsSsSdSsLPVVPnjsn
CzlngWpClJlzRJpDnpmzCndrhBcrhwcsBcLsNcsmLdQQ
qqSjqFGTFbPFSPTVjcBcBsDdQrwBVLrNcc
PtDDtZTvGDvFCMzZlRJCfWfJ
vtQDpvpvVvvcSFrrljZZsVjFrV
RqcTTRddRrlsjZrwrT
zmbdRdMmMgbPDcGhGmnDpcQn
gZqJRZRZdltFVGZQDZwV
`

Now you can solve your advent of code problem! 🙂

NOTE/WARNING

VsCode screenshot showing the file structure I'm using. Explains the difference between root and sub directories

Good luck with Advent of Code if you’re participating!

https://media.giphy.com/media/3o6ZtjqdcQfuno2WPK/giphy.gif

Final Code

// GET FILE INPUT
const fs = require('fs');

function readFile(filePath) {
try {
const data = fs.readFileSync(filePath);
return data.toString();
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}

const adventOfCodeInput = readFile('./day3/day3Input.txt');
// END OF GET FILE INPUT

Attributions

Tree Background Image: Photo by Prem Roshan on Unsplash

NodeJS Icon: Freebie Supply

Squiggly Line: Icon by 'Made by made' on The Noun Project