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


def fuel_required_for_module(mass, total=0, recursive=True):
    required_for_this_mass = max(mass // 3 - 2, 0)
    total += required_for_this_mass
    if recursive and required_for_this_mass > 0:
        return fuel_required_for_module(required_for_this_mass, total)
    return total


def total_fuel_requirement_recursive(masses_of_modules):
    return _total_fuel_requirement(masses_of_modules, True)


def total_fuel_requirement_not_recursive(masses_of_modules):
    return _total_fuel_requirement(masses_of_modules, False)


def _total_fuel_requirement(masses_of_modules, recursive):
    return sum(
        map(lambda mass: fuel_required_for_module(mass, recursive=recursive),
            masses_of_modules))


if __name__ == '__main__':
    _masses_of_modules = read_line_separated_list("masses.txt", int)
    print(total_fuel_requirement_not_recursive(_masses_of_modules))
    print(total_fuel_requirement_recursive(_masses_of_modules))
def masses_of_modules():
    return read_line_separated_list("masses.txt", int)
Пример #3
0
from src.helpers import read_line_separated_list
import numpy as np
from matplotlib import pylab as pt


if __name__ == '__main__':
    image = read_line_separated_list("image.txt")[0]
    width = 25
    height = 6

    n_layers = len(image) // (width * height)

    layers = []
    n = 0

    min_num_zero = 1e9
    num_one, num_two = None, None

    for _ in range(n_layers):
        layers.append([])
        num_zero = 0
        for j in range(height):
            layers[-1].append(image[n:n+width])
            num_zero += layers[-1][-1].count("0")
            n += width

        if num_zero < min_num_zero:
            min_num_zero = num_zero
            num_one, num_two = 0, 0
            for row in layers[-1]:
                num_one += row.count("1")
Пример #4
0
def read_and_parse_orbit_input(file_name):
    return dict(
        read_line_separated_list(file_name, lambda s: s.split(")")[::-1]))
Пример #5
0
            if val:
                s += 2**i
        return s


move_one_step_in_direction = {
    "R": lambda i, j: (i, j + 1),
    "L": lambda i, j: (i, j - 1),
    "U": lambda i, j: (i - 1, j),
    "D": lambda i, j: (i + 1, j)
}

reverse_direction = {"R": "L", "L": "R", "U": "D", "D": "U"}

if __name__ == '__main__':
    inp = read_line_separated_list("bugs2.txt")
    grid_values = {}
    center = None
    for i, line in enumerate(inp):
        for j, c in enumerate(line):
            if c != "?":
                grid_values[(i, j)] = int(c == "#")
            else:
                center = (i, j)

    height = max(grid_values, key=lambda x: x[0])[0] + 1
    width = max(grid_values, key=lambda x: x[1])[1] + 1

    recursive_grid = RecursiveGrid(width, height, center)
    recursive_grid.add_level(0, grid_values)
    recursive_grid.print()
Пример #6
0
        for command_str in commands_iter:
            self.exec(command_str)

    def exec_all_n_times(self, commands_iter, n):
        self.exec_all(commands_iter)

        increment_multiplier_per_pass = self.increment
        first_card_after_initial_pass = self.first_card

        self.increment = pow(increment_multiplier_per_pass, n, self.size)
        self.first_card = first_card_after_initial_pass * (1 - self.increment) * modinv(1 - increment_multiplier_per_pass, self.size)


if __name__ == '__main__':
    deck = Deck(10007)
    commands_iter = read_line_separated_list("cards.txt")
    deck.exec_all(commands_iter)

    cards = list(deck)
    print(cards.index(2019))

    deck2 = Deck(119315717514047)
    n_passes = 101741582076661
    deck2.exec_all_n_times(commands_iter, n_passes)
    print(deck2.get_card(2020))