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.

51 lines
1.2 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 = i.replace('\r', '').replace('\n', '')
  13. newlist.append(i)
  14. return newlist
  15. def compare_j_value(j):
  16. if j == 7:
  17. return "Valid"
  18. else:
  19. return "Invalid"
  20. def find_attributes(newlines_removed):
  21. newlist = []
  22. for i in newlines_removed:
  23. j = 0
  24. if "byr:" in i:
  25. j = j + 1
  26. if "iyr:" in i:
  27. j = j + 1
  28. if "eyr:" in i:
  29. j = j + 1
  30. if "hgt:" in i:
  31. j = j + 1
  32. if "hcl:" in i:
  33. j = j + 1
  34. if "ecl:" in i:
  35. j = j + 1
  36. if "pid:" in i:
  37. j = j + 1
  38. newlist.append(compare_j_value(j))
  39. return newlist
  40. string = file_to_string("day4input.txt")
  41. splitted_string = split_on_empty_lines(string)
  42. newlines_removed = remove_newlines(splitted_string)
  43. list_of_valids_invalids = find_attributes(newlines_removed)
  44. print(list_of_valids_invalids)
  45. print(list_of_valids_invalids.count("Valid"))