Working out solutions for Advent of Code
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """ Advent of Code Day 5b
  2. """
  3. from collections import defaultdict
  4. def comp(s1,s2):
  5. if (s1 != s2) and s1.lower() == s2.lower():
  6. return True
  7. return False
  8. def main(polymer):
  9. opoly = polymer
  10. lens = defaultdict(int)
  11. alphabet = "abcdefghijklmnopqrstuvwxyz"
  12. for char in alphabet:
  13. polymer = "".join(opoly.split(char))
  14. polymer = "".join(polymer.split(char.upper()))
  15. do_it = True
  16. while do_it:
  17. olen = len(polymer)
  18. # print(olen)
  19. for acter in alphabet:
  20. if acter != char:
  21. polymer = "".join(polymer.split("{}{}".format(acter, acter.upper())))
  22. polymer = "".join(polymer.split("{}{}".format(acter.upper(), acter)))
  23. print("with {}, after {}, len(polymer) is {}".format(char, acter, len(polymer)))
  24. # for i in range(len(polymer)-1):
  25. # if comp(polymer[i], polymer[i+1]):
  26. # if i == 0: # the first two are a match
  27. # polymer = polymer[2:]
  28. # elif i == (olen - 2): # the last two are a match
  29. # polymer = polymer[:-2]
  30. # else: # the match is in the middle
  31. # polymer = polymer[:i] + polymer[i+2:]
  32. # break
  33. if olen == len(polymer): # we didn't reduce polymer's length at all
  34. do_it = False # no more matches, stop looping
  35. lens[char] = len(polymer)
  36. with open("05out.txt","a") as ofile:
  37. ofile.write("{}: {}\n".format(char, len(polymer)))
  38. for k,v in lens.items():
  39. print("{}: {}".format(k,v))
  40. if __name__ == "__main__":
  41. inline = ""
  42. with open("05in.txt","r") as ofile:
  43. inline = ofile.read().strip()
  44. main(inline)