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.

104 lines
2.9 KiB

  1. def split_into_list(file_path):
  2. f = open(file_path, "r")
  3. list_of_lists = []
  4. for line in f:
  5. stripped_line = line.strip()
  6. stripped_line = str(stripped_line)
  7. line_list = stripped_line.split()
  8. list_of_lists.append(stripped_line)
  9. f.close()
  10. return(list_of_lists)
  11. def multiply_each_line(list_of_lists, filepath2):
  12. f = open(filepath2, "w")
  13. for i in list_of_lists:
  14. f.write(i*75) # Just an arbitrary number so I could make sure it wouldn't hit the wall
  15. #f.write("\n")
  16. f.close()
  17. def string_to_list_of_chars(string):
  18. return [char for char in string]
  19. def find_trees_and_opens(list_of_chars):
  20. newlist = []
  21. for i in list_of_chars:
  22. if i == "#":
  23. newlist.append("Tree")
  24. elif i == ".":
  25. newlist.append("Open")
  26. else:
  27. print("An error occured. Sorry.")
  28. quit()
  29. return newlist
  30. def three_right_one_down(list_of_trees_opens):
  31. newlist = []
  32. k = 0
  33. for i in list_of_trees_opens:
  34. try:
  35. newlist.append(list_of_trees_opens[k])
  36. k = k+2325
  37. k = k+3
  38. except:
  39. return newlist
  40. def one_right_one_down(list_of_trees_opens):
  41. newlist = []
  42. k = 0
  43. for i in list_of_trees_opens:
  44. try:
  45. newlist.append(list_of_trees_opens[k])
  46. k = k+2325
  47. k = k+1
  48. except:
  49. return newlist
  50. def five_right_one_down(list_of_trees_opens):
  51. newlist = []
  52. k = 0
  53. for i in list_of_trees_opens:
  54. try:
  55. newlist.append(list_of_trees_opens[k])
  56. k = k+2325
  57. k = k+5
  58. except:
  59. return newlist
  60. def seven_right_one_down(list_of_trees_opens):
  61. newlist = []
  62. k = 0
  63. for i in list_of_trees_opens:
  64. try:
  65. newlist.append(list_of_trees_opens[k])
  66. k = k+2325
  67. k = k+7
  68. except:
  69. return newlist
  70. def one_right_two_down(list_of_trees_opens):
  71. newlist = []
  72. k = 0
  73. for i in list_of_trees_opens:
  74. try:
  75. newlist.append(list_of_trees_opens[k])
  76. k = k+4650
  77. k = k+1
  78. except:
  79. return newlist
  80. list_of_lists = split_into_list("day3input.txt")
  81. multiply_each_line(list_of_lists, "day3inputmultipliedpart2.txt")
  82. list_of_lists = split_into_list("day3inputmultipliedpart2.txt")
  83. list_as_string = "".join(list_of_lists)
  84. list_of_chars = string_to_list_of_chars(list_as_string)
  85. list_of_trees_opens = find_trees_and_opens(list_of_chars)
  86. three_one = three_right_one_down(list_of_trees_opens)
  87. one_one = one_right_one_down(list_of_trees_opens)
  88. five_one = five_right_one_down(list_of_trees_opens)
  89. seven_one = seven_right_one_down(list_of_trees_opens)
  90. one_two = one_right_two_down(list_of_trees_opens)
  91. mult_trees = three_one.count("Tree") * one_one.count("Tree") * five_one.count("Tree") * seven_one.count("Tree") * one_two.count("Tree")
  92. print(one_two)
  93. print(mult_trees)