Пример #1
0
# Update the position coordinates using the given instruction
def get_new_position(instruction, position):
    if instruction is ">":
        position[0] += 1
    elif instruction is "<":
        position[0] -= 1
    elif instruction is "v":
        position[1] += 1
    elif instruction is "^":
        position[1] -= 1

    return position[:]


# Determine the history of santa's coordinates
def get_santa_movements(santas_movements):
    santas_current_position = [0, 0]
    history = [[0, 0]] + [get_new_position(movement, santas_current_position) for movement in santas_movements]
    return [str(coordinate) for coordinate in history]


# Read in the input
movements = input_reader.get_list("Day3/Day3_input.txt")[0]
santas_coordinate_history = get_santa_movements(movements)

# Convert the coordinates to strings to make them hashable
santas_coordinate_history_strings = map(str, santas_coordinate_history)

# Make a set out of the coordinates to remove duplicate coordinates, and count the results
num_all_houses_visited = len(set(santas_coordinate_history_strings))
print(num_all_houses_visited)
Пример #2
0
        if self.value:
            return self.value

        value_a = self.get(self.input_a_node)
        value_b = self.get(self.input_b_node)

        if self.operator == "AND":
            self.value = value_a & value_b
        elif self.operator == "OR":
            self.value = value_a | value_b

        return self.value


# circuit_strings = input_reader.get_list("Day7/test_input.txt")
circuit_strings = input_reader.get_list("Day7/Day7_input.txt")
nodes = {}

for line in circuit_strings:
    not_expression = re.findall(r'NOT\s(\w+)\W*(\w+)', line)
    bitshift_operator_expression = re.findall(r'(\w+)\s(LSHIFT|RSHIFT)\s(\d+)\W*(\w+)', line)
    logicial_operator_expression = re.findall(r'(\w+)\s(AND|OR)\s(\w+)\W*(\w+)', line)
    input_expression = re.findall(r'(\d+|\w+)\s->\s(\w+)', line)

    if not_expression:
        incoming_node, output_node = not_expression[0]
        NOTNode(incoming_node, output_node, nodes)

    elif bitshift_operator_expression:
        incoming_node, shift_side, shift_amount, output_node = bitshift_operator_expression[0]
        BITSHIFTNode(incoming_node, output_node, nodes, shift_side, shift_amount)
Пример #3
0
import input_reader


# The final floor is determined by subtracting the down movements from the op movements
def get_floor(movement_steps):
    floors_up = movement_steps.count("(")
    floors_down = movement_steps.count(")")
    return floors_up - floors_down


# Read the string input for this challenge
movement_steps_input = input_reader.get_list("Day1/Day1_input.txt")[0]
get_final_floor = get_floor(movement_steps_input)
print(get_final_floor)


Пример #4
0
import input_reader


# Gets the present string and turns it into an integer list of dimensions
def get_present_dimensions(present_string):
    dimensions = present_string.split("x")
    return list(map(int, dimensions))


# Gets the surface using the dimensions
def get_surface_from_dimensions(dimensions):
    # First calculate the slack
    surface_pre_multiplication = [dimensions[0]*dimensions[1], dimensions[0]*dimensions[2], dimensions[1]*dimensions[2]]
    slack = min(surface_pre_multiplication)
    # Calculate all the surfaces
    surfaces = [x*2 for x in surface_pre_multiplication]
    return sum(surfaces) + slack


total_surface = 0
present_dimensions_list = input_reader.get_list("Day2/Day2_input.txt")

present_dimensions = map(get_present_dimensions, present_dimensions_list)
surface_dimensions = map(get_surface_from_dimensions, present_dimensions)
total_surface = sum(surface_dimensions)

print(total_surface)
Пример #5
0
import re
import input_reader

def contains_three_vowels(possible_nice_string):
    vowels_in_string = re.findall('[aeiou]', possible_nice_string)
    return len(vowels_in_string) >= 3


def contains_repeating_letter(possible_nice_string):
    consequent_vowels_in_string = re.findall(r'(\w)(\1{1,})', possible_nice_string)
    return len(consequent_vowels_in_string) > 0


def contains_forbidden_word(possible_nice_string):
    found_forbidden_words = re.findall(r'ab|cd|pq|xy', possible_nice_string)
    return len(found_forbidden_words) > 0


def is_nice(possible_nice_string):
    return contains_three_vowels(possible_nice_string) \
           and contains_repeating_letter(possible_nice_string) \
           and not contains_forbidden_word(possible_nice_string)


possibly_nice_strings = input_reader.get_list("Day5/Day5_input.txt")
are_nice = list(map(is_nice, possibly_nice_strings))
print(are_nice.count(True))
Пример #6
0

def do_instruction(grid, instruction_string):
    evaluated_expression = re.findall(r'(turn on|turn off|toggle)\s(\d+),(\d+)\sthrough\s(\d+),(\d+)', instruction_string)

    instruction, x_top_corner, y_top_corner, x_bottom_corner, y_bottom_corner = evaluated_expression[0]

    slice_a = slice(int(x_top_corner), int(x_bottom_corner)+1)
    slice_b = slice(int(y_top_corner), int(y_bottom_corner)+1)

    if instruction == "turn on":
        grid[slice_a, slice_b] += 1
    elif instruction == "turn off":
        grid[slice_a, slice_b] -= 1
        # grid[grid < 0] can be used here, but this is much faster
        grid[slice_a, slice_b][grid[slice_a, slice_b] < 0] = 0
    elif instruction == "toggle":
        grid[slice_a, slice_b] += 2

    return grid

grid_width = 1000
grid_height = 1000

lighting_instruction = input_reader.get_list("Day6/Day6_input.txt")
light_grid = numpy.zeros((grid_width, grid_height), dtype=numpy.int)

for instruction in lighting_instruction:
    do_instruction(light_grid, instruction)

print(light_grid.sum())
Пример #7
0

# Gets the present string and turns it into an integer list of dimensions
def get_present_dimensions(present_string):
    dimensions = present_string.split("x")
    return list(map(int, dimensions))


# Get the minimum perimeter
def get_minimum_perimeter(dimensions):
    # Sort the dimensions and remove the highest side (last) from he list
    dimensions.sort()
    minimum_dimensions = dimensions[:-1]
    return sum(minimum_dimensions*2)


# Calculate the volume (which is equal to the length of the bow)
def get_volume(dimensions):
    return dimensions[0]*dimensions[1]*dimensions[2]

# First get all the present dimensions from the input
present_strings = input_reader.get_list("Day2/Day2_input.txt")
present_dimensions = list(map(get_present_dimensions, present_strings))

# Now calculate the total length
ribbon_lengths = list(map(get_minimum_perimeter, present_dimensions))
bow_lengths = list(map(get_volume, present_dimensions))
total_length = sum(ribbon_lengths) + sum(bow_lengths)

print(total_length)