My solutions for Advent of Code 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  1. import time
  2. def split_into_list(file_path):
  3. f = open(file_path, "r")
  4. list_of_lists = []
  5. for line in f:
  6. stripped_line = line.strip()
  7. stripped_line = int(float(stripped_line))
  8. #line_list = stripped_line.split()
  9. list_of_lists.append(stripped_line)
  10. f.close()
  11. return(list_of_lists)
  12. def add_to_each(list_of_terms, num1, num2):
  13. j = 0
  14. for i in list_of_terms:
  15. #print(i)
  16. try:
  17. k = j+1
  18. #print(k)
  19. l = list_of_terms[k]
  20. #print(l)
  21. j=j+1
  22. total = 0
  23. total += list_of_terms[num1]+list_of_terms[num2]+l
  24. #print(list_of_terms[num1])
  25. #print(list_of_terms[num2])
  26. #print(l)
  27. print(total)
  28. if (total) == 2020:
  29. print("equal")
  30. newlist = []
  31. newlist.append(list_of_terms[num1])
  32. newlist.append(list_of_terms[num2])
  33. newlist.append(l)
  34. return newlist
  35. else:
  36. print("not equal")
  37. except IndexError:
  38. print("done")
  39. def iterate_list(list_of_terms):
  40. for i in range(len(list_of_terms)):
  41. for j in range(len(list_of_terms)):
  42. newlist = add_to_each(list_of_terms, i, j)
  43. print(newlist)
  44. if newlist != None:
  45. return newlist
  46. else:
  47. continue
  48. list_of_terms = split_into_list("day1input.txt")
  49. answer_nums = iterate_list(list_of_terms)
  50. print(answer_nums)
  51. final_answer = (answer_nums[0]*answer_nums[1]*answer_nums[2])
  52. print(final_answer)