Solution to problem 2
This commit is contained in:
parent
7c7ad5ba62
commit
53c9e39071
96
src/Year_2017/P2.py
Normal file
96
src/Year_2017/P2.py
Normal file
@ -0,0 +1,96 @@
|
||||
# --- Day 2: Corruption Checksum ---
|
||||
|
||||
# As you walk through the door, a glowing humanoid shape yells in your
|
||||
# direction. "You there! Your state appears to be idle. Come help us repair the
|
||||
# corruption in this spreadsheet - if we take another millisecond, we'll have
|
||||
# to display an hourglass cursor!"
|
||||
|
||||
# The spreadsheet consists of rows of apparently-random numbers. To make sure
|
||||
# the recovery process is on the right track, they need you to calculate the
|
||||
# spreadsheet's 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.
|
||||
|
||||
# For example, given the following spreadsheet:
|
||||
|
||||
# 5 1 9 5
|
||||
# 7 5 3
|
||||
# 2 4 6 8
|
||||
|
||||
# The first row's largest and smallest values are 9 and 1, and their
|
||||
# difference is 8.
|
||||
# The second row's largest and smallest values are 7 and 3, and their
|
||||
# difference is 4.
|
||||
# The third row's difference is 6.
|
||||
|
||||
# In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
|
||||
|
||||
# What is the checksum for the spreadsheet in your puzzle input?
|
||||
|
||||
from itertools import combinations
|
||||
|
||||
with open("files/P2.txt") as f:
|
||||
str_spreadsheet: list[list[str]] = [
|
||||
line.split("\t") for line in f.read().strip().split("\n")
|
||||
]
|
||||
int_spreadsheet: list[list[int]] = [
|
||||
[int(num) for num in row] for row in str_spreadsheet
|
||||
]
|
||||
|
||||
|
||||
def part_1() -> None:
|
||||
print(
|
||||
f"Checksum is {sum(max(line) - min(line) for line in int_spreadsheet)}"
|
||||
)
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# "Great work; looks like we're on the right track after all. Here's a star for
|
||||
# your effort." However, the program seems a little worried. Can programs be
|
||||
# worried?
|
||||
|
||||
# "Based on what we're seeing, it looks like all the User wanted is some
|
||||
# information about the evenly divisible values in the spreadsheet.
|
||||
# Unfortunately, none of us are equipped for that kind of calculation - most of
|
||||
# us specialize in bitwise operations."
|
||||
|
||||
# It sounds like 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.
|
||||
|
||||
# For example, given the following spreadsheet:
|
||||
|
||||
# 5 9 2 8
|
||||
# 9 4 7 3
|
||||
# 3 8 6 5
|
||||
|
||||
# In the first row, the only two numbers that evenly divide are 8 and 2;
|
||||
# the result of this division is 4.
|
||||
# In the second row, the two numbers are 9 and 3; the result is 3.
|
||||
# In the third row, the result is 2.
|
||||
|
||||
# In this example, the sum of the results would be 4 + 3 + 2 = 9.
|
||||
|
||||
# What is the sum of each row's result in your puzzle input?
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
result = []
|
||||
for row in int_spreadsheet:
|
||||
pairs = combinations(row, 2)
|
||||
for el1, el2 in pairs:
|
||||
if el1 % el2 == 0:
|
||||
result.append(int(el1 / el2))
|
||||
break
|
||||
elif el2 % el1 == 0:
|
||||
result.append(int(el2 / el1))
|
||||
break
|
||||
|
||||
print(f"Sum of each row's result is {sum(result)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
Loading…
Reference in New Issue
Block a user