Browse Source

Adding day03

Co-Authored-By: Ryan Reed <ryanreed@noreply.gitea.ryanreed.net>
Co-Committed-By: Ryan Reed <ryanreed@noreply.gitea.ryanreed.net>
pull/4/head
Ryan Reed 3 years ago
parent
commit
b5707cd2e4
4 changed files with 1205 additions and 0 deletions
  1. +12
    -0
      inputs/day03-example.txt
  2. +1000
    -0
      inputs/day03.txt
  3. +158
    -0
      puzzles/day03.py
  4. +35
    -0
      puzzles/test_day03.py

+ 12
- 0
inputs/day03-example.txt View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

+ 1000
- 0
inputs/day03.txt
File diff suppressed because it is too large
View File


+ 158
- 0
puzzles/day03.py View File

@ -0,0 +1,158 @@
"""
Advent of Code 2021 - Day 03
Run with:
python puzzles/day03.py inputs/day03.txt
"""
import pathlib
import sys
from typing import Dict, List, Tuple
from collections import defaultdict
class Diagnostics:
report = None
def __init__(self, report: List[int]) -> None:
self.report = report
def calc_power_usage(self) -> None:
most_common = self.commonality(self.report)
gamma = epsilon = ""
for common in most_common:
if common == 1:
gamma += "1"
epsilon += "0"
else:
gamma += "0"
epsilon += "1"
gamma = int(gamma, 2)
epsilon = int(epsilon, 2)
return gamma * epsilon
def calc_oxygen_generator_rating(self, inputs) -> int:
"""
Bit Criteria:
Most common value in current bit position
If 0 and 1 are equally common, keep values with a 1
Keep only values with this bit in this position
"""
report = inputs.copy()
for column in range(len(report[0])):
most_common = self.commonality(report)
for binary in list(report):
if (
int(binary[column]) != most_common[column]
and most_common[column] != 2
) or (most_common[column] == 2 and binary[column] != "1"):
report.remove(binary)
if len(report) == 1:
return int(report[0], 2)
if len(report) > 1: # May not be necessary
report = self.calc_oxygen_generator_rating(report)
return int(report[0], 2)
def calc_co2_scrubber_rating(self, inputs) -> int:
"""
Bit Criteria:
Least common value in current position
If 0 and 1 are equally common, keep values with a 0
Keep only values with this bit in this position
"""
report = inputs.copy()
for column in range(len(report[0])):
most_common = self.commonality(report)
for binary in list(report):
if (int(binary[column]) == most_common[column]) or (
most_common[column] == 2 and binary[column] != "0"
):
report.remove(binary)
if len(report) == 1:
return int(report[0], 2)
if len(report) > 1: # May not be necessary
report = self.calc_oxygen_generator_rating(report)
return int(report[0], 2)
def calc_life_support_rating(self) -> int:
oxygen_generator_rating = self.calc_oxygen_generator_rating(self.report)
co2_scrubber_rating = self.calc_co2_scrubber_rating(self.report)
return oxygen_generator_rating * co2_scrubber_rating
@staticmethod
def commonality(report: List[int]) -> List[int]:
"""
Return the most common value in each column of binary numbers
Args:
report: List of binary numbers
Returns:
List containing the most common value in the column
Possible Values:
0
1
2 - This indicates equally common
"""
num_1 = defaultdict(int)
for binary in report:
for column, bit in enumerate(binary):
num_1[column] += int(bit)
common = []
half_total = len(report) / 2
for count in num_1.values():
if count == half_total:
common.append(2) # Indicating equally common
elif count > half_total:
common.append(1)
else:
common.append(0)
return common
def part1(inputs: List[int]) -> int:
diag = Diagnostics(inputs)
return diag.calc_power_usage()
def part2(inputs: List[int]) -> int:
diag = Diagnostics(inputs)
return diag.calc_life_support_rating()
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()

+ 35
- 0
puzzles/test_day03.py View File

@ -0,0 +1,35 @@
"""
Remove the 'Not implemented' marks when ready to run the test
"""
import pathlib
import pytest
import day03 as aoc
INPUTS_DIR = f"{pathlib.Path(__file__).parent.parent}/inputs"
@pytest.fixture
def example_data():
input_path = f"{INPUTS_DIR}/day03-example.txt"
return aoc.parse(pathlib.Path(input_path).read_text().strip())
@pytest.fixture
def day03_data():
input_path = f"{INPUTS_DIR}/day03.txt"
return aoc.parse(pathlib.Path(input_path).read_text().strip())
def test_example1(example_data):
assert aoc.part1(example_data) == 198
def test_example2(example_data):
assert aoc.part2(example_data) == 230
def test_part1(day03_data):
assert aoc.part1(day03_data) == 3885894
def test_part2(day03_data):
assert aoc.part2(day03_data) == 4375225

Loading…
Cancel
Save