示例#1
0
文件: day11.py 项目: iKevinY/advent
def simulate_robot(tape, start=0):
    grid = {}
    grid[0, 0] = start

    facing = 0
    x, y = 0, 0

    inputs = [start]
    robot = emulate(tape[:], inputs)

    try:
        while True:
            grid[x, y] = next(robot)
            if next(robot) == 0:
                facing = (facing - 1) % 4
            else:
                facing = (facing + 1) % 4

            if facing == 0:
                y += 1
            elif facing == 1:
                x += 1
            elif facing == 2:
                y -= 1
            elif facing == 3:
                x -= 1

            inputs.append(grid.get((x, y), 0))
    except StopIteration:
        return grid
示例#2
0
文件: day23.py 项目: iKevinY/advent
def start_nic(pid):
    vm = emulate(TAPE, PACKET_QUEUES[pid])

    while True:
        addr = next(vm)
        x = next(vm)
        y = next(vm)

        if addr == 255:
            NAT[0] = x
            NAT[1] = y
        else:
            PACKET_QUEUES[addr].extendleft([x, y])
示例#3
0
文件: day19.py 项目: iKevinY/advent
def affected(x, y):
    if x < 0 or y < 0:
        return 0

    vm = emulate(TAPE, [y, x])
    return next(vm)
示例#4
0
文件: day09.py 项目: iKevinY/advent
import fileinput

from intcode import emulate

# Read input
TAPE = [int(x) for x in fileinput.input()[0].split(',')]

print "BOOST keycode:", next(emulate(TAPE, [1]))
print "Coordinates of distress signal:", next(emulate(TAPE, [2]))
示例#5
0
文件: day17.py 项目: iKevinY/advent
import fileinput

from intcode import emulate
from utils import Point

# Read problem input
TAPE = [int(x) for x in fileinput.input()[0].split(',')]

# Part 1
vm = emulate(TAPE[:], [])
board = []

try:
    while True:
        resp = next(vm)
        board.append(chr(resp))
except StopIteration:
    pass

signal = [a for a in ''.join(board).split()]

height = len(signal)
width = len(signal[0])

grid = {}

for y in range(height):
    for x in range(width):
        grid[Point(x, y)] = signal[y][x]
        if signal[y][x] == '^':
            START = Point(x, y)
示例#6
0
NOT C T
AND T J
NOT A T
OR T J
WALK
"""

running = """\
NOT C T
OR T J
NOT A T
OR T J
NOT B T
OR T J
AND D J
AND H J
NOT A T
OR T J
RUN
"""

for instructions in (walking, running):
    program = [ord(c) for c in instructions]
    vm = emulate(TAPE, program[::-1])
    try:
        while True:
            resp = next(vm)
            chr(resp),
    except Exception as e:
        print "{} hull damage: {}".format(instructions.split()[-1], resp)
示例#7
0
文件: day15.py 项目: iKevinY/advent
            for np in p.neighbours_4():
                if graph.get(np, 1) == 0:
                    continue

                if end is not None and np == end:
                    return dist + 1

                if np not in seen:
                    new_horizon.append(np)

                seen.add(np)

        horizon = new_horizon
        dist += 1

    return dist


# Read input
TAPE = [int(x) for x in fileinput.input()[0].split(',')]

BOARD = {}
OXYGEN = None

instructions = [0]
vm = emulate(TAPE, instructions)
robot_dfs(vm, instructions, BOARD, Point(0, 0), 0)

print "Optimal movement to oxygen:", bfs(BOARD, Point(0, 0), OXYGEN)
print "Minutes taken to fill up:", bfs(BOARD, OXYGEN) - 1
示例#8
0
文件: day13.py 项目: iKevinY/advent
import fileinput
from collections import Counter

from intcode import emulate

# Read input
TAPE = [int(x) for x in fileinput.input()[0].split(',')]
TAPE[0] = 2

inputs = [0]
game = emulate(TAPE, inputs)
grid = {}
ball_x = 0
padd_x = 0
score = None

try:
    while True:
        x = next(game)
        y = next(game)
        n = next(game)

        # Score output
        if x == -1 and y == 0:
            if score is None:
                print "Blocks at start of game:", Counter(grid.values())[2]
            score = n
        else:
            grid[x, y] = n
            if n == 3:
                padd_x = x
示例#9
0
文件: day25.py 项目: iKevinY/advent
west
take monolith
west
"""

items = [
    'space law space brochure', 'mouse', 'sand', 'wreath', 'manifold',
    'astrolabe', 'mug', 'monolith'
]

for i in range(1, len(items) + 1):
    for comb in combinations(items, i):
        for item in items:
            TAS += "drop {}\n".format(item)

        for c in comb:
            TAS += "take {}\n".format(c)

        TAS += 'west\n'

TAPE = [int(x) for x in fileinput.input()[0].split(',')]

vm = emulate(TAPE, [ord(c) for c in TAS][::-1])
try:
    while True:
        resp = chr(next(vm))
        sys.stdout.write(resp)

except Exception as e:
    pass