def main(): """Main Puzzle Entry.""" recipes = int(get_puzzle_input("input/input14.txt")[0]) scoreboard = [3, 7] elf1 = 0 # The first elf picks the score at index 0 elf2 = 1 # The second elf gets the next one print( "Part 1: Return 10 Recipes after number of iterations from puzzle input." ) while len(scoreboard) < recipes + 10: scoreboard, elf1, elf2 = create_new_recipe(scoreboard, elf1, elf2) print("".join([str(x) for x in scoreboard[-10:]])) print("Part 2: Return index of pattern match from puzzle input.") scoreboard = [3, 7] elf1 = 0 # The first elf picks the score at index 0 elf2 = 1 # The second elf gets the next one pattern = [int(letter) for letter in str(recipes)] while True: scoreboard, elf1, elf2 = create_new_recipe(scoreboard, elf1, elf2) try: index = find_sub_list(scoreboard[-20:], pattern) if index: length = len(scoreboard) - (20 - index) print(f"Pattern Appears at {length}") print(scoreboard[-20:]) break except ValueError: pass
def main(): """Main Puzzle Entry.""" puzzle = get_puzzle_input("input/input16.txt") samples = get_sample_operations("input/input16.txt") print("Part 1: How many samples behave like 3 or more opcodes?") print(len(find_ops_that_match_xplus(samples, 3))) print("Part 2: Find the opcode numbers, run the program and return register 0.") dev = solve_opcodes(samples) dev.run_program(transform_program(puzzle[3005:])) print(dev)
def main(): """Main Puzzle Entry.""" puzzle = get_puzzle_input("input/input19.txt") program = transform_program(puzzle[1:]) print("Part 1: What is left in register 0 after the program ends.") dev = Device(instruction_register=int(puzzle[0][3:])) dev.run_program_with_flow_control(program) print(dev) print( "Part 2: Register 0 started with the value 1. What is left after the program?" ) dev2 = Device(registers=[1, 0, 0, 0, 0, 0], instruction_register=int(puzzle[0][3:])) dev2.run_program_with_flow_control(program) print(dev2)
def print_grid(points): """Print the grid of points using the smallest and largest points as the bounding box.""" x_bound = get_x_boundary(points) y_bound = get_y_boundary(points) for row in range(y_bound[0], y_bound[1] + 1): for column in range(x_bound[0], x_bound[1] + 1): if location_exists_at_points(points, column, row): print("#", end="") else: print(".", end="") print() if __name__ == "__main__": PUZZLE = get_puzzle_input("input/input10.txt") POINTS = get_points_from_input(PUZZLE) for point in POINTS: point.advance(10086) print_grid(POINTS) def test_location(): """Verify that we can advance n steps correctly.""" location = Location(Point(0, 0), Velocity(1, -1)) location.advance(1) assert location.point == {"x": 1, "y": -1} location.advance(2) assert location.point == {"x": 3, "y": -3}
"""Advent of Code 2018 Day 1""" from shared.common import get_puzzle_input def device_calibration(frequencies): """Calculate the final frequency drift after falling in time.""" return sum(frequency for frequency in frequencies) def find_repeating_frequency(frequencies): """Find the first frequency that repeats.""" seen_frequencies = set() current_freq = 0 while True: for frequency in frequencies: current_freq += frequency if current_freq not in seen_frequencies: seen_frequencies.add(current_freq) else: return current_freq if __name__ == "__main__": frequencies = [int(x) for x in get_puzzle_input("input/input1.txt")] print(f"Final Frequency: {device_calibration(frequencies)}") print(f"First Repeated: {find_repeating_frequency(frequencies)}")
self.value = 0 def generate_nodes(tree, data, parent): """Walk through the tree, recursively generating nodes""" node = Node(parent) num_of_children = next(data) num_of_metadata = next(data) for _ in range(num_of_children): tree, data, val = generate_nodes(tree, data, node) node.child_values.append(val) for _ in range(num_of_metadata): node.meta.append(next(data)) if num_of_children == 0: node.value = sum(node.meta) else: for val in node.meta: try: node.value += node.child_values[val - 1] except IndexError: pass tree.append(node) return tree, data, node.value if __name__ == "__main__": PUZZLE = [int(x) for x in get_puzzle_input("input/input8.txt")[0].split(" ")] TREE, _, ROOT_VALUE = generate_nodes([], iter(PUZZLE), "Root") print(f"Metadata sum: {sum([sum(node.meta) for node in TREE])}") print(f"Root Value: {ROOT_VALUE}")
def play_marbles(num_of_players, num_of_marbles): """Play a game of marbles with the elves.""" player = cycle([x for x in range(num_of_players)]) score = {key: 0 for key in range(num_of_players)} current_marble = Marble(0) current_marble.next = current_marble current_marble.previous = current_marble for marble, current_player in zip(range(1, num_of_marbles + 1), player): if marble % 23 == 0: score[current_player] += marble for _ in range(7): current_marble = current_marble.previous score[current_player] += current_marble.number current_marble = current_marble.remove() else: current_marble = current_marble.next.append(marble) return max(value for _, value in score.items()) if __name__ == "__main__": PUZZLE = get_puzzle_input("input/input9.txt")[0].split(" ") PLAYERS = int(PUZZLE[0]) LAST_MARBLE = int(PUZZLE[6]) FINAL_SCORE = play_marbles(PLAYERS, LAST_MARBLE) # print(f"32: {play_marbles(9, 25)}") # print(f"8317: {play_marbles(10, 1618)}") # print(f"146373: {play_marbles(13, 7999)}") # print(f"2764: {play_marbles(17, 1104)}") print(f"Final Score is {FINAL_SCORE}") print(f"Jackpot is {play_marbles(PLAYERS, LAST_MARBLE * 100)}")
count += 1 closest_point, total_distance = get_nearest_point((x, y), points) if y == 0 or y == (upper["y"] - 1) or x == 0 or x == (upper["x"] - 1): infinite_points.add(closest_point) if total_distance < 10000: safe_regions.append((x, y)) grid[x][y] = closest_point print(f"Safe Region Size: {len(safe_regions)}") return grid, {p: v for p, v in points.items() if p not in infinite_points} if __name__ == "__main__": COORDINATES = [(int(x[0]), int(x[1])) for x in (x.split(",") for x in get_puzzle_input("input/input6.txt"))] UPPER = get_upper_boundary(COORDINATES) NEW_COORDINATES = {key: value for key, value in enumerate(COORDINATES)} GRID, VALID_POINTS = generate_and_fill_grid(UPPER, NEW_COORDINATES) AREAS = get_largest(GRID, VALID_POINTS) LARGEST = AREAS.most_common(1)[0] print(f"Largest Area: {VALID_POINTS[LARGEST[0]]} Size: {LARGEST[1]}") def test_manhattan_distance(): """Make sure that the rise/run is being calculated correctly.""" assert manhattan_distance((1, 1), (2, 2)) == 2 assert manhattan_distance((1, 2), (2, 2)) == 1 assert manhattan_distance((1, 3), (2, 2)) == 2