Ejemplo n.º 1
0
def get_valid_count(invalidation_fn):
    count = 0
    for line in load_data(4):
        words = line.strip().split(" ")
        if not invalidation_fn(words):
            count += 1
    return count
Ejemplo n.º 2
0
def dance(progs):
    if not MOVES:
        for move in load_data(16)[0].split(","):
            if move[0] == "s":
                MOVES.append(("s", int(move[1:])))
            elif move[0] == "x":
                pos1, pos2 = (int(i) for i in move[1:].split("/"))
                MOVES.append(("x", pos1, pos2))
            elif move[0] == "p":
                prog1, prog2 = move[1:].split("/")
                MOVES.append(("p", prog1, prog2))

    for move in MOVES:
        if move[0] == "s":
            progs = progs[-move[1]:] + progs[:-move[1]]
        elif move[0] == "x":
            pos1, pos2 = move[1:]
            progs[pos1], progs[pos2] = progs[pos2], progs[pos1]
        elif move[0] == "p":
            prog1, prog2 = move[1:]
            pos1 = progs.index(prog1)
            pos2 = progs.index(prog2)
            progs[pos1], progs[pos2] = progs[pos2], progs[pos1]

    return progs
Ejemplo n.º 3
0
def get_data():
    data = {}
    for line in load_data(7):
        bits = line.split(" ")
        name = bits[0]
        weight = int(bits[1][1:-1])
        children = sorted(n.strip(",") for n in bits[3:])
        data[name] = weight, children

    return data
Ejemplo n.º 4
0
def get_instructions():
    instructions = []

    def try_int(val):
        try:
            return int(val)
        except ValueError:
            return val

    for line in load_data(18):
        instructions.append(tuple(try_int(v) for v in line.split(" ")))

    return instructions
Ejemplo n.º 5
0
def part1():
    global GLOBAL_MAX
    x = 0
    y = 0
    for d in load_data(11)[0].split(","):
        dx, dy = DIRS[d]
        x += dx
        y += dy
        dist = hex_dist(x, y)
        if dist > GLOBAL_MAX:
            GLOBAL_MAX = dist

    return hex_dist(x, y)
Ejemplo n.º 6
0
def do_work():
    global GLOBAL_MAX
    cells = defaultdict(int)
    for line in load_data(8):
        this_name, action, val, _, that, cmp_op, cmp_val = line.split(" ")
        cmp_val = int(cmp_val)
        if CMP_FNS[cmp_op](cells[that], cmp_val):
            val = int(val)
            if action == "inc":
                cells[this_name] += val
            elif action == "dec":
                cells[this_name] -= val

            if cells[this_name] > GLOBAL_MAX:
                GLOBAL_MAX = cells[this_name]

    return max(cells.values())
Ejemplo n.º 7
0
def follow_path():
    path = load_data(19, strip=False)

    # path = [
    #     "     |          ",
    #     "     |  +--+    ",
    #     "     A  |  C    ",
    #     " F---|----E|--+ ",
    #     "     |  |  |  D ",
    #     "     +B-+  +--+ ",
    #  ]

    for i, c in enumerate(path[0]):
        if c == "|":
            curr = 0, i
            prev = -1, i
            break
    else:
        raise NoPathFoundError

    been = set()
    letters = []
    steps = 0

    while True:
        val = path[curr[0]][curr[1]]
        if val in string.ascii_letters:
            letters.append(val)
        elif val == " ":
            break
        steps += 1
        been.add(curr)
        next_pos = get_next_pos(path, been, curr, prev)
        prev = curr
        curr = next_pos
    return "".join(letters), steps
Ejemplo n.º 8
0
def get_data():
    return int(load_data(17)[0])
Ejemplo n.º 9
0
def get_data():
    return [[int(i) for i in line.split("\t") if i] for line in load_data(2)]
Ejemplo n.º 10
0
    while True:
        val = (val * mul) % MOD
        if val % mod == 0:
            yield val


def part1(a_start, b_start):
    a_gen = gen(a_start, MUL_A)
    b_gen = gen(b_start, MUL_B)
    count = 0
    for _ in range(40000000):
        if next(a_gen) & MASK == next(b_gen) & MASK:
            count += 1
    return count


def part2(a_start, b_start):
    a_gen = gen(a_start, MUL_A, 4)
    b_gen = gen(b_start, MUL_B, 8)
    count = 0
    for _ in range(5000000):
        if next(a_gen) & MASK == next(b_gen) & MASK:
            count += 1
    return count


if __name__ == "__main__":
    a, b = (int(line.rsplit(" ", 1)[1]) for line in load_data(15))
    print("Part 1: {}".format(part1(a, b)))
    print("Part 2: {}".format(part2(a, b)))
Ejemplo n.º 11
0
def get_data():
    return [int(line) for line in load_data(5)]
Ejemplo n.º 12
0
def get_data():
    return load_data(10)[0]
Ejemplo n.º 13
0
            indices = {inner, num - 1}

            if i == 0:  # corner
                pass
            elif i == 1:  # after
                indices.add(num - 2)
                indices.add(inner + 1)
            elif i == actual_side_length - 1:  # before
                indices.add(inner + 1)
            else:  # edge
                indices.add(inner + 1)
                indices.add(inner + 2)

            total = 0
            for index in indices:
                total += vals[index]

            if total > target:
                return total

            vals[num] = total

        n += actual_side_length
        sl += 0.5


if __name__ == "__main__":
    input_val = int(load_data(3)[0])
    print("Part 1: {}".format(part1(input_val)))
    print("Part 2: {}".format(part2(input_val)))
Ejemplo n.º 14
0
def get_grid():
    hashes = [knot_hash(load_data(14)[0] + "-" + str(i)) for i in range(128)]
    return [list(bin(int(h, 16))[2:].zfill(128)) for h in hashes]
Ejemplo n.º 15
0
def get_data():
    return load_data(9)[0]
Ejemplo n.º 16
0
def get_data():
    data = {}
    for line in load_data(13):
        layer, depth = line.split(": ")
        data[int(layer)] = int(depth)
    return data
Ejemplo n.º 17
0
def get_data():
    return [int(i) for i in load_data(6)[0].split("\t")]
Ejemplo n.º 18
0
def get_data():
    data = {}
    for line in load_data(12):
        val, _, neighbours = line.split(" ", 2)
        data[val] = {n for n in neighbours.split(", ")}
    return data