Example #1
0
from session import fetch

RAW_DATA = fetch(2020, 23)

result_part_1 = 0
result_part_2 = 0

def play_cups(seed, cup_count, turns):
    assert(sorted(seed) == list(range(1, max(seed)+1)))

    cups = [-1] + list(range(2, cup_count+1)) + [1]
    for cup, next_cup in zip(seed, seed[1:]):
        cups[cup] = next_cup
    if len(seed) < cup_count:
        cups[seed[-1]] = max(seed)+1
        cups[-1] = seed[0]
    else:
        cups[seed[-1]] = seed[0]

    min_cup = min(seed)
    current = seed[0]
    for _ in range(turns):
        p1 = cups[current]
        p2 = cups[p1]
        p3 = cups[p2]
        cups[current] = cups[p3]
        dest = current-1
        while True:
            if dest < min_cup:
                dest = cup_count
            if dest not in [p1, p2, p3]:
Example #2
0
from session import fetch

RAW_DATA = fetch(2020, 1)

INPUT = [int(i) for i in RAW_DATA.splitlines()]

found = False
for i in INPUT:
    if found:
        break
    for j in INPUT:
        if found:
            break
        if i + j == 2020:
            found = True
            print(f"PART I: {i * j}")

found = False
for i in INPUT:
    if found:
        break
    for j in INPUT:
        if found:
            break
        for k in INPUT:
            if found:
                break
            if i + j + k == 2020:
                found = True
                print(f"PART II: {i * j * k}")
Example #3
0
from session import fetch

RAW_DATA = fetch(2021, 1)

INPUT = [int(i) for i in RAW_DATA.splitlines()]

increases = 0
last = None
for v in INPUT:
    if not last:
        last = v
        continue
    if v > last:
        increases += 1
    last = v

print(f"PART I: {increases}")

increases = 0
last = None
for i, v in enumerate(INPUT):
    if not last:
        last = INPUT[i] + INPUT[i + 1] + INPUT[i + 2]
        continue
    try:
        INPUT[i + 2]
    except IndexError:
        continue
    current = INPUT[i] + INPUT[i + 1] + INPUT[i + 2]
    if current > last:
        increases += 1
Example #4
0
from session import fetch

RAW_DATA = fetch(2019, 3)

result_part_1 = 0
result_part_2 = 0


COORDS = {0: {}, 1: {}}


def follow(wire, stop_at=None):
    coord = [0, 0]
    steps = 0
    for p in RAW_DATA.splitlines()[wire].split(','):
        direction = p[0]
        length = int(p[1:])
        for i in range(length):
            steps += 1
            if direction == 'R':
                coord[1] += 1
            elif direction == 'L':
                coord[1] -= 1
            elif direction == 'U':
                coord[0] += 1
            else:
                coord[0] -= 1
            if stop_at and stop_at == tuple(coord):
                return steps
            COORDS[wire].setdefault(coord[0], [])
            COORDS[wire][coord[0]].append(coord[1])
Example #5
0
from session import fetch

RAW_DATA = fetch(2020, 8).splitlines()

result_part_1 = 0
result_part_2 = 0

ACCU = 0
LINE = 0
EXECUTED = {}


def execute(line, accu):
    command, arg = RAW_DATA[line].split()
    if command == 'nop':
        line += 1
        return (line, accu)
    number = int(arg[1:])
    if command == 'acc':
        line += 1
        if arg[0] == '+':
            accu += number
        else:
            accu -= number
        return (line, accu)
    if command == 'jmp':
        if arg[0] == '+':
            line += number
        else:
            line -= number
        return (line, accu)