|
| 1 | +# --- Day 2: Corruption Checksum --- |
| 2 | +# |
| 3 | +# As you walk through the door, a glowing humanoid shape yells in your direction. |
| 4 | +# "You there! Your state appears to be idle. Come help us repair the corruption |
| 5 | +# in this spreadsheet - if we take another millisecond, we'll have to display an |
| 6 | +# hourglass cursor!" |
| 7 | +# |
| 8 | +# The spreadsheet consists of rows of apparently-random numbers. To make sure |
| 9 | +# the recovery process is on the right track, they need you to calculate the |
| 10 | +# spreadsheet's checksum. For each row, determine the difference between the |
| 11 | +# largest value and the smallest value; the checksum is the sum of all of these |
| 12 | +# differences. |
| 13 | +# |
| 14 | +# For example, given the following spreadsheet: |
| 15 | +# |
| 16 | +# 5 1 9 5 |
| 17 | +# 7 5 3 |
| 18 | +# 2 4 6 8 |
| 19 | +# The first row's largest and smallest values are 9 and 1, and their difference is 8. |
| 20 | +# The second row's largest and smallest values are 7 and 3, and their difference is 4. |
| 21 | +# The third row's difference is 6. |
| 22 | +# In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18. |
| 23 | +# |
| 24 | +# What is the checksum for the spreadsheet in your puzzle input? |
| 25 | +# Answer: 41887 |
| 26 | +# |
| 27 | +# --- Part Two --- |
| 28 | +# |
| 29 | +# "Great work; looks like we're on the right track after all. |
| 30 | +# Here's a star for your effort." However, the program seems a little worried. |
| 31 | +# Can programs be worried? |
| 32 | +# |
| 33 | +# "Based on what we're seeing, it looks like all the User wanted is some |
| 34 | +# information about the evenly divisible values in the spreadsheet. |
| 35 | +# Unfortunately, none of us are equipped for that kind of calculation - |
| 36 | +# most of us specialize in bitwise operations." |
| 37 | +# |
| 38 | +# It sounds like the goal is to find the only two numbers in each row where one |
| 39 | +# evenly divides the other - that is, where the result of the division |
| 40 | +# operation is a whole number. They would like you to find those numbers on |
| 41 | +# each line, divide them, and add up each line's result. |
| 42 | +# |
| 43 | +# For example, given the following spreadsheet: |
| 44 | +# |
| 45 | +# 5 9 2 8 |
| 46 | +# 9 4 7 3 |
| 47 | +# 3 8 6 5 |
| 48 | +# In the first row, the only two numbers that evenly divide are 8 and 2; |
| 49 | +# the result of this division is 4. |
| 50 | +# In the second row, the two numbers are 9 and 3; the result is 3. |
| 51 | +# In the third row, the result is 2. |
| 52 | +# In this example, the sum of the results would be 4 + 3 + 2 = 9. |
| 53 | + |
| 54 | +# What is the sum of each row's result in your puzzle input? |
| 55 | +# Answer: 226 |
| 56 | +# |
| 57 | +# ------------------------------------------------------------------------------ |
| 58 | + |
| 59 | +import os |
| 60 | + |
| 61 | +def checksum1(lines): |
| 62 | + return sum([min_max_diff(line) for line in lines]) |
| 63 | + |
| 64 | +def min_max_diff(line): |
| 65 | + return max(line) - min(line) |
| 66 | + |
| 67 | +def checksum2(lines): |
| 68 | + return sum([mod(line) for line in lines]) |
| 69 | + |
| 70 | +def mod(line): |
| 71 | + for n in line: |
| 72 | + l2 = line.copy() |
| 73 | + l2.remove(n) |
| 74 | + for n2 in l2: |
| 75 | + if n % n2 == 0: |
| 76 | + return int(n / n2) |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "inputs/day02.txt") |
| 80 | + f = open(file_path) |
| 81 | + data = f.read().split('\n') |
| 82 | + data = [[int(e) for e in line.split()] for line in data] |
| 83 | + print(checksum1(data)) |
| 84 | + print(checksum2(data)) |
0 commit comments