def main(): data = intcode.load() sim_state = intcode.SimState('ascii', data) while True: ret = intcode.simulator(sim_state) if ret == "INPUT": buf = sys.stdin.read(1) if not buf: break ch = buf[0] b = ord(ch) sim_state.input.append(b) elif ret == "OUTPUT": while sim_state.output: b = sim_state.output.pop(0) if b > 0x7F: print(f'\n{b}') else: ch = chr(b) sys.stdout.write(ch) sys.stdout.flush() elif ret == "HALT": break else: raise Exception(f"unknown intcode return state: {ret}")
import sys sys.path.append("../intcode") import intcode grid = {} pos = (0, 0) facing = (0, -1) prog = intcode.load(sys.argv[1]) try: g = intcode.run(prog) output = g.next() # output should be empty here. while True: output = g.send(grid.get(pos, 0)) grid[pos] = output[0] if output[1] == 0: facing = facing[1], -facing[0] else: facing = -facing[1], facing[0] pos = pos[0] + facing[0], pos[1] + facing[1] except StopIteration: pass print "Painted %d panels at least once" % len(grid)
dx, dy = rdir[0], rdir[1] if n == 0: dx, dy = dy, -dx elif n == 1: dx, dy = -dy, dx else: raise RuntimeError(f'bad turn: {n}') rpos[0] += dx rpos[1] += dy rdir[0] = dx rdir[1] = dy intcode.run(prog, stdin=robot_in, stdout=robot_out) return white, painted # part 1 prog = intcode.load('day11-input.txt' if len(sys.argv) < 2 else sys.argv[1]) white, painted = robot(prog) print(len(painted)) # part 2 white, painted = robot(prog, set([(0,0)])) min_x, max_x = min(w[0] for w in white), max(w[0] for w in white) min_y, max_y = min(w[1] for w in white), max(w[1] for w in white) for y in range(min_y, max_y+1): for x in range(min_x, max_x+1): c = '#' if (x,y) in white else ' ' print(c, end='') print('')
def flood(maze): i = 0 locs = {loc for loc, item in maze.items() if item == GOAL} spaces = {loc for loc, item in maze.items() if item == SPACE} while spaces: newlocs = set() for loc in locs: for dn in dirs: newloc = step(loc, dn) if newloc in spaces: spaces.remove(newloc) newlocs.add(newloc) i += 1 locs = newlocs return i if __name__ == "__main__": m = Maze(intcode.load(15)) mode = 'full' if len(sys.argv) > 1: mode = sys.argv[1] if mode == 'curses': wrapper(interactive_main, m) elif mode == 'solve': solve_main(m) else: explore_main(m) print(flood(m.map))
def __init__(self): self.pos = (0, 0) self.vm = intcode.VM(intcode.load('day15-input.txt'))
def main(): data = load() simulate_network(data)
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import intcode import itertools import sys prog = intcode.load('day25-input.txt') # run interactively: #intcode.run(prog, stdin='ascii', stdout='ascii') #sys.exit(0) # solution: # - asterisk, astronaut ice cream, fixed point, ornament # - 134227456 # solver below class Computer: def __init__(self, prog): self._vm = intcode.VM(prog) self._vm._stdin = self._in
def parse_image(image): lines = image.splitlines() height = len(lines) width = max(map(len, lines)) pixels = {} for y, line in enumerate(lines): for x, c in enumerate(line): pixels[x, y] = c return pixels, width, height def find_junctions(image): pixels, width, height = parse_image(image) for (x, y), c in pixels.items(): if x == 0 or y == 0 or x == width - 1 or y == height - 1: continue if c != '#': continue if (pixels[x - 1, y] != '#' or pixels[x, y - 1] != '#' or pixels[x + 1, y] != '#' or pixels[x, y + 1] != '#'): continue yield x, y if __name__ == "__main__": c = Camera(intcode.load(17)) image = c.get_image() print(image) print(sum(a * b for a, b in find_junctions(image)))
def part2(): program = intcode.load(_INPUT_PATH) for n in range(100): for v in range(100): if intcode.execute_from_memory(program, n, v) == 19690720: return 100 * n + v
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import intcode import sys prog = intcode.load('day17-input.txt') # part 1 def out2path(out): rows = ''.join(chr(c) for c in out).split('\n') while rows[-1] == '': rows.pop() path = set() for y, row in enumerate(rows): for x, c in enumerate(row): if c != '.': path.add((x,y)) return path out = [] intcode.run(prog, stdin=[], stdout=out)