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.

75 lines
1.9 KiB

  1. import re
  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 = str(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 get_numbers(list):
  13. nums = []
  14. for i in list:
  15. x = re.search("^([\S]+)", i).group(0)
  16. nums.append(x)
  17. return nums
  18. def get_strings(list):
  19. strings = []
  20. for i in list:
  21. y = re.sub(r".*?(\w+)\s*$", r"\1", i)
  22. strings.append(y)
  23. return strings
  24. def get_letters(list):
  25. letters = []
  26. for i in list:
  27. z = re.match("([^:]+)", i).group(0)
  28. z = z.split('.')[0].lstrip().split(' ')[1]
  29. letters.append(z)
  30. return letters
  31. def find_positions(nums):
  32. pos1 = []
  33. pos2 = []
  34. for i in nums:
  35. splitted_nums = []
  36. splitted_nums = i.split('-')
  37. posx = int(splitted_nums[0])
  38. posy = int(splitted_nums[1])
  39. pos1.append(posx)
  40. pos2.append(posy)
  41. return pos1, pos2
  42. def find_valids(strings, pos1, pos2, letters):
  43. j = 0
  44. valids = []
  45. for i in strings:
  46. string = strings[j]
  47. low_pos = pos1[j] - 1
  48. high_pos = pos2[j] - 1
  49. if string[low_pos] != string[high_pos]:
  50. if string[low_pos] == letters[j]:
  51. valids.append("Valid")
  52. elif string[high_pos] == letters[j]:
  53. valids.append("Valid")
  54. else:
  55. valids.append("Invalid")
  56. else:
  57. valids.append("Invalid")
  58. j = j+1
  59. return valids
  60. list = split_into_list("day2input.txt")
  61. nums = get_numbers(list)
  62. letters = get_letters(list)
  63. strings = get_strings(list)
  64. pos1, pos2 = find_positions(nums)
  65. valids = find_valids(strings, pos1, pos2, letters)
  66. print(valids)
  67. print(valids.count("Valid"))