Advent of Code is an annual series of programming challenges, released every day from 1st December to 25th December.
Some general tips for solving the Advent of Code puzzles, regardless of language:
- Try your solution on the test input first, as this is small, so will complete quickly, and you already know the correct answer.
- Don’t overcomplicate solutions. Most puzzles can be solved with basic data structures such as linked lists.
- 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.
- 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’.
Some tips specifically relating to C:
- Don’t worry about memory management, e.g. ensuring every call to
mallocis 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.
- 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.