Advent of Code

Advent of Code is an annual series of programming challenges, released every day from 1st December to 25th December.

General tips

Some general tips for solving the Advent of Code puzzles, regardless of language:

  1. Try your solution on the test input first, as this is small, so will complete quickly, and you already know the correct answer.
  2. Don’t overcomplicate solutions. Most puzzles can be solved with basic data structures such as linked lists.
  3. Don’t spend a long time trying to pick the ‘right’ data structure. The puzzle input is generally small enough that even an inefficient data structure will suffice, e.g. don’t try and work out whether a binary tree would be faster than a singly linked list when the execution time is only a matter of seconds anyway.
  4. Read the instructions carefully. It’s easy to accidentally write a condition such as ‘less than or equal to’ when the instructions require ‘less than’.

C tips

Some tips specifically relating to C:

  1. Don’t worry about memory management, e.g. ensuring every call to calloc or malloc is followed by free. The kernel will recover memory used by a process when the process terminates, and your solution is only likely to run for a few seconds.
  2. The way you solve problems in C may be different to the ‘obvious’ solution in other languages. You have to break the problem down into much smaller parts, instead of being able to say ‘read this file, sort the input, add up all the numbers’.

Bear in mind that solving the puzzles in C is likely to take longer than some other languages, simply because you often have to write more code, especially when it comes to processing data structures. For example, you can’t use the GLib singly linked list structure to store a list of integers, instead you have to store a list of pointers and then remember to cast them to/from integers.