NOTE: This is my second post discussing Advent of Code. Check out the first post to learn more about why I love AoC.
Advent of Code is only a day away! I can't wait to get started. If you're thinking about trying Advent of Code but you're not sure where to start, I hope I can share some tips.
First and foremost, make sure you've signed up at adventofcode.com. Each day, a new puzzle will become available at midnight. Now that you've signed up, let's talk about how to start writing some code to solve each puzzle.
It's often difficult to start on any programming project. The number of options and approaches can feel overwhelming. I definitely experience this sensation of overwhelm when starting a project. Over the years, I've learned strategies for overcoming some of the anxious thoughts that can creep in when just getting started with something. The strategy I come back to time after time is to start with the simplest approach that works.
This is the strategy that I recommend for jumping into Advent of Code. In this post, I'll demonstrate two simple templates that should make it fairly easy to get started solving Advent of Code problems.
There are other resources that suggest templates that will automatically download your input and submit your output. Or that will even show performance benchmarks for each of your solutions. These are great, but I think it's even more fun to start simple and introduce your own features as you get more comfortable using a basic setup.
If you want to check out a great example of a more advanced setup, Anthony Sottile posted a great video demonstrating his template for solving AoC quickly.
Python
I love solving the problems with python each year. For small puzzles like those in AoC, python gives you a lot of power with very little ceremony.
For a basic setup, I recommend using the fileinput
module from the standard library. This library takes care of all of the boilerplate of loading input files
and makes it easy to swap in a sample input if you want to debug your solution
on a simpler example.
Usually, I start with something like this:
import fileinput
lines = [line.strip() for line in fileinput.input()]
If I've saved my solution to a file named sol.py
and my input to a file named
input.txt
, then I can run my solution by running the command:
python3 -m sol input.txt
Let's see how it looks to solve day 1 part 1 from the 2021 edition of Advent of Code.
import fileinput
-lines = [line.strip() for line in fileinput.input()]
+depths = [int(line.strip()) for line in fileinput.input()]
+
+
+result = 0
+for i in range(len(depths) - 1):
+ if depths[i] < depths[i + 1]:
+ result += 1
+print("Part one:", result)
As you can see, most of the adjustments that we've made are focused on just solving the specifics of the problem. First we parse an integer from each line, then we are able to walk our list to figure out how many times the depth increases.
JavaScript
JavaScript is a great language for getting started learning to code especially because the web is such a ubiquitous platform. There are several excellent JavaScript runtimes that you can use to solve AoC problems in JavaScript. I recommend node.js as it is the most popular server-side JavaScript platform.
Unfortunately, node does not provide quite as much convenience as python (and its enormous standard library). But once you get past the boilerplate setup, it's not so bad!
Let's take a look:
const fs = require("fs");
const input = fs.readFileSync("./input.txt").toString();
const lines = input.split("\n").map((s) => s.trim());
To run this, assuming that I've saved this to sol.js
(and the input to input.txt
) I can run the command:
node sol.js
Note that we've hardcoded the path of the input file, so you'll have to do
a little bit more work to test out your solution on a sample input. It's possible
to recreate our setup from the python section with a little bit of research
on command line argument parsing (see the docs on process.argv
for more details).
Now let's see what it looks like to solve 2021 day 1 part 1 in JavaScript:
const fs = require("fs");
const input = fs.readFileSync("./input.txt").toString();
-const lines = input.split("\n").map((s) => s.trim());
+const depths = input.split("\n").map((s) => parseInt(s.trim()));
+
+let result = 0;
+for (let i = 0; i < depths.length - 1; ++i) {
+ if (depths[i] < depths[i + 1]) {
+ ++result;
+ }
+}
+console.log("Part one:", result);
Not too bad!
Wrap Up
As you can see, it's possible to get started solving Advent of Code puzzles with just a little bit of setup. Once you've solved your first few puzzles, it can be fun to experiment with different ways to speed up your solutions or to automatically handle fetching your input or submitting your output. But it's fine to start simple! Hopefully you'll join me tomorrow night (and the rest of the month) in solving some festive puzzles!
Good luck!