Advertisement
Armandur

Untitled

Dec 9th, 2020
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. linesFormatted = []
  2. with open("9 - input", 'r') as file:
  3.     lines = file.readlines()
  4.     for line in lines:
  5.         linesFormatted.append(int(line.strip("\n")))
  6.  
  7. start = 0
  8. answer = 0
  9. while start+25 <= len(linesFormatted):
  10.     stop = start + 25
  11.     target = linesFormatted[stop]
  12.  
  13.     valid = False
  14.     for num1 in linesFormatted[start:stop]:
  15.         for num2 in linesFormatted[start:stop]:
  16.             if num1+num2 == target and num1 != num2:
  17.                 print(f"Sum exists for {target}")
  18.                 valid = True
  19.                 break
  20.         if valid:
  21.             break
  22.     if not valid:
  23.         print(f"Sum does not exist for {target}!")
  24.         answer = target
  25.         break
  26.     start += 1
  27.  
  28. # part 2
  29. distance = 2 #  Start with a list with two elements
  30. while distance <= len(linesFormatted):
  31.     start = 0
  32.  
  33.     while start+distance <= len(linesFormatted):
  34.         stop = start + distance
  35.         if sum(linesFormatted[start:stop]) == answer:
  36.             smallest = min(linesFormatted[start:stop])
  37.             largest = max(linesFormatted[start:stop])
  38.             print(f"Smallest: {smallest}, Largest: {largest}, Sum: {smallest+largest}")
  39.             break
  40.         start += 1
  41.     distance += 1   #   Increase the length of the list to try sums in
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement