Beispiel #1
0
def search(program):
    stack = [('', program)]
    grid = dict()
    oxygen = None
    while stack:
        sequence, p = stack.pop(0)
        c = coordinate(sequence)
        #draw(grid, X=c)

        # Update map
        grid[c] = '.'

        # Run sequence
        for d in 'NSWE':
            ds = sequence + d
            dc = coordinate(ds)

            if dc in grid:
                continue

            vm = Intcode(p)
            vm.write(CMD[d])
            status = vm.run()

            if status == 0:
                grid[dc] = '#'
            elif status == 1:
                # Take new step
                stack.append((ds, vm.program))
            elif status == 2:
                oxygen = ds
                stack.append((ds, vm.program))
            else:
                print("bad status:", status)
    return grid, oxygen
Beispiel #2
0
def amplifier(program, settings):
    i = 0
    for setting in settings:
        amp = Intcode(program.copy())
        outputs = amp.run(data=[i, setting])
        i = outputs
    return outputs
Beispiel #3
0
def amplifier_feedback(program, settings):
    amps = [Intcode(program.copy()) for _ in settings]
    for amp, setting in zip(amps, settings):
        amp.write(setting)

    last_e = None
    amps[0].write(0)
    i = 0
    for index, amp in cycle(enumerate(amps)):
        #print(i, index, len([a for a in amps if not a.is_terminated()]))
        output = amp.run()
        if output is not None:
            amps[(index + 1) % len(amps)].write(output)
            if index == len(settings) - 1:
                last_e = output
        if all(a.is_terminated() for a in amps):
            break
        i += 1
    #print(last_e)
    return last_e
Beispiel #4
0
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)
Beispiel #5
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))
Beispiel #6
0
# 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
Beispiel #7
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))
Beispiel #8
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')
Beispiel #9
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 = []
Beispiel #10
0
 def __init__(self):
     self.intcode = Intcode(program)
     self.latest = None
     self.position = (0, 0)
     self.grid = dict()
Beispiel #11
0
class State:
    def __init__(self):
        self.intcode = Intcode(program)
        self.latest = None
        self.position = (0, 0)
        self.grid = dict()

    def go(self, direction):
        if direction in DIRECTIONS:
            self.grid[self.position] = 'visited'
            dx, dy = DIRECTIONS[direction]
            x, y = self.position
            self.position = x + dx, y + dy

        data = direction
        game.intcode.write_ascii(data)
        game.intcode.write(10)
        game.latest = self.intcode.read_ascii()
        self.update_seen()
        print(self.latest)

    def update_seen(self):
        # Update seen rooms
        nav = parse(self.latest).get('navigation') or []
        x, y = self.position
        for direction in nav:
            if direction in DIRECTIONS:
                dx, dy = DIRECTIONS[direction]
                p = x + dx, y + dy
                if p not in self.grid:
                    self.grid[p] = 'seen'

    def take(self, item):
        data = "take " + request.json
        self.intcode.write_ascii(data)
        self.intcode.write(10)
        self.latest = game.intcode.read_ascii()
        print(self.latest)

    def drop(self, item):
        data = "drop " + request.json
        self.intcode.write_ascii(data)
        self.intcode.write(10)
        self.latest = game.intcode.read_ascii()
        print(self.latest)

    def chart(self):
        w, h = 16, 16

        def tojs(p, **kwargs):
            x, y = p
            d = dict(x=x + w // 2, y=y + h // 2)
            d.update(kwargs)
            return d

        rooms = []
        for p, value in self.grid.items():
            rooms.append(tojs(p, value=value))
        return dict(rooms=rooms, position=tojs(self.position))

    def inventory(self):
        self.intcode.write_ascii('inv\n')
        raw = self.intcode.read_ascii()
        return parse_inventory(raw)

    def json(self):
        j = parse(game.latest)
        j['inventory'] = self.inventory()
        return j
Beispiel #12
0
        game.latest = game.intcode.read_ascii()
        game.update_seen()

    return jsonify(game.json())


@app.route('/take', methods=['POST'])
def take():
    item = request.json
    game.take(item)
    return jsonify(game.json())


@app.route('/drop', methods=['POST'])
def drop():
    item = request.json
    game.drop(item)
    return jsonify(game.json())


@app.route('/go', methods=['POST'])
def go():
    direction = request.json
    game.go(direction)
    return jsonify(game.json())


if __name__ == "__main__":
    term = Terminal(Intcode(program))
    term.run()