Working out solutions for Advent of Code
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

02b.py 765B

1234567891011121314151617181920212223242526272829303132
  1. """ Advent of Code 2018
  2. December 02, puzzle 2
  3. """
  4. def main(lines):
  5. """ For each line, check to see if another line varies by only one character.
  6. There's an easy but expensive way to do this. I'll start with that
  7. and see if I can refine it later.
  8. """
  9. for i, line in enumerate(lines):
  10. if i + 1 != len(lines):
  11. for word in lines[(i+1):]:
  12. diffs = 0
  13. idx = 0
  14. for j in range(len(line)):
  15. if line[j] != word[j]:
  16. diffs += 1
  17. idx = j
  18. if diffs == 1:
  19. print("Found a match: {} and {}".format(line, word))
  20. print("Matched value: {}".format("".join([word[:idx],word[(idx+1):]])))
  21. return
  22. if __name__ == "__main__":
  23. lines = []
  24. with open("02in.txt","r") as f:
  25. for line in f:
  26. lines.append(line.strip())
  27. main(lines)