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.

94 lines
2.8 KiB

  1. import re
  2. def file_to_string(file_path):
  3. f = open(file_path, "r")
  4. string = f.read()
  5. f.close()
  6. return string
  7. def split_on_empty_lines(string):
  8. return re.split(r"(?:\r?\n){2,}", string.strip())
  9. def remove_newlines(splitted_string):
  10. newlist = []
  11. for i in splitted_string:
  12. i = re.sub("\n", " ", i)
  13. newlist.append(i)
  14. return newlist
  15. def split_at_space(string):
  16. return string.split()
  17. def find_letters(newlines_removed):
  18. amount_of_yes = []
  19. for i in newlines_removed:
  20. list_of_strings = split_at_space(i)
  21. letters_list = ["Yes"] * 26
  22. for j in list_of_strings:
  23. if "a" not in j:
  24. letters_list[0] = "No"
  25. if "b" not in j:
  26. letters_list[1] = "No"
  27. if "c" not in j:
  28. letters_list[2] = "No"
  29. if "d" not in j:
  30. letters_list[3] = "No"
  31. if "e" not in j:
  32. letters_list[4] = "No"
  33. if "f" not in j:
  34. letters_list[5] = "No"
  35. if "g" not in j:
  36. letters_list[6] = "No"
  37. if "h" not in j:
  38. letters_list[7] = "No"
  39. if "i" not in j:
  40. letters_list[8] = "No"
  41. if "j" not in j:
  42. letters_list[9] = "No"
  43. if "k" not in j:
  44. letters_list[10] = "No"
  45. if "l" not in j:
  46. letters_list[11] = "No"
  47. if "m" not in j:
  48. letters_list[12] = "No"
  49. if "n" not in j:
  50. letters_list[13] = "No"
  51. if "o" not in j:
  52. letters_list[14] = "No"
  53. if "p" not in j:
  54. letters_list[15] = "No"
  55. if "q" not in j:
  56. letters_list[16] = "No"
  57. if "r" not in j:
  58. letters_list[17] = "No"
  59. if "s" not in j:
  60. letters_list[18] = "No"
  61. if "t" not in j:
  62. letters_list[19] = "No"
  63. if "u" not in j:
  64. letters_list[20] = "No"
  65. if "v" not in j:
  66. letters_list[21] = "No"
  67. if "w" not in j:
  68. letters_list[22] = "No"
  69. if "x" not in j:
  70. letters_list[23] = "No"
  71. if "y" not in j:
  72. letters_list[24] = "No"
  73. if "z" not in j:
  74. letters_list[25] = "No"
  75. amount_of_yes.append(letters_list.count("Yes"))
  76. return amount_of_yes
  77. def find_total_yes(amount_of_yes):
  78. total = 0
  79. for i in amount_of_yes:
  80. total = total + i
  81. return total
  82. string = file_to_string("day6input.txt")
  83. splitted_string = split_on_empty_lines(string)
  84. newlines_removed = remove_newlines(splitted_string)
  85. amount_of_yes = find_letters(newlines_removed)
  86. total_yes = find_total_yes(amount_of_yes)
  87. print(total_yes)