Source: Corruption Checksum
Part 1: The checksum of a list of numbers is the difference between the largest and smallest number in the row. What is the sum of checksums of a file containing many such lists?
print(sum(
max(*row) - min(*row)
for row in data
))
It’s not the most Pythonic thing ever (closer to a lisp), but I think it’s still fairly clean. I think the most interesting part is using the *
operator to send the entire list as parameters to max
and min
.
Part 2: Exactly two numbers in each list are evenly divisible by one another. The new checksum is the result of dividing the larger by the smaller. Find the sum of checksums.
This one looks similar, but instead of max
and min
, we’ll use %
to check for divisibility and //
to do integer division.
print(sum(
a // b
for row in data
for a in row
for b in row
if a != b and a % b == 0
))
Really fast too:
$ python3 run-all.py day-02
day-02 python3 check-it.py input.txt --part 1 0.06736087799072266 51139
day-02 python3 check-it.py input.txt --part 2 0.0611109733581543 272