day 9
this one was a LOT nicer for my mathematical brain lol - 9b.py solves both problems, so i'm gonna talk about the code in that
starts off with the usual reading the file in and setting stuff up (each line is a number, but splitting gives them as strings, so i used list comprehension to make these into ints; tf is the list of the previous twenty-five things but it also reflects my current mood):
with open('9.txt', 'r') as file: input = file.read() # turn the input into a list, one element is one number as a string input_list = list(input.split('\n')) # get a list of numbers instead of strings num_list = [int(n) for n in input_list] # keep a list of the previous 25 numbers tf = []
we then loop over n in num_list, and start by taking the case with the first 25 numbers, in which case we just keep adding these to the tf list and doing nothing else.
if we aren't in the preamble (there are at least 25 numbers before the current n), we have two extra layers of loops:
- loop over i where 0 <= i < 24
- loop over j where i < j <= 24
for each n, we look at the 25 numbers before it (zero-indexed, so these are from 0 to 24), and check if the i^th and j^th values in this list add up to n AND are not equal to each other.
if we find such an (i,j) then this value of n is valid, so we break the j-loop, which breaks the i-loop, and we get to the final two commands within the n-loop: removing the first value in tf and appending n to tf, so we have the 25 values before the value after n.
if we don't find such an (i,j), then this value of n is invalid (and as such is the answer to part a), so we print this and break the n-loop.
for n in num_list: # first 25 numbers case if len(tf) < 25: # in preamble tf.append(n) # add to list continue # go to next num # after first 25 numbers case # for int pairs (i,j) where 0 <= i < j < 25 for i in range(0,24): for j in range(i+1,25): if not tf[i] == tf[j]: # numbers must have distinct values if tf[i] + tf[j] == n: # must add up to n break # tf[i] =/= tf[j] and tf[i] + tf[j] = n else: # j loop did not break # there is no such j; go to next i continue # j loop broke # tf[i] =/= tf[j] and tf[i] + tf[j] = n break else: # i loop did not break # => j loop did not break # => invalid number print('invalid number (part a): ' + str(n)) # we have the answer break # n loop # if we get here, then we are past preamble and had a valid number # remove first number from tf tf.pop(0) # add latest number to tf tf.append(n)
now we want to get a list of at least 2 consecutive values that add up to this n - i checked through the list and the minimum value was 1, so we don't need to worry about any negative numbers. as such, we can find this list by appending values one by one: if the sum of the list exceeds n, we remove values from the start until it doesn't; if the sum equals n, we print the "encryption weakness" (sum of the min and max values in the consecutive list) and then break; if the sum is less than n or the list has under 2 entries, we continue (and add the next value to the list)
for m in num_list: # add m to the list cs.append(m) # make sure the sum is <= n while sum(cs) > n: # too large; remove first number cs.pop(0) # check if list is at least len 2 if len(cs) > 1: if sum(cs) == n: # if it adds up to n print('encryption weakness (part b): ' + str(min(cs) + max(cs))) break # we have the answer else: # sum(cs) < n continue # need to append the next else: continue # only one so we need to append the next
Files
Get aoc 2020
aoc 2020
not a mod, just some code
Status | Released |
Category | Other |
Author | riv |
Tags | advent-of-code, advent-of-code-2020, python |
Leave a comment
Log in with itch.io to leave a comment.