Пример #1
0
from helpers.helpers import read_input

input = read_input("input_files/aoc_2_input.txt")


def is_correct(rule, password):
    char = rule.split(" ")[1]
    rule_length = rule.split(" ")[0].split("-")
    return password.count(char) >= int(
        rule_length[0]) and password.count(char) <= int(rule_length[1])


def is_correct_official(rule, password):
    char = rule.split(" ")[1]  # read the char
    char_position = rule.split(" ")[0].split("-")  # split the rule on `-`
    char_position = list(map(int, char_position))  # parse to integers
    return (password[char_position[0]] == char and password[char_position[1]]
            != char) or (password[char_position[0]] != char
                         and password[char_position[1]] == char)


correct_password = 0
for line in input:
    value = line.split(":")
    if is_correct(value[0], value[1]):
        correct_password += 1
print("Result 1: {}".format(correct_password))

correct_password_official = 0
for line in input:
    value = line.split(":")
Пример #2
0
from helpers.helpers import read_input

input = read_input("input_files/aoc_5_input.txt", '\n')


def parse_row(row_data):
    rows = list(range(128))
    for char in row_data:
        if char == "B":
            rows = rows[round(len(rows) / 2):]
        if char == "F":
            rows = rows[:round(len(rows) / 2)]
    return rows[0]


def parse_column(column_data):
    rows = list(range(8))
    for char in column_data:
        if char == "R":
            rows = rows[round(len(rows) / 2):]
        if char == "L":
            rows = rows[:round(len(rows) / 2)]
    return rows[0]


def calculate_seat_id(row, column):
    return row * 8 + column


highest_seat_id = 0
seat_ids = []
Пример #3
0
from helpers.helpers import read_input
adapters = read_input("input_files/aoc_10_input.txt", '\n')

adapters = [int(x) for x in adapters]
adapters.sort()


def get_device_jolts(adapters):
    return adapters[-1] + 3


adapter_counter = [1, 0, 1]

for index in range(0, len(adapters)):
    for lookahead in range(1, 4):
        if len(adapters) > (
                index + 1) and adapters[index] + lookahead == adapters[index +
                                                                       1]:
            adapter_counter[lookahead - 1] = adapter_counter[lookahead - 1] + 1
            break

print("Result 2: {}".format(adapter_counter[0] * adapter_counter[2]))
Пример #4
0
from helpers.helpers import read_input
input = read_input("input_files/aoc_7_test.txt", '\n')


class Tree:
    def __init__(self, name, amount=1):
        self.name = name
        self.amount = amount
        self.childs = list()

    def __repr__(self):
        """"Prints the bag with it children in it"""
        return "Bag: {} {} with {}".format(self.amount, self.name, self.childs)

    def add_child(self, child):
        """Adds a child to this tree object"""
        self.childs.append(child)

    def find_all_by_name(self, name, is_root_object=True):
        """"Recursively looks for a Tree object with the given name"""
        print("Looking for: {}".format(name))
        found = list()
        if self.name == name and not is_root_object:
            print("Found self: {}".format(self))
            found.append(self)
        for child in self.childs:
            found.extend(child.find_all_by_name(name))
            if child.name == name:
                print("Found child: {}".format(child))
                found.append(child)
        return found
Пример #5
0
from helpers.helpers import read_input
cypher = read_input("input_files/aoc_9_input.txt", '\n')

# clean data to int array
cypher = [int(x) for x in cypher]

buffer_length = 25
buffer = cypher[:buffer_length]

result1 = 0


def valid(cypher, index, buffer):
    to_find = cypher[index]

    for x_index, x in enumerate(buffer):
        for y_index, y in enumerate(buffer):
            if y_index is not x_index and int(x + y) == int(to_find):
                return True
    return False


def find_numbers(cypher, start):
    sum_array = []
    for index in range(start, len(cypher)):
        sum_array.append(cypher[index])
        if int(sum(sum_array)) == int(result1):
            return sum_array
        if sum(sum_array) > result1:
            sum_array.clear()
            break