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.

55 lines
1.6 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*35) # 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+1085
  37. k = k+3
  38. except:
  39. return newlist
  40. list_of_lists = split_into_list("day3input.txt")
  41. multiply_each_line(list_of_lists, "day3inputmultiplied.txt")
  42. list_of_lists = split_into_list("day3inputmultiplied.txt")
  43. list_as_string = "".join(list_of_lists)
  44. list_of_chars = string_to_list_of_chars(list_as_string)
  45. list_of_trees_opens = find_trees_and_opens(list_of_chars)
  46. list_of_slope_encounters = three_right_one_down(list_of_trees_opens)
  47. print(list_of_slope_encounters)
  48. print(list_of_slope_encounters.count("Tree"))