示例#1
0
def part_2(data: aoc.Data):
    data = data.rstrip()
    blocks = []
    for i in range(128):
        row = knot_hash(f'{data}-{i}')
        blocks.append(''.join([bin(int(x, 16))[2:].zfill(4) for x in row]))

    processed = set()
    components = []
    for x, y in product(range(128), range(128)):
        if (x, y) in processed or blocks[y][x] == '0':
            continue
        frontier = deque([(x, y)])
        seen = set()
        while frontier:
            x, y = frontier.popleft()
            if (x, y) in seen:
                continue
            seen.add((x, y))
            neighbours = [
                (dx, dy)
                for (dx, dy) in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
                if (dx, dy) not in seen
                and 0 <= dx < 128
                and 0 <= dy < 128
                and blocks[dy][dx] == '1'
            ]
            frontier.extend(neighbours)
        components.append(seen)
        processed.update(seen)

    return len(components)
示例#2
0
def part_1(data: aoc.Data):
    data = data.rstrip()
    blocks = ''
    for i in range(128):
        row = knot_hash(f'{data}-{i}')
        blocks += ''.join([bin(int(x, 16))[2:].zfill(4) for x in row])
    return blocks.count('1')
示例#3
0
def part_2(data: aoc.Data):
    steps = data.rstrip().split(',')
    pos = Cube(0, 0, 0)
    furthest = 0
    for step in steps:
        pos += directions[step]
        furthest = max(furthest, pos.distance())
    return furthest
示例#4
0
def part_2(data: aoc.Data):
    return len([
        x for x in data.splitlines()
        if len(x.split()) == len([
            w for w in x.split()
            if ''.join(sorted(w)) not in {''.join(sorted(w2)) for w2 in set(x.split()) - {w}}
        ])
    ])
示例#5
0
def part_1(data: aoc.Data):
    lower, upper = map(int, data.split('-'))
    matches = 0
    for password in range(lower, upper + 1):
        p = str(password)
        dupes = re.findall(r'(.)\1', p)
        if dupes and sorted(p) == list(p):
            matches += 1
    return matches
示例#6
0
def part_2(data: aoc.Data):
    seen = []
    actions = data.split(',')
    target = 1_000_000_000
    programs = deque(ascii_lowercase[:16])
    for i in range(target):
        programs = dance(programs, actions)
        prog = ''.join(programs)
        if prog in seen:
            return seen[(target - 1) % i]
        seen.append(prog)
示例#7
0
def part_2(data: aoc.Data):
    lower, upper = map(int, data.split('-'))
    matches = 0
    for password in range(lower, upper + 1):
        p = str(password)
        dupes = re.findall(r'(.)\1', p)
        larger = re.findall(r'(.)\1{2,}', p)
        # must have a dupe which is not a part of larger group
        if set(dupes) - set(larger) and sorted(p) == list(p):
            matches += 1
    return matches
示例#8
0
def part_1(data: aoc.Data):
    wires = []
    for wire in data.splitlines():
        wires.append([])
        pos = Point()
        for segment in wire.split(','):
            direction = segment[0]
            length = int(segment[1:])
            delta = move[direction] * length
            wires[-1].append(Wire(pos, pos + delta))
            pos += delta

    intersections = set()
    for a in wires[0]:
        for b in wires[1]:
            if intersect := a.intersect(b):
                intersections.add(intersect)
示例#9
0
def part_1(data: aoc.Data):
    size = 5 if data.is_example else 16
    actions = data.split(',')
    programs = deque(ascii_lowercase[:size])
    programs = dance(programs, actions)
    return ''.join(programs)
示例#10
0
def part_1(data: aoc.Data):
    return len([
        x for x in data.splitlines()
        if len(x.split()) == len(set(x.split()))
    ])
示例#11
0
def part_1(data: aoc.Data):
    puzzle = [int(x) for x in data.rstrip()] + [int(data[0])]
    return sum(a for a, b in zip(puzzle, puzzle[1:]) if a == b)
示例#12
0
def part_2(data: aoc.Data):
    puzzle = [int(x) for x in data.rstrip()]
    return sum(a for i, a in enumerate(puzzle)
               if a == puzzle[(i + len(puzzle) // 2) % len(puzzle)])
示例#13
0
def part_1(data: aoc.Data):
    steps = data.rstrip().split(',')
    pos = Cube(0, 0, 0)
    for step in steps:
        pos += directions[step]
    return pos.distance()