def get2020Product() -> Optional[int]: expense_list = readList('input.txt', int) for multiplicand in expense_list: for multiplier in expense_list: if multiplicand + multiplier == 2020: return multiplicand * multiplier return None
def __init__(self, max_length: int, filename='input.txt'): self.input_list = readList(filename, int) self.max_length = max_length self.input_deque = deque(self.input_list[0:self.max_length], maxlen=self.max_length) self.target_index = 0
def get2020ProductPart2() -> Optional[int]: expense_list = readList('input.txt', int) for first in expense_list: for second in expense_list: for third in expense_list: if first + second + third == 2020: return first * second * third return None
def getSeatIDs(): seat_ids: List[int] = [] max_row = 128 max_col = 8 for boarding_pass in readList(): row, col = getRow(boarding_pass, max_row), getCol(boarding_pass, max_col) seat_id = getSeatID(row, col) seat_ids.append(seat_id) return seat_ids
def answerTwo(): valid = [] for line in readList('input.txt'): occurrences, character, password = line.split(' ') occurrences = list(map(lambda x: int(x) - 1, occurrences.split('-'))) character = character.strip(':') assert (len(occurrences) == 2) if bool(password[occurrences[0]] == character) ^ bool( password[occurrences[1]] == character): valid.append(line) return len(valid)
def main(): # I did this not because it's good code, but to see if I could make it work. It works. part1 = [ password.count(character.strip(':')) in inclusiveRange(*map(int, occurrences.split('-'))) for occurrences, character, password in [line.split(' ') for line in readList('input.txt')] ].count(True) part2 = answerTwo() print(f'Answer 1: {part1}') print(f'Answer 2: {part2}')
def treesHit(right: int, down: int) -> int: tree_map = readList() trees_hit = 0 row_num = 0 width_increment = right for line in tree_map: if row_num % down != 0: row_num += 1 continue # The pattern repeats col_num = (row_num * width_increment) % len(line) if line[col_num] == '#': trees_hit += 1 row_num += 1 return trees_hit
def __init__(self, filename='input.txt'): self.accumulator = 0 self.instructions = readList(filename=filename) self.instructions_read = set() self.instruction_pattern = re.compile(r'^(acc|jmp|nop) ([+\-])(\d+)$')
def __init__(self, filename: str = 'input.txt'): self.input_list = readList(filename=filename) # A list of top level bags found at the start of each line self.bag_graph = {} self.gold_list = [] self.total_bags: int = 0
import os from os.path import join from common import readList, quote, keywordsAndSymbolsFrequencies languages = readList("langs.txt") results = dict() for language in languages: print "Language: ", language files = [f for f in os.listdir(language) if os.path.isfile(join(language, f))] fileno = 1 kws = readList(language + "_keywords.txt") for filename in files: if fileno % 10 == 0: print filename, " file ", fileno, " out of ", len(files) fileno += 1 with open(language + "/" + filename, 'r') as file: features = keywordsAndSymbolsFrequencies(kws, file.read()) results[filename] = (features, language) allFeatures = reduce(lambda x, y: x | set(y[0].keys()), results.values(), set()) with open("out_nonalpha.csv", "w") as out: out.write("name," + ",".join(map(quote, allFeatures)) + "\n") for entry in results.keys(): out.write(results[entry][1]) for feature in allFeatures:
def __init__(self, filename='input.txt'): self.input_list = [0] + readList(filename, int) self.list_combinations = set()