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.

преди 3 години
123456789101112131415161718192021222324252627282930
  1. import sys
  2. def main():
  3. with open("aoc3-1.txt", "r") as file:
  4. inputs = [el.strip() for el in file.readlines()]
  5. digits = [[] for _ in range(len(inputs[0]))]
  6. for j, ip in enumerate(inputs):
  7. # print(digits, ip)
  8. for i, dg in enumerate(ip):
  9. # print(i, dg)
  10. dg = dg.strip()
  11. if dg != "":
  12. digits[i].append(int(dg))
  13. # if j >= 2:
  14. # sys.exit(0)
  15. gamma, epsilon = [], []
  16. for el in digits:
  17. gamma.append(1 if el.count(1) >= el.count(0) else 0)
  18. epsilon.append(0 if el.count(1) >= el.count(0) else 1)
  19. gamma_str = "".join([str(el) for el in gamma])
  20. epsilon_str = "".join([str(el) for el in epsilon])
  21. gamma_int = int(gamma_str, 2)
  22. epsilon_int = int(epsilon_str, 2)
  23. print(f"Gamma: {gamma_str} -- {gamma_int}")
  24. print(f"Epsilon: {epsilon_str} -- {epsilon_int}")
  25. print(f"Consumption: {gamma_int*epsilon_int}")
  26. if __name__ == "__main__":
  27. main()