bourb0n

Untitled

Dec 1st, 2020
639
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. def part_1(target, numbers):
  5.   for num in numbers: # Find the 2 numbers that compliment to the target
  6.     compliment = target - num
  7.     if compliment in numbers:
  8.       print("Part 1 Answer {} * {} = {}".format(num, compliment, num * compliment))
  9.       break
  10.   else:
  11.     exit("refactor code. No matches found.")
  12.  
  13.  
  14. def part_2(target, numbers):
  15.   for idx, _ in enumerate(numbers): # Find the 3 numbers that compliment to the target
  16.     next_idx = idx + 1
  17.     last_idx = len(numbers) - 1
  18.  
  19.     while last_idx >= next_idx:
  20.       if numbers[idx] + numbers[next_idx] + numbers[last_idx] == target:
  21.         print("Answer found")
  22.  
  23.       if numbers[idx] + numbers[next_idx] + numbers[last_idx] > target:
  24.         last_idx-=1
  25.       else:
  26.         next_idx+=1
  27.  
  28.  
  29. if __name__ == '__main__':
  30.   target = 2020
  31.  
  32.   numbers = []
  33.   with open('input.txt', 'r') as f: # Read in the data as a list of ints
  34.     numbers = [int(i) for i in f.read().splitlines()]
  35.  
  36.   part_1(target, numbers)
  37.   part_2(target, numbers)
  38.  
  39.  
RAW Paste Data