Esempio n. 1
0
def main(filename):

    tickets = read_file(filename)
    seats = [get_seat(x) for x in tickets]
    plain = np.zeros((n_rows, n_cols))

    for s in seats:
        r, c = s
        plain[r][c] = 1

    in_bounds = lambda x, y: 0 <= x < n_rows and 0 <= y < n_cols

    for i in range(n_rows):
        for j in range(n_cols):

            if plain[i][j] != 0:
                continue

            res = True
            for k in range(-1, 2, 1):
                for w in range(-1, 2, 1):
                    if (k, w) != (0, 0) and in_bounds(
                            i + k, j + w) and plain[i + k][j + w] == 0:
                        res = False

            if res is True:
                return to_id(i, j)
Esempio n. 2
0
def main(file_name):
    instructions = read_file(file_name)

    # north/south ; west/east
    boat = np.array([0, 0])
    waypoint = np.array([10, 1])

    for action, value in instructions:
        if action == 'N':
            waypoint[1] += value
        elif action == 'S':
            waypoint[1] -= value
        elif action == 'E':
            waypoint[0] += value
        elif action == 'W':
            waypoint[0] -= value
        elif action == 'L':
            waypoint = rotate(waypoint, (0, 0), value)
        elif action == 'R':
            waypoint = rotate(waypoint, (0, 0), (-value))
        elif action == 'F':
            boat[0] += waypoint[0] * value
            boat[1] += waypoint[1] * value

        print(action, value)
        print("waypoint : ", waypoint)
        print("boat : ", boat)

    print("taxi cab: ", taxi_cab_dist(boat, [0, 0]))
Esempio n. 3
0
def from_cycle(filename):
    orginal_program = read_file(filename)
    _acc, is_cycle, pred, statement = has_cycle(orginal_program)

    if is_cycle:
        cycle = enum_cycle(statement, orginal_program, pred)
        #print("brute force: ", len(list(filter(lambda x: x.instruction in {JMP, NOP}, orginal_program))))
        #print("cycle: ", len(list(filter(lambda x: x.instruction in {JMP, NOP}, cycle))))
        return find_acc(orginal_program, cycle)
Esempio n. 4
0
def main2(filename):
    content = read_file(filename)
    content.sort()
    for x in content:
        for y in content:
            if x == y:
                continue
            z = x + y
            if z <= 2020:
                w = 2020 - z
                if bin_search(content, w) != -1:
                    return w * x * y
Esempio n. 5
0
def main(filename):

    content = read_file(filename)

    for x in content:
        for y in content:
            if x == y:
                continue
            for z in content:
                if x == z and y == z:
                    continue
                if x + z + y == 2020:
                    return x * z * y
Esempio n. 6
0
def main3(filename):
    content = read_file(filename)
    m = {}
    for x in content:
        for y in content:
            if x == y:
                continue
            z = x + y
            if z <= 2020:
                w = 2020 - z
                m[w] = (x, y)
    for n in content:
        if n in m:
            x, y = m[n]
            return n * x * y
Esempio n. 7
0
def main(file_name):
    content = read_file(file_name)
    content.append(0)
    content.append(max(content) + 3)
    content_set = set(content)

    paths = [0] * (max(content_set) + 1)
    paths[0] = 1

    for i in range(1, max(content_set) + 1):
        for j in range(1, 4, 1):
            if i - j in content_set:
                paths[i] += paths[i - j]

    print(str(paths[-1]))
Esempio n. 8
0
def find_numbers(file_name):

    invalid_number = find_invalid_number(file_name)
    content = read_file(file_name)

    for i in range(len(content)):
        _sum = content[i]
        numbers = [content[i]]
        for j in range(i + 1, len(content), 1):
            k = content[j]
            _sum += k
            numbers.append(k)
            if _sum == invalid_number and len(numbers) >= 2:
                return numbers
            if _sum > invalid_number:
                break
    return []
Esempio n. 9
0
from puzzle1 import tree_count
from puzzle1 import read_file


def multiple_trees(lst_splope, map_basis):
    acc = 1
    for slope in lst_splope:
        acc *= tree_count(slope[0], slope[1], map_basis)
    return acc


if __name__ == "__main__":
    map_basis = read_file("input-3.txt")
    lst_splope = {(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)}
    print(multiple_trees(lst_splope, map_basis))
Esempio n. 10
0
def brute_force(filename):
    orginal_program = read_file("input-8.txt")
    return find_acc(orginal_program, orginal_program)