aoc2021/day03.py
Stefan Harmuth dde2715d2e day03
2021-12-03 06:39:52 +01:00

41 lines
1.2 KiB
Python

from aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = [198]
test_solutions_p2 = [230]
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 = [a for a in self.getInput()]
report_co2 = [a for a in 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)