예제 #1
0
def part2():
    mem = flatten(parse("02.txt"))
    for noun in range(0, 99):
        for verb in range(0, 99):
            output = get_output(mem, noun, verb)
            if output == 19690720:
                pp(noun, verb, output)
                pp(100 * noun + verb)
예제 #2
0
def part1():
    numbers = flatten(parse("01.txt"))
    total = 0
    for number in numbers:
        fuel = number // 3 - 2
        pp(number, fuel)
        total += fuel
    print(total)
예제 #3
0
def part1():
    mem = flatten(parse("02.txt"))
    mem[1] = 12
    mem[2] = 2
    # pp(mem)
    eip = 0
    while eip is not None:
        eip = step(mem, eip)
        # pp(eip, mem)
    pp(mem[0])
예제 #4
0
def read_literal(packet: List[int]) -> Tuple[List[int], int]:
    # Read the literal value
    literal = []
    for c in chunk(5, packet):
        literal.append(c[1:])
        if c[0] == "0":
            break
    packet = packet[len(literal) * 5:]
    literal = int("".join(flatten(literal)), 2)

    return packet, literal
예제 #5
0
def part2():
    print("hello")
    numbers = flatten(parse("01.txt"))
    total = 0
    for number in numbers:
        subtotal = 0
        fuel = number
        fuel = fuel // 3 - 2
        while fuel > 0:
            subtotal += fuel
            fuel = fuel // 3 - 2

        pp(number, subtotal)
        total += subtotal
    print(total)
예제 #6
0
    Numbers(),
])

your_ticket = chunks[1][0]
tickets = chunks[2]
rules = {
    m[0]: [range(int(m[1]),
                 int(m[2]) + 1),
           range(int(m[3]),
                 int(m[4]) + 1)]
    for m in chunks[0]
}

# Part 1

all_rule_values = flatten(rules.values())


def is_valid_for_some_field(v):
    return any(v in x for x in all_rule_values)


aoc.p1(
    sum(
        sum(0 if is_valid_for_some_field(v) else v for v in t)
        for t in tickets))

# Part 2

tickets = [t for t in tickets if all(is_valid_for_some_field(v) for v in t)]
field_indices = {r: set(range(len(tickets[0]))) for r in rules.keys()}
예제 #7
0
    )


for n in numbers_drawn:
    bingo_cards = [
        [[b[r][c] if b[r][c] != n else None for c in range(5)] for r in range(5)]
        for b in bingo_cards
    ]
    finished_cards = list(filter(is_bingo, bingo_cards))

    if finished_cards:
        last_drawn = n
        bingo = finished_cards[0]
        break

unmarked = sum(filter(None, flatten(bingo)))
aoc.p1(unmarked * last_drawn)

# Part 2

bingo_cards = chunk(5, list(filter(None, data.numbers_by_line()[1:])))

for n in numbers_drawn:
    bingo_cards = [
        [[b[r][c] if b[r][c] != n else None for c in range(5)] for r in range(5)]
        for b in bingo_cards
    ]
    unfinished_cards = list(filter(lambda x: not is_bingo(x), bingo_cards))

    if len(bingo_cards) == 1 and len(unfinished_cards) == 0:
        last_drawn = n
예제 #8
0
def part1():
    pp(
        best_sequence(
            [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]))

    pp(
        best_sequence([
            3,
            23,
            3,
            24,
            1002,
            24,
            10,
            24,
            1002,
            23,
            -1,
            23,
            101,
            5,
            23,
            23,
            1,
            24,
            23,
            23,
            4,
            23,
            99,
            0,
            0,
        ]))
    pp(
        best_sequence([
            3,
            31,
            3,
            32,
            1002,
            32,
            10,
            32,
            1001,
            31,
            -2,
            31,
            1007,
            31,
            0,
            33,
            1002,
            33,
            7,
            33,
            1,
            33,
            31,
            31,
            1,
            32,
            31,
            31,
            4,
            31,
            99,
            0,
            0,
            0,
        ]))
    mem = flatten(parse("07.txt"))
    pp(best_sequence(mem))
예제 #9
0
def part2():
    mem = flatten(parse("05.txt"))
    eip = 0
    while eip is not None:
        eip = step(mem, eip, input=5)
예제 #10
0
from aoc import AOC, Position, flatten

aoc = AOC(year=2020, day=17)
data = aoc.load()

# Part 1

lines = data.lines()
active = set(
    flatten([[
        Position(x - len(l) // 2, y - len(lines) // 2, 0)
        for x, c in enumerate(l) if c == "#"
    ] for y, l in enumerate(lines)]))
seen = set(
    flatten([[
        Position(x - len(l) // 2, y - len(lines) // 2, 0)
        for x, c in enumerate(l)
    ] for y, l in enumerate(lines)]))
seen.update(flatten([[a for a in c.adjacent()] for c in seen]))


def cycle(active, seen):
    next_active, next_seen = set(), set(seen)
    for c in seen:
        adj = c.adjacent()
        next_seen.update(adj)
        if c in active and 2 <= len([1 for x in adj if x in active]) <= 3:
            next_active.add(c)
        elif c not in active and len([1 for x in adj if x in active]) == 3:
            next_active.add(c)
    return (next_active, next_seen)
예제 #11
0
        base_id, offset, rotation, direction = position
        if base_id == 0:
            positions[scanner_id] = ScannerPosition(offset, offset, rotation,
                                                    direction, base_id)
        else:
            positions[scanner_id] = ScannerPosition(
                offset_relative_to_zero(offset, base_id),
                offset,
                rotation,
                direction,
                base_id,
            )

# Part 1
beacons = set(
    flatten([position_relative_to_zero(b, id) for b in scanners[id]]
            for id in positions))
aoc.p1(len(beacons))

# Part 2

maximum_distance = 0
for ida in positions:
    for idb in positions:
        position_a = position_relative_to_zero(positions[ida].offset,
                                               positions[ida].base)
        position_b = position_relative_to_zero(positions[idb].offset,
                                               positions[idb].base)
        maximum_distance = max(maximum_distance,
                               manhattan(position_a, position_b))
aoc.p2(maximum_distance)
예제 #12
0
def read_binary(hexa: str) -> List[int]:
    # Convert hexadecimal to binary, padding all hex values to 4 digit binary vlaues
    return flatten(
        [list(str(bin(int(c, 16))[2:].rjust(4, "0"))) for c in hexa])