|
|
@ -0,0 +1,65 @@ |
|
|
|
""" |
|
|
|
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 |
|
|
|
|
|
|
|
def part1(inputs: List[int]) -> int: |
|
|
|
num_1 = defaultdict(int) |
|
|
|
for line in inputs: |
|
|
|
for index, value in enumerate(line): |
|
|
|
num_1[index] += int(value) |
|
|
|
|
|
|
|
total = len(inputs) |
|
|
|
gamma = epsilon = "" |
|
|
|
for value in num_1.values(): |
|
|
|
if value > (total/2): |
|
|
|
gamma += "1" |
|
|
|
epsilon += "0" |
|
|
|
else: |
|
|
|
gamma += "0" |
|
|
|
epsilon += "1" |
|
|
|
|
|
|
|
gamma = int(gamma, 2) |
|
|
|
epsilon = int(epsilon, 2) |
|
|
|
power = gamma * epsilon |
|
|
|
return 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() |