Example #1
0
File: 11.py Project: vidstige/aoc
def robot():
    vm = Intcode(load(11))
    grid = dict()
    di = 0
    p = (0, 0)

    grid[p] = 1
    while not vm.is_terminated():
        camera = grid.get(p, 0)
        vm.write(camera)
        color = vm.run()
        turn_right = vm.run()
        if color is not None:
            grid[p] = color
        di = (di + (1 if turn_right == 1 else -1)) % len(DIRECTIONS)
        dx, dy = DIRECTIONS[di]
        x, y = p
        p = x + dx, y + dy

    print(len(grid))
    render(grid)
Example #2
0
def main():
    program = load(day=23)
    machines = [Intcode(program) for _ in range(50)]
    queues = [[] for _ in range(50)]
    polling = [False] * 50

    nat = None

    for i, machine in enumerate(machines):
        machine.write(i)

    while True:
        for i, (machine, queue) in enumerate(zip(machines, queues)):
            if queue:
                ix, iy = queue.pop(0)
                machine.write(ix)
                machine.write(iy)
            else:
                machine.write(-1)

            a = machine.run()
            polling[i] = a is None
            if a is not None:
                x = machine.run()
                y = machine.run()
                if a == 255:
                    #print("sending {}, {} to NAT")
                    nat = (x, y)
                else:
                    #print("sending {}, {} to {}".format(x, y, a))
                    queues[a].append((x, y))

            if all(polling) and all(not q for q in queues):
                x, y = nat
                print(x, y)
                queues[0].append((x, y))
Example #3
0
File: 21.py Project: vidstige/aoc
# ABC
##.#.##

# A! AND B AND !C

#@ABCDEFGHI
####.#.##.#.####

#   J
#####...#########

#  J
#####..#.########

vm = Intcode(program=load(day=21))

# ABCDEFGHI
#@   @   @

# Jump if any space (unless !A AND B AND !C) and double landing spaces ok
# !(A AND AND B C OR) AND D AND (H OR E)

#ABCDEFGHI
#.#.##.#.####

program = """
NOT T T
AND A T
AND B T
AND C T
Example #4
0
    yield x - 1, y
    yield x, y + 1
    yield x, y - 1


def intersections(grid):
    for x, y in grid:
        if all(n in grid for n in neighbours(x, y)):
            yield x, y


def alignment(grid):
    return sum(x * y for x, y in intersections(grid))


program = load(day=17)
vm = Intcode(program)

grid = set()
x, y = 0, 0
while not vm.is_terminated():
    c = vm.run()
    if c == 10:
        x, y = 0, y + 1
    if c == 35:
        grid.add((x, y))
    if c in (35, 46):
        x += 1

print(alignment(grid))
Example #5
0
File: 15.py Project: vidstige/aoc
            elif status == 2:
                oxygen = ds
                stack.append((ds, vm.program))
            else:
                print("bad status:", status)
    return grid, oxygen


def oxygnize(grid, oxygen):
    t = 0
    grid[coordinate(oxygen)] = 'O'

    while any(v == '.' for v in grid.values()):
        draw(grid)
        # find all coordinates with oxygen
        o = [key for key, value in grid.items() if value == 'O']
        # spread
        for c in o:
            x, y = c
            for d in 'NSWE':
                cc = x + DX[d], y + DY[d]
                if grid[cc] == '.':
                    grid[cc] = 'O'
        t += 1
    print(t)


grid, oxygen = search(load(day=15))
print("distance to oxygen:", len(oxygen))
draw(grid, X=coordinate(oxygen))
oxygnize(grid, oxygen)
Example #6
0
def reset_state():
    global vm
    vm = Intcode(program=load(day=13))
    vm.program[0] = 2  # insert coin
    vm.write(0) # dont touch joy first frame
    return jsonify('ok')
Example #7
0
from flask import Flask, render_template, jsonify, request

from intvm import Intcode, load
from draw import draw

vm = Intcode(program=load(day=13))
vm.program[0] = 2  # insert coin
ball = None
paddle = None

app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('13.html')

@app.route('/state', methods=('DELETE',))
def reset_state():
    global vm
    vm = Intcode(program=load(day=13))
    vm.program[0] = 2  # insert coin
    vm.write(0) # dont touch joy first frame
    return jsonify('ok')

@app.route('/state', methods=('POST', 'GET'))
def state():
    # manual play
    #vm.write(request.json)
    
    score = None
    tiles = []
Example #8
0
from intvm import Intcode, load, Terminal

# Everything except. Also leave space shuttle, gigantic magnet and photons
# - asterisk
# - cake
# - food ration
# - sand

DIRECTIONS = {
    'north': (0, -1),
    'south': (0, 1),
    'west': (-1, 0),
    'east': (1, 0),
}

program = load(day=25)


def parse(raw):
    header = None
    items = []
    navigation = None
    description = []
    head = description
    for line in raw.splitlines()[1:]:
        if not line:
            head = None
        if head is not None:
            head.append(line.lstrip(' -'))
        if line.startswith('=='):
            header = line