Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- linesFormatted = []
- with open("9 - input", 'r') as file:
- lines = file.readlines()
- for line in lines:
- linesFormatted.append(int(line.strip("\n")))
- start = 0
- answer = 0
- while start+25 <= len(linesFormatted):
- stop = start + 25
- target = linesFormatted[stop]
- valid = False
- for num1 in linesFormatted[start:stop]:
- for num2 in linesFormatted[start:stop]:
- if num1+num2 == target and num1 != num2:
- print(f"Sum exists for {target}")
- valid = True
- break
- if valid:
- break
- if not valid:
- print(f"Sum does not exist for {target}!")
- answer = target
- break
- start += 1
- # part 2
- distance = 2 # Start with a list with two elements
- while distance <= len(linesFormatted):
- start = 0
- while start+distance <= len(linesFormatted):
- stop = start + distance
- if sum(linesFormatted[start:stop]) == answer:
- smallest = min(linesFormatted[start:stop])
- largest = max(linesFormatted[start:stop])
- print(f"Smallest: {smallest}, Largest: {largest}, Sum: {smallest+largest}")
- break
- start += 1
- distance += 1 # Increase the length of the list to try sums in
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement