def run(data): data = map_as_list(lambda x: int(x), data) result_1 = count_increases(data) result_2 = count_rolling_increases(data) return (result_1, result_2)
def run(data): data = map_as_list(parse_control, data) result_1 = fold(lambda x, y : (x[0] + y[0], x[1] + y[1]), data, (0, 0)) result_1 = result_1[0] * result_1[1] result_2 = fold(lambda x, y : (x[0] + y[0], x[0] * y[2] + y[1], x[1] + y[2]), data, (0, 0, 0)) result_2 = result_2[0] * result_2[1] return (result_1, result_2)
def run(data): data = map_as_list(parse_binary, data) gamma = binary_to_decimal(get_most_common_bits(data)) result_1 = gamma * ((2 ** len(data[0]) - 1) - gamma) o2_gen_rating = get_o2_gen_rating(data) co2_scrub_rating = get_co2_scrub_rating(data) result_2 = binary_to_decimal(o2_gen_rating) * binary_to_decimal(co2_scrub_rating) return (result_1, result_2)
def run(data): data = map_as_list(int, data[0].split(',')) data.sort() def get_fuel_to_1(i): return sum(map_as_list(lambda x: abs(x - i), data)) def get_fuel_to_2(i): return sum(map_as_list(lambda x: sum_to_n(abs(x - i)), data)) result_1 = min(map(get_fuel_to_1, range(data[0], data[-1] + 1))) result_2 = min(map(get_fuel_to_2, range(data[0], data[-1] + 1))) return (result_1, result_2)
def run(data): fish_counts = [] fishes = map_as_list(int, data[0].split(',')) for fish in fishes: while len(fish_counts) < fish + 1: fish_counts.append(0) fish_counts[fish] += 1 fish_counts = tick_many(fish_counts, 80) result_1 = sum(fish_counts) fish_counts = tick_many(fish_counts, 256 - 80) result_2 = sum(fish_counts) return (result_1, result_2)
def get_boards(data): data = data[2:] f = lambda x: [int(x), False] split_row = lambda r: map_as_list( f, filter(lambda x: len(x) > 0, r.split(' '))) i = 0 boards = [] while i < len(data): board = [] for j in range(5): board.append(split_row(data[i + j])) boards.append(board) i += 6 return boards
def main(): days = map_as_list(int, sys.argv[1:]) if len(days) == 0: days = range(1, 26) print('Running the following days:\n' + ', '.join(map(str, days)) + '\n') for day in days: try: data = load_file(day) code = importlib.import_module(f'days._{day}_code') except: print(f'Files for day {day} don\'t exist, exiting...\n') return print(day, code.run(data)) print()
def get_fuel_to_2(i): return sum(map_as_list(lambda x: sum_to_n(abs(x - i)), data))
def get_fuel_to_1(i): return sum(map_as_list(lambda x: abs(x - i), data))
import os import sys from helpers.list import map_as_list code_content = r""" def run(data): result_1 = None result_2 = None return (result_1, result_2) """ if __name__ == "__main__": days = map_as_list(int, sys.argv[1:]) directory = os.path.join(os.path.dirname(__file__), 'days') if not os.path.exists(directory): os.makedirs(directory) for day in days: code_path = os.path.join(directory, f'_{day}_code.py') input_path = os.path.join(directory, f'_{day}_input.txt') if not os.path.exists(code_path): with open(code_path, 'w') as file: file.write(code_content) if not os.path.exists(input_path): open(input_path, 'x')
def get_number_order(data): return map_as_list(int, data[0].split(','))