Browse Source

Adding type hints

day09
Ryan Reed 3 years ago
parent
commit
9fc595e2f4
1 changed files with 10 additions and 10 deletions
  1. +10
    -10
      puzzles/day08.py

+ 10
- 10
puzzles/day08.py View File

@ -8,7 +8,7 @@ Run with:
import pathlib import pathlib
import sys import sys
from collections import defaultdict from collections import defaultdict
from typing import List, Tuple
from typing import Dict, List, Tuple
class Display: class Display:
@ -32,10 +32,10 @@ class Display:
gggg gggg .... gggg gggg gggg gggg .... gggg gggg
""" """
def __init__(self, entries: List[str]):
def __init__(self, entries: List[str]) -> None:
self.entries = self._parse(entries) self.entries = self._parse(entries)
def decode(self):
def decode(self) -> int:
results = [] results = []
for entry in self.entries: for entry in self.entries:
@ -45,21 +45,21 @@ class Display:
return sum(results) return sum(results)
def unique_count(self):
def unique_count(self) -> int:
unique = 0 unique = 0
for entry in self.entries: for entry in self.entries:
unique += self._get_unique(entry)
unique += self._get_unique(entry["outputs"])
return unique return unique
@staticmethod @staticmethod
def _decrypt(cipher, enc_string):
def _decrypt(cipher: Dict[str, int], enc_string: List[str]) -> int:
output = "" output = ""
for code in enc_string: for code in enc_string:
output += str(cipher["".join(sorted(code))]) output += str(cipher["".join(sorted(code))])
return int(output) return int(output)
@staticmethod @staticmethod
def _generate_cipher(entry):
def _generate_cipher(entry: List[str]) -> Dict[str, int]:
codes = {} # Length: Actual Number codes = {} # Length: Actual Number
filtered = defaultdict(list) filtered = defaultdict(list)
for signal in entry: for signal in entry:
@ -92,15 +92,15 @@ class Display:
return results return results
@staticmethod @staticmethod
def _get_unique(entries):
def _get_unique(entries: List[str]) -> int:
unique = 0 unique = 0
for entry in entries["outputs"]:
for entry in entries:
length = len(entry) length = len(entry)
unique += length in [2, 4, 3, 7] # 1,4,7,8 unique += length in [2, 4, 3, 7] # 1,4,7,8
return unique return unique
@staticmethod @staticmethod
def _parse(lines):
def _parse(lines: List[str]) -> List:
entries = [] entries = []
for line in lines: for line in lines:
split = line.split() split = line.split()


Loading…
Cancel
Save