aoc2021/day03.py
2021-12-28 10:05:13 +01:00

54 lines
1.4 KiB
Python

from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(198, "test_input03"),
(4147524, "input03")
],
[
(230, "test_input03"),
(3570354, "input03")
]
]
def part1(self) -> Any:
report = self.getInput()
gamma = epsilon = ""
for c in range(len(report[0])):
count = 0
for b in report:
count += b[c] == "1"
gamma += str(int(count >= len(report) // 2))
epsilon += str(int(count < len(report) // 2))
return int(gamma, 2) * int(epsilon, 2)
def part2(self) -> Any:
report_ox = self.getInput()
report_co2 = self.getInput()
for i in range(len(report_ox[0])):
count_ox = 0
count_co2 = 0
for num in report_ox:
count_ox += num[i] == "1"
for num in report_co2:
count_co2 += num[i] == "1"
if len(report_ox) > 1:
report_ox = [a for a in report_ox if a[i] == str(int(count_ox >= len(report_ox) / 2))]
if len(report_co2) > 1:
report_co2 = [a for a in report_co2 if a[i] == str(int(count_co2 < len(report_co2) / 2))]
return int(report_ox[0], 2) * int(report_co2[0], 2)
if __name__ == '__main__':
day = Day(2021, 3)
day.run(verbose=True)