def read_input_4d(file_name: str) -> Grid4D: rows: List[str] = read_text_list(file_name) grid: Grid4D = {} for y in range(0, len(rows)): row = rows[y] coords: Grid4D = {Point4D(i, y, 0, 0): (value == '#') for i, value in enumerate(row)} grid.update(coords) return grid
candidate = 1 amount_to_add = 1 bus, offset = buses_with_offset.pop(0) found = False while not found: if (candidate + offset) % bus == 0: if len(buses_with_offset) == 0: found = True else: amount_to_add *= bus bus, offset = buses_with_offset.pop(0) else: candidate += amount_to_add return candidate def part_a(input_lines: List[str]) -> int: arrival_time = int(input_lines[0]) buses: List[int] = [int(bus) for bus in input_lines[1].replace(',x', '').split(',')] remainders = [(bus, math.ceil(arrival_time / bus) * bus - arrival_time) for bus in buses] best_bus = min(remainders, key=lambda bus_and_wait: bus_and_wait[1]) return best_bus[0] * best_bus[1] if __name__ == '__main__': lines = read_text_list('day_13.txt') print(part_a(lines)) print(part_b(lines[1]))
instruction_to_replace: int) -> List[str]: new_list = instruction_list.copy() current_value = new_list[instruction_to_replace] if 'acc' in current_value: return instruction_list elif 'nop' in current_value: new_value = current_value.replace('nop', 'jmp') new_list[instruction_to_replace] = new_value else: new_value = current_value.replace('jmp', 'nop') new_list[instruction_to_replace] = new_value return new_list def run_until_terminates(instruction_list: List[str]) -> int: instruction_to_replace = 0 result: tuple[int, bool] = run_program(instruction_list, instruction_to_replace) while not result[1]: instruction_to_replace += 1 result = run_program(instruction_list, instruction_to_replace) return result[0] if __name__ == '__main__': input_lines = read_text_list('day_8.txt') print(run_program(input_lines)) print(run_until_terminates(input_lines))
alphabetical_allergens = sorted(resolved.keys()) ingredients = [resolved[allergen] for allergen in alphabetical_allergens] return ','.join(ingredients) def resolve_allergens( possible_allergens: dict[Allergen, Set[Ingredient]] ) -> dict[Allergen, Ingredient]: resolved_positions: dict[Allergen, Ingredient] = {} while len(possible_allergens) > 0: determined_fields = list( filter(lambda e: len(e[1]) == 1, possible_allergens.items())) for allergen, ingredients in determined_fields: ingredient: Ingredient = possible_allergens.pop(allergen).pop() resolved_positions[allergen] = ingredient remove_from_all_values(possible_allergens, ingredient) return resolved_positions def remove_from_all_values(possible_allergens: dict[Allergen, Set[Ingredient]], ingredient: Ingredient): for ingredients in possible_allergens.values(): ingredients.discard(ingredient) if __name__ == '__main__': lines = read_text_list('day_21.txt') print(count_safe_ingredient_appearances(lines)) print(get_canonical_dangerous_list(lines))
import unittest from src.day_21.day_21 import * from utils import read_text_list example_lines = read_text_list('day_21_example.txt') class TestDay21(unittest.TestCase): def test_get_ingredients_and_allergens(self): ingredients, allergens = get_ingredients_and_allergens('mxmxvkd kfcds sqjhc nhms (contains dairy, fish)') self.assertEqual(ingredients, {'mxmxvkd', 'kfcds', 'sqjhc', 'nhms'}) self.assertEqual(allergens, ['dairy', 'fish']) def test_get_ingredients_and_allergens_2(self): ingredients, allergens = get_ingredients_and_allergens('trh fvjkl sbzzf mxmxvkd (contains dairy)') self.assertEqual(ingredients, {'trh', 'fvjkl', 'sbzzf', 'mxmxvkd'}) self.assertEqual(allergens, ['dairy']) def test_get_safe_ingredients(self): ingredients = get_safe_ingredients(example_lines) self.assertEqual(ingredients, {'kfcds', 'nhms', 'sbzzf', 'trh'}) def test_count_safe_ingredient_appearances(self): count = count_safe_ingredient_appearances(example_lines) self.assertEqual(count, 5) def test_get_allergen_to_culprits_dict(self): result = get_allergen_to_culprits_dict(example_lines) self.assertEqual(result, {'dairy': {'mxmxvkd'}, 'fish': {'sqjhc', 'mxmxvkd'}, 'soy': {'sqjhc', 'fvjkl'}})
] return math.prod(tree_counts) def count_trees_for_slope(input_lines: List[str], x_slope: int, y_slope: int) -> int: row_count: int = len(input_lines) rows_visited: List[str] = [ input_lines[ix] for ix in range(0, row_count, y_slope) ] x_coords: iter = count(0, x_slope) path: List[str] = [ get_item_at_location(row, next(x_coords)) for row in rows_visited ] return count_where(is_tree, path) def is_tree(item: str) -> bool: return item == '#' def get_item_at_location(row: str, x_coord: int) -> str: return row[x_coord % len(row)] if __name__ == '__main__': inputLines = read_text_list("day_3.txt") part_a(inputLines) part_b(inputLines)
def test_get_water_roughness(self): example_tile_lines = read_text_list('day_20_sea_monsters_grid.txt') tile = Tile.from_lines(5, example_tile_lines) self.assertEqual(tile.get_water_roughness(), 273)
def test_count_sea_monsters_2(self): example_tile_lines = read_text_list('day_20_sea_monsters_grid.txt') tile = Tile.from_lines(5, example_tile_lines) self.assertEqual(tile.count_sea_monsters(), 2)
def test_has_sea_monster(self): example_tile_lines = read_text_list('day_20_sea_monsters_grid.txt') tile = Tile.from_lines(5, example_tile_lines) self.assertTrue(tile.has_sea_monster((2, 2)))
def parse_black_tiles(file_name: str) -> Set[Hex]: lines = read_text_list(file_name) tiles = [line_to_hex(line) for line in lines] return {tile for tile in tiles if tiles.count(tile) % 2 != 0}