def amplifier(in_pipe, out_pipe, args): vm.Parser(file, vm.Machine(in_pipe, out_pipe, args)).parse()
import vm from pipes import Pipe p = vm.Parser('day9.txt') program = p.parse() pipe = Pipe() m1 = vm.Machine(program.copy(), pipe, pipe, [1]) m2 = vm.Machine(program.copy(), pipe, pipe, [2]) m1.execute() print("Day 9 part 1:", pipe.inspect()) m2.execute() print("Day 9 part 2:", pipe.inspect())
def run(program, in_pipe, out_pipe, status): brain = vm.Machine(program, in_pipe, out_pipe, status, []) brain.execute()
from pipes import Pipe from functools import reduce from itertools import permutations import vm, threading def amplifier(in_pipe, out_pipe, args): vm.Parser(file, vm.Machine(in_pipe, out_pipe, args)).parse() file = 'day7.txt' outputs = [] perms = permutations([0, 1, 2, 3, 4]) pipes = [Pipe() for _ in range(5)] for perm in perms: vm.Parser(file, vm.Machine(pipes[0], pipes[1], [0, perm[0]])).parse() vm.Parser(file, vm.Machine(pipes[1], pipes[2], [perm[1]])).parse() vm.Parser(file, vm.Machine(pipes[2], pipes[3], [perm[2]])).parse() vm.Parser(file, vm.Machine(pipes[3], pipes[4], [perm[3]])).parse() vm.Parser(file, vm.Machine(pipes[4], pipes[0], [perm[4]])).parse() outputs.append(pipes[0].get_input()) print("Day 7 Part 1: ", reduce(max, outputs, 0)) perms = permutations([5, 6, 7, 8, 9]) outputs = [] for perm in perms: a0 = threading.Thread(target=amplifier, args=( pipes[0], pipes[1],
# -*- coding: latin-1 -*- import parse, vm parse.assemble_file_by_name("src/bios.rasm") main_vm = vm.Machine() main_vm.normal_boot()
def runpvm(rpvmFile="vmdisk.hdd"): master = Tk() w = Canvas(master, width=800, height=600) w.pack() w.create_rectangle(0, 0, 800, 600, fill='black', width=0) # Define VM Screen Object linetx = str(30) linetxc = str(0) screen = w def drawPixel(x, y, c): w.create_rectangle(x, y, 1, 1, fill=c) def drawText(x, y, t, c, style_s=""): w.create_text(x, y, text=t, fill=c, stipple=style_s) def drawRect(x, y, x2, y2, c): w.create_rectangle(x, y, x2, y2, fill=c) def drawCircle(x, y, c): w.create_rectangle(x, y, 1, 1, fill=c) def fillBackground(c): w.create_rectangle(0, 0, 800, 600, fill=c, width=0) def drawImages(x, y, im): img = PhotoImage(file=im) canvas.create_image(x, y, anchor=NW, image=img) def appendVGA(tx): drawText(40, 300, tx, "grey") print(crayons.green("reformatting temporary disk...", bold=True)) with open("vmtemp.mem", mode="w") as vmwtemp: vmwtemp.write("") with open(rpvmFile, mode="r") as vmwbtemp: hdd_disk0 = vmwbtemp.read() vmtempread = open("vmtemp.mem", mode="r") vmtempwrite = open("vmtemp.mem", mode="w") print(crayons.green("reformatted temporary disk", bold=True)) print(crayons.green("reading hard disk...", bold=True)) time.sleep(2) print(crayons.green("v-vga started", bold=True)) print(crayons.magenta("system image loaded", bold=True)) # Drawing commands Here # """def paint( event ): python_green = "#476042" x1, y1 = ( event.x - 1 ), ( event.y - 1 ) x2, y2 = ( event.x + 1 ), ( event.y + 1 ) w.create_oval( x1, y1, x2, y2, fill = python_green ) w.bind( "<B1-Motion>", paint )""" optcode = optomizer.OptomizerTrans(hdd_disk0) c = vm.Machine(optcode) c.run() c.dump_stack() appendVGA(c.log) vmtempwrite.write(str(c.data_stack)) # End Drawing commands Here # print(crayons.magenta("setup image done", bold=True)) mainloop() print(crayons.red("pvm exit", bold=True)) vmtempread.close() vmtempwrite.close()
import vm vm.Machine([ '"Welcome to the exponentiator! Enter two numbers, and then you\'ll get the result."', "println", '""', "println", '"#1: "', "print", "read", "cast_int", '"#2: "', "print", "read", "cast_int", "^", "println" ]).run()
def run(program_memory, input_pipe, output_pipe, status_register): vm.Machine(program_memory, input_pipe, output_pipe, status_register, []).execute()
# vm executor for running commands line-by-line in a separate file. import sys import vm opened = open(sys.argv[1], "r") SPLIT_LINES = opened.read().splitlines() LINES = list(filter(lambda line: line != '', SPLIT_LINES)) opened.close() index_counter = 0 for line in LINES: if line[0] == "#": # It's an integer; convert. LINES[index_counter] = int(line[1:]) elif line[0] == "@": # It's a float; convert. LINES[index_counter] = float(line[1:]) index_counter += 1 vm.Machine(LINES).run()