コード例 #1
0
from solutions.common.file_reader import FileReader
from solutions.day12.ship import Ship
from solutions.day12.waypoint import Waypoint


def execute_single_instruction(navigated_ship, ship_waypoint, instruction):
    action = instruction[0]
    value = int(instruction[1:])
    if action in ["N", "W", "E", "S"]:
        ship_waypoint.move_in_cardinal_direction(action, value)
    elif action in ["L", "R"]:
        ship_waypoint.rotate(action, value)
    else:
        navigated_ship.move_to_waypoint(ship_waypoint, value)


if __name__ == '__main__':
    puzzle_input = FileReader.to_line_list("input.txt")
    ship = Ship()
    waypoint = Waypoint()
    for instr in puzzle_input:
        execute_single_instruction(ship, waypoint, instr)
    manhattan_distance = abs(ship.position[0]) + abs(ship.position[1])
    print("Manhattan distance from starting position: {}".format(
        manhattan_distance))
コード例 #2
0
from solutions.common.file_reader import FileReader
from solutions.day10.distinct_adapters_arrangements import count_number_of_distinct_adapters_arrangements


def build_chain_using_all_adapters(available_adapters):
    return sorted(available_adapters)


def count_given_differences(chain, value):
    return len(
        list(filter(lambda x: x[0] - x[1] == value, zip(chain[1:],
                                                        chain[:-1]))))


if __name__ == '__main__':
    puzzle_input = [int(x) for x in FileReader.to_line_list("input.txt")]
    built_in_adapter = max(puzzle_input) + 3
    valid_chain = [0] + build_chain_using_all_adapters(puzzle_input) + [
        built_in_adapter
    ]
    number_of_one_jolt_differences = count_given_differences(valid_chain, 1)
    number_of_three_jolt_differences = count_given_differences(valid_chain, 3)
    print("[PART 1] Number of one jolt differences: {}".format(
        number_of_one_jolt_differences))
    print("[PART 1] Number of three jolt differences: {}".format(
        number_of_three_jolt_differences))
    print("[PART 1] Product: {}".format(number_of_one_jolt_differences *
                                        number_of_three_jolt_differences))
    print("[PART 2] Number of distinct adapters arrangements: {}".format(
        count_number_of_distinct_adapters_arrangements(valid_chain)))
コード例 #3
0
from solutions.common.file_reader import FileReader
from solutions.day02.puzzle_input_parser import parse_puzzle_input


def validate_password(letter_lower_bound, letter_upper_bound, letter,
                      password):
    letter_count = password.count(letter)
    return letter_lower_bound <= letter_count <= letter_upper_bound


if __name__ == '__main__':
    puzzle_input = parse_puzzle_input(FileReader.to_line_list("input.txt"))
    number_of_valid_passwords = len(
        [entry for entry in puzzle_input if validate_password(*entry)])
    print(number_of_valid_passwords)
コード例 #4
0
from functools import reduce
from solutions.common.file_reader import FileReader


def convert_to_int_array(to_convert):
    return [0 if x == '.' else 1 for x in to_convert]


def count_trees(vertical, horizontal, pattern):
    pattern_width = len(pattern[0])
    current_position = (vertical, horizontal)
    encountered_trees = 0
    while current_position[0] < len(pattern):
        encountered_trees = encountered_trees + puzzle_input[
            current_position[0]][current_position[1]]
        current_position = current_position[0] + vertical, (
            current_position[1] + horizontal) % pattern_width
    return encountered_trees


if __name__ == '__main__':
    puzzle_input = [
        convert_to_int_array(x) for x in FileReader.to_line_list("input.txt")
    ]
    slopes = [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)]
    trees = [count_trees(x[0], x[1], puzzle_input) for x in slopes]
    print("Part 1: {}".format(trees[1]))
    print("Part 2: {}".format(reduce(lambda x, y: x * y, trees)))