41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
test_solutions_p1 = [198, 4147524]
|
|
test_solutions_p2 = [230, 3570354]
|
|
|
|
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)
|