"""
|
|
Advent of Code 2021 - Day 03
|
|
|
|
Run with:
|
|
python puzzles/day03.py inputs/day03.txt
|
|
"""
|
|
|
|
import pathlib
|
|
import sys
|
|
from typing import List, Tuple
|
|
|
|
from collections import defaultdict
|
|
|
|
class Submarine:
|
|
gamma: int = 0
|
|
epsilon: int = 0
|
|
power: int = 0
|
|
|
|
def decode_report(self, report: List[int]) -> None:
|
|
num_1 = defaultdict(int)
|
|
for binary in report:
|
|
for index, bit in enumerate(binary):
|
|
num_1[index] += int(bit)
|
|
|
|
total = len(report)
|
|
gamma = epsilon = ""
|
|
for binary in num_1.values():
|
|
if binary > (total/2):
|
|
gamma += "1"
|
|
epsilon += "0"
|
|
else:
|
|
gamma += "0"
|
|
epsilon += "1"
|
|
|
|
self.gamma = int(gamma, 2)
|
|
self.epsilon = int(epsilon, 2)
|
|
self.power = self.gamma * self.epsilon
|
|
|
|
|
|
def part1(inputs: List[int]) -> int:
|
|
sub = Submarine()
|
|
sub.decode_report(inputs)
|
|
return sub.power
|
|
|
|
def part2(inputs: List[int]) -> int:
|
|
return False
|
|
|
|
|
|
def parse(inputs: str) -> List[int]:
|
|
"""Parse the input string"""
|
|
return [line for line in inputs.split()]
|
|
|
|
|
|
def solve(path: str) -> Tuple[int, int]:
|
|
"""Solve the puzzle"""
|
|
puzzle_input = parse(pathlib.Path(path).read_text().strip())
|
|
part1_result = part1(puzzle_input)
|
|
part2_result = part2(puzzle_input)
|
|
|
|
return part1_result, part2_result
|
|
|
|
|
|
def main() -> None:
|
|
for path in sys.argv[1:]:
|
|
print(f"Input File: {path}")
|
|
|
|
part1_result, part2_result = solve(path)
|
|
|
|
print(f"Part 1 Result: {part1_result}")
|
|
print(f"Part 2 Result: {part2_result}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|