3.py 886 B

123456789101112131415161718192021222324252627282930313233
  1. from util import get_input
  2. input = get_input('3.input')
  3. def count(input):
  4. counts = [0 for _ in input[0]]
  5. for line in input:
  6. for i, c in enumerate(line):
  7. counts[i] += 1 if c == '1' else 0
  8. return counts
  9. gamma = ''.join(['1' if c > len(input) / 2 else '0' for c in count(input)])
  10. epsilon = ''.join(['0' if c > len(input) / 2 else '1' for c in count(input)])
  11. print(int(gamma, 2) * int(epsilon, 2))
  12. def search(input, invert):
  13. remaining = list(input)
  14. pos = 0
  15. while len(remaining) > 1:
  16. print(len(remaining))
  17. cmp = count(remaining)[pos] >= len(remaining) / 2
  18. if invert:
  19. cmp = not cmp
  20. target = '1' if cmp else '0'
  21. remaining = [a for a in remaining if a[pos] == target]
  22. pos += 1
  23. return remaining[0]
  24. ox = search(input, False)
  25. co = search(input, True)
  26. print(int(ox, 2) * int(co, 2))