This year, I’m posting my solutions to the puzzles in the Advent of Code. Each post will consist of pseudocode, with a link to the actual code I used to solve the puzzle on Github.
And now, on with the solution:
Day 12, part 1
I wasn’t sure whether I’d be able to continue after Day 11. I hit a similar wall last year, although I think I may be able to solve some of the later puzzles from last year.
Simplified problem: Using instructions written in a similar style to Assembler, calculate the value of register a once the instructions have been completed.
There are 4 registers: a, b, c, and d. All registers are initialised with a value of 0. There are four instructions:
cpy x y
copiesx
(either an integer or the value of a register) into registery
.inc x
increases the value of registerx
by one.dec x
decreases the value of registerx
by one.jnz x y
jumps to an instructiony
away (positive means forward; negative means backward), but only ifx
is not zero.
After completing the instructions in input, what is the value in register a?
Solution
- Set Instructions=(your input)
- Set index=0 (this is the instruction index)
- While index < number of Instructions
- Set parts = instructions[index], split on the space character
- Set instruction=parts[0]
- If instruction=”cpy”
- If parts[1] is a register
- Set value=register value
- Else
- Set value=parts[1]
- End If
- Set register[parts[2]]=value
- If parts[1] is a register
- Else if instruction=”inc”
- register[parts[1]]=value+1
- Else if instruction=”dec”
- register[parts[1]]=value-1
- Else (instruction=”jnz”)
- If parts[1] is a register
- Set registervalue=registers[parts[1]]
- Else (value is a literal number)
- Set registervalue=parts[1]
- End If
- If registervalue is not equal to 0
- Set index=index+parts[2]
- End If
- If parts[1] is a register
- End If
- If instruction is not “jnz”
- Set index=index+1
- End If
- End While
- Return value of register[a]
Day 12, part 2
Using the same rules and input as in Part 1, what the value of register a if register c is initialised to 1?
Solution
Perform the same steps as in Part 1, setting register c to 1 instead of 0.
This process can take quite some time.
You can find the full solution on Github