Example #1
0
        for adapter in adapters:
            count[adapter] = 0
            if adapter - 1 in count:
                count[adapter] += count[adapter - 1]
            if adapter - 2 in count:
                count[adapter] += count[adapter - 2]
            if adapter - 3 in count:
                count[adapter] += count[adapter - 3]

        return count[max(count)]


if __name__ == '__main__':
    file_reader = FileReader('./input.txt')
    adapters = file_reader.to_int_list()

    bag = Bag(adapters)
    print(
        f"⚡️ Counting adapters with 1 difference : {bag.count_jolt_differences(1)}"
    )
    print(
        f"⚡️ Counting adapters with 3 difference : {bag.count_jolt_differences(3)}"
    )

    print(
        f"Multiplied : {bag.count_jolt_differences(1) * bag.count_jolt_differences(3)}"
    )

    print(f"Possible ways : {bag.count_possible_ways_v2()}")
            if matching_value in self.rows:
                return [entry, matching_value]

        return []

    def findMatchingTriplet(self) -> List[int]:
        VALUE_TO_FIND = 2020
        for index, initial_entry in enumerate(self.rows):
            remaining_entries = self.rows[index + 1:]
            for second_entry in remaining_entries:
                matching_value = VALUE_TO_FIND - initial_entry - second_entry

                if matching_value in self.rows:
                    return [initial_entry, second_entry, matching_value]

        return []

    def get_result(self) -> int:
        return reduce((lambda x, y: x * y), self.findMatchingPair())

    def get_result_for_triplet(self) -> int:
        return reduce((lambda x, y: x * y), self.findMatchingTriplet())


if __name__ == '__main__':
    file_reader = FileReader('./input')
    list_of_expenses = file_reader.to_int_list()
    expense_book = ExpenseBook(list_of_expenses)
    print(expense_book.get_result())
    print(expense_book.get_result_for_triplet())
Example #3
0
def find_contiguous_numbers(sequence: List[int], value: int) -> List[int]:
    for start_index, v in enumerate(sequence):
        end_index = start_index

        value_is_reached_or_passed = False
        while not value_is_reached_or_passed:
            end_index += 1
            subsequence = sequence[start_index:end_index]

            total = sum(subsequence)

            if total >= value:
                value_is_reached_or_passed = True

        if total == value:
            return subsequence


if __name__ == '__main__':
    file_reader = FileReader('./input')
    instructions = file_reader.to_int_list()

    invalid_key = find_weak_sequence(instructions, 25)
    print(f"Invalid Code 🔑 : {invalid_key}")
    min = min(find_contiguous_numbers(instructions, invalid_key))
    max = max(find_contiguous_numbers(instructions, invalid_key))
    print(f"Min value âž– : {min}")
    print(f"Max value âž• : {max}")
    print(f"Total 🧮 : {min + max}")