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 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. def common_digit(list_of_lists: list, position: int, most=True) -> int:
  2. digits = []
  3. for ip in list_of_lists:
  4. digits.append(int(ip[position]))
  5. # print(f"1: {digits.count(1)}, 0: {digits.count(0)}", end="")
  6. if not most:
  7. return 0 if digits.count(1) >= digits.count(0) else 1
  8. return 1 if digits.count(1) >= digits.count(0) else 0
  9. def main() -> None:
  10. with open("aoc3-1.txt", "r") as file:
  11. inputs = [el.strip() for el in file.readlines()]
  12. digits = [[] for _ in range(len(inputs[0]))]
  13. for j, ip in enumerate(inputs):
  14. for i, dg in enumerate(ip):
  15. dg = dg.strip()
  16. if dg != "":
  17. digits[i].append(int(dg))
  18. gamma, epsilon = [], []
  19. for el in digits:
  20. gamma.append(1 if el.count(1) >= el.count(0) else 0)
  21. epsilon.append(0 if el.count(1) >= el.count(0) else 1)
  22. gamma_str = "".join([str(el) for el in gamma])
  23. epsilon_str = "".join([str(el) for el in epsilon])
  24. gamma_int = int(gamma_str, 2)
  25. epsilon_int = int(epsilon_str, 2)
  26. print(f"Gamma: {gamma_str} -- {gamma_int}")
  27. print(f"Epsilon: {epsilon_str} -- {epsilon_int}")
  28. print(f"Consumption: {gamma_int*epsilon_int}")
  29. oxy_inputs = [el for el in inputs]
  30. co2_inputs = [el for el in inputs]
  31. i = 0
  32. while len(oxy_inputs) > 1:
  33. mcd = common_digit(oxy_inputs, i)
  34. oxy_inputs = [el for el in oxy_inputs if el[i] == str(mcd)]
  35. # print(f", MCD: {mcd}, len: {len(oxy_inputs)}")
  36. i += 1
  37. i = 0
  38. while len(co2_inputs) > 1:
  39. lcd = common_digit(co2_inputs, i, False)
  40. co2_inputs = [el for el in co2_inputs if el[i] == str(lcd)]
  41. # print(f", LCD: {lcd}, len: {len(co2_inputs)}")
  42. i += 1
  43. oxy_str, co2_str = oxy_inputs[0], co2_inputs[0]
  44. oxy_int, co2_int = int(oxy_str, 2), int(co2_str, 2)
  45. print(f"Oxygen: {oxy_str} -- {oxy_int}")
  46. print(f"CO2: {co2_str} -- {co2_int}")
  47. print(f"Life support: {oxy_int*co2_int}")
  48. if __name__ == "__main__":
  49. main()