Jan 22, 2019
For several years, I have been itching to dive into the sea of programming around Christmas but I never really got hooked. I’m fortunate enough that my family does not get all that crazy about home decorations or buying/exchanging presents. I had never played the Advent of Code(AoC) game before, but I do know that it is quite exhausting as one has to work through daily problems for 25 days in December. Nonetheless, I expected it to be a rewarding exercise. My solutions are available at my AoC 2018 github repo.
All puzzles follow the same format. Given some input, perform some kind of logic that arrive at a single number or string as the answer. The problem can stretch a programmer’s design/coding skills in various ways, using different kinds of data structures or having to write efficient code or smart logic to solve a large-scale problem.
Some puzzles are really fun. On Day 10, we start with a bunch of points of light in the sky and they are all moving in different direction at different speeds. After some time, the lights would align perfectly and you can see a message. I did the simulation and produced an animated PNG file using Plots.jl. After cropping away the unnecessary frames, you can see the message being formed (at frame 15):
Some puzzles look like computer games. On Day 13, you’re given a railway map where multiple carts run along the track and may turn left, go straight, or turn right at a junction with a predictable logic. The problem is to find out how long it takes all carts to crash into each other eventually. Solving that puzzle is a joy because you are essentially writing a game simulation. Here’s what a map looks like The > and v symbols indicates where the cart is going.
I didn’t expect to build an interpreter in a timed programming contest. On Day 16, you are provided a set of assembly-like instructions and you have to implement an interpreter to run the instructions. The second part was even more intriguing. You basically have to figure out the mapping between operation code and machine code based on the program’s output. To get a taste of the instruction set, take a look at my Julia implementation:
ops = [
(:addr, (x, a, b, c) -> x[c+1] = x[a+1] + x[b+1]),
(:addi, (x, a, i, c) -> x[c+1] = x[a+1] + i),
(:mulr, (x, a, b, c) -> x[c+1] = x[a+1] * x[b+1]),
(:muli, (x, a, i, c) -> x[c+1] = x[a+1] * i),
(:banr, (x, a, b, c) -> x[c+1] = x[a+1] & x[b+1]),
(:bani, (x, a, i, c) -> x[c+1] = x[a+1] & i),
(:borr, (x, a, b, c) -> x[c+1] = x[a+1] | x[b+1]),
(:bori, (x, a, i, c) -> x[c+1] = x[a+1] | i),
(:setr, (x, a, z, c) -> x[c+1] = x[a+1]),
(:seti, (x, i, z, c) -> x[c+1] = i),
(:gtir, (x, i, b, c) -> x[c+1] = i > x[b+1] ? 1 : 0),
(:gtri, (x, a, i, c) -> x[c+1] = x[a+1] > i ? 1 : 0),
(:gtrr, (x, a, b, c) -> x[c+1] = x[a+1] > x[b+1] ? 1 : 0),
(:eqir, (x, i, b, c) -> x[c+1] = i == x[b+1] ? 1 : 0),
(:eqri, (x, a, i, c) -> x[c+1] = x[a+1] == i ? 1 : 0),
(:eqrr, (x, a, b, c) -> x[c+1] = x[a+1] == x[b+1] ? 1 : 0)
]
The puzzles seemed to get harder each day. On Day 17, you have a 2-D map of clay buckets in various positions. The puzzle asks you to simulate pouring water from the top. When a bucket is full, the water leaks to the ones below. The buckets are of different shapes and sizes, thus the simulation logic gets a bit hairy. To date, my code is still buggy and I haven’t come to the correct solution yet :-}
By Day 20, I was exhausted… Every day, I had been spending over 1-3 hours working on the puzzles and only have few hours of sleep. In addition, I had a vacation planned before Christmas and so I had to stop playing. I hope I will have more time next year.
I really appreciate my family’s understanding when I was not able to spend much time with them before Christmas. At least, I was able to get some help from my daughter one day when I figured out what do while explaining the puzzle to her. Thank you, Karen.