r/backtickbot • • Dec 01 '20

https://np.reddit.com/r/adventofcode/comments/k4e4lm/2020_day_1_solutions/ge9adsg/

Python

# Part 1
def find_pair(data, sum=2020):
  for datum in data:
    key = sum - datum
    if key in data:
      return (datum, key)
  return -1

# Part 2
def find_triplet(data):
  for datum in data:
    remainder = 2020 - datum
    pair = find_pair(data, remainder)
    if pair != -1 and pair[0] != pair[1] and datum not in pair:
      return (datum, pair[0], pair[1])
  return -1

I removed the main function which just read the input from a text file and called the above functions.

1 Upvotes

0 comments sorted by