Posted on • Edited on • Originally published atfossheim.io
Advent of Code 2019: Day 2 solutions in Python
Originally posted onfossheim.io.
Earlier we solved thefirst day of the Advent of Code 2019 challenge. Now, let's take a look at how to solve the second puzzle.
The problem is pretty straight-forward. We get a list of numbers, that we split up in groups of four. In a sequence of four, the first number refers to which operation we have to perform.
- 1 = addition
- 2 = multiplication
- 99 = substraction
The next two numbers give the position in the array of the two numbers we have to add or multiply with eachother. The fourht number gives the position where the result of the addition or multiplication will be assigned to.
Part 1
We will have to go through the list in blocks of four. There are different ways of doing this, but for this example we'll use a for-loop:
forindexinrange(0,len(arr),4):# Do the calculations here
This will loop through the entire array, increasing the index by 4 with each loop.
Now, we can easily extract the next pieces of information. The operator is always on the first position of the sequence, and the two next items will tell usthe position in the array of the two numbers we'll use.
operator=arr[index]numberA=arr[arr[index+1]]numberB=arr[arr[index+2]]
Now we can use an if-elif statement to perform the correct calculations, and assign the output to the correct position in the array.
ifoperator==99:returnarr[0]elifoperator==1:arr[arr[index+3]]=numberA+numberBelifoperator==2:arr[arr[index+3]]=numberA*numberB
If we run this on the examples provided in the puzzle, it should give us the right answers.
However, we're not completely done with part 1 yet. The puzzle also mentions that we have toreplace position 1 with the value 12 and replace position 2 with the value 2
before running the program. If we include this before our for-loop, our program is finished and will work for the provided input sequence:
arr[1]=12arr[2]=2forindexinrange(0,len(arr),4):operator=arr[index]numberA=arr[arr[index+1]]numberB=arr[arr[index+2]]ifoperator==99:returnarr[0]elifoperator==1:arr[arr[index+3]]=numberA+numberBelifoperator==2:arr[arr[index+3]]=numberA*numberBprint("SOLUTION:",arr[0])
See the full solution on GitHub
Part 2
In part 2 we replace the first and second position of the array with two variables between 0 and 99: a noun (forarr[1]
) and a verb (forarr[2]
). Our goal is to find the noun and verb combination that would output an array with19690720
on the first position, and then put them in a simple formula:100 * noun + verb
.
We can largely re-use the same code as in part 2, and wrap it inside two for-loops to find the noun-verb combination.
To make things more readable, I also moved the code from part 1 in a function:
defprocess_array(input_arr):arr=input_arr[:]# Part 1 for-loopreturnarr[0]
I also made a shallow copy of the input array. T*his is because we'll be looping over the array a lot (up to99*99
times), and we want to start each iteration with our original input array, not the modified ones.
The rest is pretty straight-forward. We'll nest two loops, one for the noun and the other one for the verb, assign the noun toarr[1]
and the verb toarr[2]
and then call ourprocess_array
function.
fornouninrange(100):forverbinrange(100):input_arr[1]=nouninput_arr[2]=verboutput=process_array(input_arr)
Then the only thing that's left to do is stop or loop if the output equals19690720
, and put them inside of our formula.
fornouninrange(100):forverbinrange(100):input_arr[1]=nouninput_arr[2]=verboutput=process_array(input_arr)ifoutput==19690720:print(100*noun+verb)break
It's also possible to pass the noun and verb into the processing function, so it can easily be reused for both parts of the exercise.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse