Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- def part_1(target, numbers):
- for num in numbers: # Find the 2 numbers that compliment to the target
- compliment = target - num
- if compliment in numbers:
- print("Part 1 Answer {} * {} = {}".format(num, compliment, num * compliment))
- break
- else:
- exit("refactor code. No matches found.")
- def part_2(target, numbers):
- for idx, _ in enumerate(numbers): # Find the 3 numbers that compliment to the target
- next_idx = idx + 1
- last_idx = len(numbers) - 1
- while last_idx >= next_idx:
- if numbers[idx] + numbers[next_idx] + numbers[last_idx] == target:
- print("Answer found")
- if numbers[idx] + numbers[next_idx] + numbers[last_idx] > target:
- last_idx-=1
- else:
- next_idx+=1
- if __name__ == '__main__':
- target = 2020
- numbers = []
- with open('input.txt', 'r') as f: # Read in the data as a list of ints
- numbers = [int(i) for i in f.read().splitlines()]
- part_1(target, numbers)
- part_2(target, numbers)
RAW Paste Data