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.

79 lines
2.0 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. #print(string)
  50. #print([letters[j]])
  51. #print(string[low_pos])
  52. #print(string[high_pos])
  53. if string[low_pos] != string[high_pos]:
  54. if string[low_pos] == letters[j]:
  55. valids.append("Valid")
  56. elif string[high_pos] == letters[j]:
  57. valids.append("Valid")
  58. else:
  59. valids.append("Invalid")
  60. else:
  61. valids.append("Invalid")
  62. j = j+1
  63. return valids
  64. list = split_into_list("day2input.txt")
  65. nums = get_numbers(list)
  66. letters = get_letters(list)
  67. strings = get_strings(list)
  68. pos1, pos2 = find_positions(nums)
  69. valids = find_valids(strings, pos1, pos2, letters)
  70. print(valids)
  71. print(valids.count("Valid"))