The next step in the journey is to solve a checksum.
For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.
filepath='input.txt' output =  with open(filepath) as fp: for line in fp: out = list(map(int,line.split())) output.append(out) checksum=0 for i in range(len(output)): checksum += max(output[i])-min(output[i]) print(checksum)
First step is to get the input file imported in the correct format. For each line, I’ve split it and converted each term into an integer. I then append that list to the overall list.
After that, we loop through the terms, calculate difference between the max and the min of each row and add that difference to our checksum.
“the goal is to find the only two numbers in each row where one evenly divides the other – that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line’s result.”
div_sum = 0 for i in range(len(output)): for j in range(len(output[i])): for k in range(len(output[j])): if output[i][j]%output[i][k]==0 and j!=k: div_sum += output[i][j]/output[i][k] print(div_sum)
This process starts of the same but the process for each row is more complicated.
I want to loop through each term, check if it is divisible by every other term and then add together the dividends.
I used a comparison using mod for this and then added it together. I ended up with a bigger total than the final result. I realised that I was including the term divided by itself so I added in the j!=k term to make sure this division didn’t get added.
The main aspect of the challenge here was getting the data into the correct form. Python has great methods to be able to find everything else. I had to look up the map function to make sure I got the syntax right. Day 2 done!