Esempio n. 1
0
 def boot(self) -> None:
     for i in range(self.size):
         _thread = icp.Thread(icp.loadProgram(self.program))
         # Set IP
         _thread.addInput(i)
         _thread.id = i
         self.computers.append(_thread)
Esempio n. 2
0
def part2() -> None:
  ip = icp.Thread(icp.loadProgram(PROGRAM_PATH))
  ip.addInputAscii('NOT A T\nNOT B J\nOR T J\nNOT C T\nOR T J\nAND D J\nNOT E T\nNOT T T\nOR H T\nAND T J\nRUN\n')

  while not ip.didSucceed():
    ip.process()
    
  ip.readOutputAscii()
  print(ip.readOutput())
Esempio n. 3
0
def countBlocks():
    tiles = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}

    arcade = icp.Thread(icp.loadProgram(PROGRAM_PATH))
    while not arcade.didSucceed():
        arcade.process()
        if len(arcade.ostream) == 3:
            arcade.ostream.pop(0)
            arcade.ostream.pop(0)
            tiles[arcade.ostream.pop(0)] += 1

    print(tiles[2])
Esempio n. 4
0
def findO2System() -> Scanner:
  init = icp.Thread(icp.loadProgram(PROGRAM_PATH))
  drones = [Scanner(init.clone(), DM_N, 0, IntPoint(0,0)),
            Scanner(init.clone(), DM_E, 0, IntPoint(0,0)),
            Scanner(init.clone(), DM_S, 0, IntPoint(0,0)),
            Scanner(init.clone(), DM_W, 0, IntPoint(0,0))]
  status = -1
  drone = drones.pop(0)
  while not status == DS_ONO2:
    drone.program.process()
    if drone.program.needInput():
      # Drone waiting for input.
      #print("----------s%d %s" % (drone.program.id, drone.position))
      #printMap(droneMap, drone.position)
      direction = drone.direction
      """Bulletproof Input
      print(drone.position, DS_P[status], end=" ")
      newDirection = -1
      while newDirection == -1:
        try:
          newDirection = S_D[input("<=")]
        except:
          newDirection = -1
      
      direction = newDirection
      #"""
      drone.program.istream.append(direction)
      continue
    if drone.program.hasOutput():
      status = drone.program.readOutput()
      if status == DS_MOVE or status == DS_ONO2:
        drone.steps += 1
        drone.position += MD[direction]
        addPosition(droneMap, drone.position, status)

        for d in MC:
          if d == drone.direction: continue
          if not hasBeenVisited(droneMap, drone.position + MD[d]):
            #print("Making scanner facing %s" % DM_P[d])
            drones.append(Scanner(drone.program.clone(), d, drone.steps, drone.position))

      elif status == DS_WALL:
        wallPosition = drone.position + MD[direction]
        addPosition(droneMap, wallPosition, status)
        if len(drones): drone = drones.pop(0)
        else:
          #print("-------")
          #printMap(droneMap,drone.position)
          return drone
      continue

  return drone
Esempio n. 5
0
def playGame():
    arcade = icp.Thread(icp.loadProgram(PROGRAM_PATH))
    arcade.program[0] = 2
    while not arcade.didSucceed():
        arcade.process()
        if arcade.status.err == icp.ERR_INPUT:
            render(arcade.ostream, ai)
            t = 0
            if ai[0] < ai[1]: t = -1
            elif ai[0] > ai[1]: t = 1
            arcade.istream.append(t)

    render(arcade.ostream, ai)
    print(score[0])
Esempio n. 6
0
def part1() -> Tuple[Set[IntPoint], IntPoint]:
    ip = icp.Thread(icp.loadProgram(PROGRAM_PATH))
    output: List[str] = []
    x, y = 0, 0
    scaffolds: Set[IntPoint] = set()
    position: IntPoint = IntPoint(0, 0)
    while not ip.didSucceed():
        ip.process()
        if ip.hasOutput():
            value = chr(ip.readOutput())
            currentLocation = IntPoint(x, y)
            if value == '#':
                # A scaffold exists at this location.
                scaffolds.add(currentLocation)
            elif value == '^':
                # The robot exists at this location.
                # It is on a scaffold.
                scaffolds.add(currentLocation)
                position = currentLocation

            if value == '\n':
                x = 0
                y += 1
            else:
                x += 1

            output.append(value)

    alignment_sum = 0

    #print(''.join(map(str,output)))

    for s in scaffolds:
        checks = {
            s + DIRECTIONS['n'], s + DIRECTIONS['e'], s + DIRECTIONS['s'],
            s + DIRECTIONS['w']
        }

        if len(scaffolds.intersection(checks)) == 4:
            alignment_sum += s.x * s.y

    print(alignment_sum)

    return scaffolds, position
Esempio n. 7
0
def startWhite():
    hull = {(0, 0): WHITE}
    robotPosition = (0, 0)
    robotFacing = UP

    robotBrain = icp.Thread(icp.loadProgram(PROGRAM_PATH))
    while not robotBrain.didSucceed():
        robotBrain.process()
        if robotBrain.needInput():
            robotBrain.istream.append(hull.get(robotPosition, BLACK))
            continue

        if robotBrain.noError() and len(robotBrain.ostream) == 2:
            paint = robotBrain.ostream.pop(0)
            hull[robotPosition] = paint
            if robotBrain.ostream[0] == 0: robotFacing = ROT_LEFT[robotFacing]
            else: robotFacing = ROT_RIGHT[robotFacing]
            robotBrain.ostream.pop(0)
            robotPosition = move(robotPosition, DIRECTIONS[robotFacing])

    printHull(hull)
Esempio n. 8
0
def part2():
    """
  Via inspection, it is seen that the sequences that work match:

  A,B,A,C,A,B,C,B,C,B
  A=L,10,R,8,L,6,R,6
  B=L,8,L,8,R,8
  C=R,8,L,6,L,10,L,10
  """

    ip = icp.Thread(icp.loadProgram(PROGRAM_PATH))
    ip.program[0] = 2
    ip.addInputAscii(
        'A,B,A,C,A,B,C,B,C,B\nL,10,R,8,L,6,R,6\nL,8,L,8,R,8\nR,8,L,6,L,10,L,10\nn\n'
    )

    while not ip.didSucceed():
        ip.process()
    # Clear the output buffer of the map.
    ip.readOutputAscii()
    print(ip.readOutput())
Esempio n. 9
0
# adventofcode.com
# Day 19
# https://adventofcode.com/2019/day/19

from common import getFilePath
import intcode as icp
from typing import Dict, Tuple, Set, List

core = icp.Thread(icp.loadProgram(getFilePath('input19.txt')))

def scan(x: int, y: int) -> int:
  t = core.clone()
  t.istream.append(x)
  t.istream.append(y)
  while not t.didSucceed():
        t.process()

  return t.readOutput()

def checkPoint(left:int, bottom:int) -> Tuple[int, int, int]:
  top = bottom - 99
  right = left + 99

  return (scan(right,top),left,top)


def part1():
  strength = 0

  for y in range(50):
    for x in range(50):