Beispiel #1
0
def paint(starting_color):
    computer = ic.Computer(ic.load("input"))

    field = defaultdict(int)
    pos = Point(0, 0)
    facing = up

    field[pos] = starting_color

    commands = []
    seen = set()  #probably don't really need this
    while not computer.halted:
        if computer.needs_input():
            computer.send(field[pos])
        response = computer.tick()
        if response is not None:
            commands.append(response)
            if len(commands) == 2:
                color, direction = commands
                field[pos] = color
                seen.add(pos)
                if direction == 0:
                    facing = counterclockwise[facing]
                else:
                    facing = clockwise[facing]
                pos += facing
                commands.clear()

    return field
Beispiel #2
0
def execute(springcode, verbose=False):
    """
    run the springcode and return the large integer if the bot succeeds.
    if the bot fails, return the row that killed it.
    prints all output if verbose is True.
    """
    computer = ic.Computer(ic.load("input"))
    for c in s:
        computer.send(ord(c))

    rows = [[]]
    #computer.program[1665] = 0 #flight mode
    while True:
        if computer.pc == 610:
            print(f"Packed data: {computer.program[753]:3}; unpacked data:",
                  [computer.program[x] for x in range(716, 747)])
        # for i in range(726, 734):     #no gap mode
        # computer.program[i]= 1
        if (x := computer.tick()) is not None:
            if x > 256:
                return x
            else:
                if verbose:
                    print(chr(x), end="")
                if chr(x) == "\n":
                    rows[-1] = "".join(rows[-1])
                    if all(x in rows[-1] for x in "@#"):
                        return rows[-1].replace("@", ".")
                    rows.append([])
                else:
                    rows[-1].append(chr(x))
Beispiel #3
0
def play():
    if os.path.exists(SAVE_FILENAME) and ask("Load previous save? "):
        computer = load()
    else:
        computer = ic.Computer(ic.load("input"))

    while True:
        if computer.needs_input():
            command = input()
            if command == "save":
                with open(SAVE_FILENAME, "w") as file:
                    file.write(computer.dumps())
                print("Saved. Command?")
            else:
                command = command + "\n"
                for c in command:
                    computer.send(ord(c))
        else:
            x = computer.tick()
            if x is not None:
                print(chr(x), end="")
                sys.stdout.flush()
Beispiel #4
0
def iter_output():
    computer = ic.Computer(ic.load("input"))
    while not computer.halted:
        x = computer.tick()
        if x is not None:
            yield (chr(x))
Beispiel #5
0
intersections = []
for p, cell in list(field.items()):
    if cell != EMPTY:
        neighboring_scaffolds = 0
        for delta in deltas:
            neighbor = p + delta
            if field[neighbor] != EMPTY:
                neighboring_scaffolds += 1
        if neighboring_scaffolds > 2:
            intersections.append(p)

print(sum(p.x * p.y for p in intersections))

#part 2
#value determined by looking at the output of longpath() very intently
s = "B,A,B,C,A,C,A,C,B,C\nL,8,R,10,R,6\nR,12,L,10,R,12\nR,12,L,10,R,10,L,8\nn\n"

computer = ic.Computer(ic.load("input"))
computer.program[0] = 2

for c in s:
    computer.send(ord(c))

while not computer.halted:
    x = computer.tick()
    if x is not None:
        if x > 256:
            print(x)
        else:
            #print(chr(x), end="")
            pass
Beispiel #6
0
            if instruction.is_function_return():
                print("\n")
            pc += 1 + len(instruction.params)
        else:
            line = f"{str(program[pc]) + ',':30} #{pc:4} (data)"
            if str(pc) in annotations["global variables"]:
                line += f" aka {annotations['global variables'][str(pc)]}"
            if program[pc] in map(ord, string.printable):
                line += f"\t\tpossibly {repr(chr(program[pc]))}"
            print(line)
            pc += 1

if __name__ == "__main__":
    filename = "input" if len(sys.argv) < 2 else sys.argv[1]
    annotations_filename = "annotations.json" if len(sys.argv) < 3 else sys.argv[2]
    if os.path.exists(annotations_filename):
        with open(annotations_filename) as file:
            annotations = json.load(file)
    else:
        annotations = {}
    expected_section_defaults = {"global variables": {}, "functions": {}, "data ranges": []}
    for section, default in expected_section_defaults.items():
        if section not in annotations:
            annotations[section] = default
    unexpected_sections = list(annotations.keys() - expected_section_defaults.keys())
    if unexpected_sections:
        print("Warning: annotation json contains unexpected sections:", "\n".join("  " + section for section in unexpected_sections))
        print(f"Expected sections are {expected_section_defaults.keys()}")
    
    program = ic.load(filename)
    dis(program, annotations)
Beispiel #7
0
            x, y, value = [computer.tick_until_output() for _ in range(3)]
            if x is None:  #probably needs input again
                break
            if x == -1 and y == 0:
                print("Score:", value)
                booted = True
                break
            if value == 3: paddle_x = x
            if value == 4: ball_x = x
            color = ["white", "black", "red", "green", "blue"][value]
            canvas.itemconfig(cell_ids[x, y], fill=color)
        if booted: break
    root.after(10, update)


program = ic.load("input")
program[0] = 2
computer = ic.Computer(program)

root = tkinter.Tk()

cols = 46
rows = 24
cell_size = 10

canvas = tkinter.Canvas(root, width=cell_size * cols, height=cell_size * rows)
canvas.pack()

cell_ids = {}
for i in range(cols):
    for j in range(rows):
Beispiel #8
0
import intcomputer

program = intcomputer.load("input")
print(intcomputer.Computer(program, [1]).tick_until_output())
print(intcomputer.Computer(program, [2]).tick_until_output())
Beispiel #9
0
def get(x,y):
    computer = ic.Computer(ic.load("input"))
    computer.send(x)
    computer.send(y)
    return computer.tick_until_output()
Beispiel #10
0
import intcomputer as ic
for x in (1, 5):
    print(ic.Computer(ic.load("input"), [x]).tick_until_blocked()[-1])
Beispiel #11
0
import intcomputer as ic

computers = [ic.Computer(ic.load("input"), [i]) for i in range(50)]
output_bufs = [[] for _ in range(50)]

IDLE = 0
BUSY = 1

time = 0
idle_states = [BUSY for _ in range(50)]

part_one_answered = False

nat_x = None
nat_y = None
last_sent_y = None
while True:
    time += 1
    for idx, (computer, output_buf) in enumerate(zip(computers, output_bufs)):
        if computer.needs_input():
            computer.send(-1)
            if computer.program[61] == 1:
                idle_states[idx] = IDLE
        x = computer.tick()
        if x is not None:
            output_buf.append(x)
            if len(output_buf) == 3:
                pc, x, y = output_buf
                output_buf.clear()
                if pc == 255:
                    if not part_one_answered: