Advent of Code 2017 Day 2
Where we find checksums for broken spreadsheets
Day 2 challenge description - http://adventofcode.com/2017/day/2
December 18th 2017
10:14
Reading through the puzzle description and this seems really easy. I checked my puzzle input and there are just 16 lines so it's not like there's some kind of optimization required here either.
- Just iterate through each line, and take the min and max for each line and check the difference between them and keep adding up those differences.
- Just in case this turns out to be a doozy, I'm going to track the min and max numbers for each line and store them in some kind of array. I feel like this entire puzzle is a trap into making me lower my guard. Oh well's time to code. Tests first!
- I feel like part of the ease from this also comes from the fact that there is such a thing as
min
andmax
built into almost every language. Not entirely sure how I might write this in Bash. Does Javascript come withmin
/max
?
10:39
So the code itself to get the checksum according to the specifications of the puzzle is trivial. Here it is:
def get_checksum(spreadsheet):
checksum = 0
for line in spreadsheet:
checksum += max(line) - min(line)
return checksum
- I admittedly didn't bother storing stuff as I mentioned I would above.
- While the code above is trivial, the actual reading of the file turned out to be more tedious since the file can be formatted in all kinds of strange ways. It's still not hard but I think I'm not doing it in a truly pythonic way.
def solve_challenge_part_1():
puzzle_input_file = open('python/day2part1input.txt', 'r')
puzzle_input = []
for line in puzzle_input_file.read().strip().split('\n'):
puzzle_input.append([int(number) for number in line.split(' ') if number != '' ])
puzzle_input_file.close()
print(get_checksum(puzzle_input))
- I could have combined the for loop into a single statement, but the readbility of it vanishes down the toilet then.
10:42
Solving Part 2
- Oh geez. This looks like it's going to kick me hard. Each line thankfully can contain only ONE pair of numbers that evenly divide each other. If it was an unspecified number, that would have been horrible.
- Either way, I'm still going to brute force it the only way I think I can.
- I'm going to sort each line, descending order, and then for each of the big numbers, test against all the other numbers in the array to see if it can be evenly divided.
- I'm going to guess that the array has no duplicate numbers. Because if it does, <insert this changes everything meme here>
11:06
That took longer than expected. All tests are passing. And once again, the code looks... Well... Not smart would be a good way to put it. But I can "read" the logic over here so I'm ok with it. This code is just for finding the pair of divisble values in a given row
. The code to iterate through each line and add the results together is trivial.
def find_evenly_divisible_values(spreadsheet_row):
sorted_row = spreadsheet_row.copy()
sorted_row.sort()
max_value_index = len(sorted_row) - 1
while max_value_index>0:
min_value_index = 0
while min_value_index < max_value_index:
if sorted_row[max_value_index] % sorted_row[min_value_index] == 0:
return (sorted_row[max_value_index], sorted_row[min_value_index])
min_value_index += 1
max_value_index -= 1
- This assumes that every line must have only one evenly divisible values. And I think it fits as per the description of the puzzle. Remember kids. Always read the specifications and know your assumptions.
11:15
And we are done!
➜ advent-of-code-2017 git:(master) ✗ python3 -m python.day2test -v
test_solution_returns_correct_checksum (__main__.TestDay2Part1Solution) ... ok
test_evenly_divisible_values_can_be_found (__main__.TestDay2Part2Solution) ... ok
test_final_sum_of_values_is_calculated_correctly (__main__.TestDay2Part2Solution) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
➜ advent-of-code-2017 git:(master) ✗ python3 -m python.day2
/usr/bin/python3: No module named python.day2
➜ advent-of-code-2017 git:(master) ✗ python3 -m python.day2solution
=== Part 1 ===
36766
=== Part 2 ===
261
Posted on December 18 2017 by Adnan Issadeen