Esempio n. 1
0
        return len(self.parents())

    def __repr__(self):
        return f"ObjectInSpace('{self.name}')"


objects_in_space = dict()


def find_object(o):
    if o not in objects_in_space:
        objects_in_space[o] = ObjectInSpace(o)
    return objects_in_space[o]


for line in load_input(6):
    a, b = line.strip().split(')')
    find_object(a).add_child(find_object(b))

orbits = 0
for o in objects_in_space.values():
    orbits += o.num_parents

print("Part 1", orbits)

assert orbits == 158090

you = objects_in_space['YOU']
san = objects_in_space['SAN']
you_parents = you.parents(True)[::-1]
san_parents = san.parents(True)[::-1]
Esempio n. 2
0
    sorted_crossings = sorted(crossings, key=lambda x: x[1])
    return sorted_crossings[0][1]


def do(data, show=False):
    wire_data = parse_wire_data(data)
    wire_paths, minmax = convert_to_lines(wire_data)
    if show:
        draw_wires(wire_paths, minmax, 10)
    return search_crossings(wire_paths[0], wire_paths[1])


if __name__ == '__main__':
    with open("AoC-2019-test-3-0.txt") as f:
        c0 = do(f.readlines())
        assert closest_crossing(c0) == 159
        assert shortest_length(
            c0) == 610, f"Expected 610, got {shortest_length(c0)}"

    with open("AoC-2019-test-3-1.txt") as f:
        c1 = do(f.readlines())
        assert closest_crossing(c1) == 135
        assert shortest_length(
            c1) == 410, f"Expected 610, got {shortest_length(c1)}"

    crossings = do(load_input(3), True)
    d = closest_crossing(crossings)
    assert d != 76
    print(f"Part 1: {d}")
    print(f"Part 2: {shortest_length(crossings)}")
Esempio n. 3
0
def init_program():
    return [int(x) for x in load_input(5, readlines=False).split(',')]
Esempio n. 4
0
#!/usr/bin/env python3
"""Solution for Advent of Code challenge 2019 - Day 1"""

__author__ = "Serge Beaumont"
__date__ = "December 2019"

from norvigutils import load_input

data = [int(x) for x in load_input(1)]

total = sum([x // 3 - 2 for x in data])

print("Part 1:", total)

assert total == 3305115
Esempio n. 5
0
def init_program(noun, verb):
    data = [int(x) for x in load_input(2, readlines=False).split(',')]
    data[1] = noun
    data[2] = verb
    return data
Esempio n. 6
0
    def layer_to_lines(self, layer):
        lines = list()
        for i in range(self.height):
            lines.append(cat(layer[i * self.width:i * self.width +
                                   self.width]))
        return lines


def print_layer(layer):
    for line in layer:
        print(cat(['\u2588' if c == '1' else ' ' for c in line]))


def do(data, width, height):
    picture = Picture.from_string(data, width, height)
    return picture.rendered


if __name__ == '__main__':
    test_render = do('0222112222120000', 2, 2)
    assert test_render == ['01', '10']
    pic = do(load_input(8, False), PIC_WIDTH, PIC_HEIGHT)

    print("\nRendered picture\n")
    for line in pic:
        print(line)

    print("\nPrinted picture\n")
    print_layer(pic)
Esempio n. 7
0
def load_program():
    return [int(x) for x in load_input(7, False).split(',')]