Advent of Code 2022
I've been really enjoying working on this year's Advent of Code. If you haven't heard of it, it's a series of coding puzzles, two a day for 25 days, from December 1st to December 25th every year. It only started a few days ago, so it's not too late to catch up. Or, if you're reading this later, you can always go back and try it out at your leisure. But, it is a lot of fun to wait until midnight each day to see what the next puzzle is.
What's cool about it, is that you can use any programming language you want. You just need to take an input file, run a calculation based on the puzzle instructions, and come up with an "answer", which is usually a number of some kind.
You can use your favourite language to try to come up with an answer as fast as possible, or you can use it as an opportunity to strengthen your skills in another language, even a language you've never used before and want to try out!
You need to log in with GitHub, Google, Reddit or Twitter, and then you can download an input file for each day. You'll need to read the input file in to your language of choice, and parse and process each line of the file.
If you're really fast, you can even get on the leaderboard. But doing that requires completing the puzzles in just a few minutes at midnight EST so that you're one of the first 100 people to do so. I'm definitely not fast enough to even bother trying!
So far, I've been using JavaScript with Node.js this year. My approach is to pipe in the input into my puzzle solution like this:
node solution.js < input.txt
To do this, I'm using an npm library called split that simply splits a stream into lines, to make it easier to work with. Here's a simple example that just counts the number of lines in a stream:
import split from 'split';
// keep some variables in the module scope to keep track of things
let count = 0;
// read the input file as a stream from stdin (Standard Input)
process.stdin
// pipe it to the split library, to split it into lines
.pipe(split())
// receive a callback for each line in the stream
.on('data', (line) => {
// do something with each line, in this case just counting
count++;
})
// receive a single callback when the file is done
.on('end', () => {
// do something at the end, eg. console.log the output
console.log(count);
});
If you're interested, there is an online community on Reddit where you can share your solution and join in the discussion to see what others have done each day.
I'll be sharing my progress on Mastodon at @[email protected], so you can follow me on there for updates and commentary.
I've also been pushing my solutions to Advent of Code up to GitHub, so feel free to see how I've approached it, if you're interested. But no cheating! 😉