12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- def common_digit(list_of_lists: list, position: int, most=True) -> int:
- digits = []
- for ip in list_of_lists:
- digits.append(int(ip[position]))
- # print(f"1: {digits.count(1)}, 0: {digits.count(0)}", end="")
- if not most:
- return 0 if digits.count(1) >= digits.count(0) else 1
- return 1 if digits.count(1) >= digits.count(0) else 0
-
- def main() -> None:
- with open("aoc3-1.txt", "r") as file:
- inputs = [el.strip() for el in file.readlines()]
- digits = [[] for _ in range(len(inputs[0]))]
- for j, ip in enumerate(inputs):
- for i, dg in enumerate(ip):
- dg = dg.strip()
- if dg != "":
- digits[i].append(int(dg))
- gamma, epsilon = [], []
- for el in digits:
- gamma.append(1 if el.count(1) >= el.count(0) else 0)
- epsilon.append(0 if el.count(1) >= el.count(0) else 1)
- gamma_str = "".join([str(el) for el in gamma])
- epsilon_str = "".join([str(el) for el in epsilon])
- gamma_int = int(gamma_str, 2)
- epsilon_int = int(epsilon_str, 2)
- print(f"Gamma: {gamma_str} -- {gamma_int}")
- print(f"Epsilon: {epsilon_str} -- {epsilon_int}")
- print(f"Consumption: {gamma_int*epsilon_int}")
- oxy_inputs = [el for el in inputs]
- co2_inputs = [el for el in inputs]
- i = 0
- while len(oxy_inputs) > 1:
- mcd = common_digit(oxy_inputs, i)
- oxy_inputs = [el for el in oxy_inputs if el[i] == str(mcd)]
- # print(f", MCD: {mcd}, len: {len(oxy_inputs)}")
- i += 1
- i = 0
- while len(co2_inputs) > 1:
- lcd = common_digit(co2_inputs, i, False)
- co2_inputs = [el for el in co2_inputs if el[i] == str(lcd)]
- # print(f", LCD: {lcd}, len: {len(co2_inputs)}")
- i += 1
- oxy_str, co2_str = oxy_inputs[0], co2_inputs[0]
- oxy_int, co2_int = int(oxy_str, 2), int(co2_str, 2)
-
- print(f"Oxygen: {oxy_str} -- {oxy_int}")
- print(f"CO2: {co2_str} -- {co2_int}")
- print(f"Life support: {oxy_int*co2_int}")
-
- if __name__ == "__main__":
- main()
|