Beispiel #1
0
 def reset(self):
   self.control_program = IntcodeComputer(self.original_program.copy())
   self.position = (40,30)
   self.screen = {}
   self.control_program.run_program()
   self.moves = []
   self.process_output()
Beispiel #2
0
from computer import IntcodeComputer
import logging
logging.basicConfig(level=logging.INFO)

with open('input') as fin:
    program = fin.read().strip()
instructions = [int(s) for s in program.split(",")]

computron = IntcodeComputer(instructions)
computron.run_program()


def extract_map(computron):
    scaffold_map = {}
    # Display computron's output
    row = 0
    col = 0
    for c in computron.output:
        logging.debug("Found an ouput of {}".format(c))
        #print(chr(c), end="")
        if c == 10:
            row += 1
            col = 0
        else:
            logging.info("Setting ({},{}) to {}".format(col, row, c))
            scaffold_map[(col, row)] = c
            col += 1
    return scaffold_map


def show_map(m):
Beispiel #3
0

m = {}
total_affected = 0
distance = 1700
target_square = 100
start_y = 1000

max_line = 0
max_square = 0

for y in range(start_y, distance):
    line_affected = 0
    stopped_short = False
    for x in range(int(y / 2), distance):
        computron = IntcodeComputer(instructions.copy())
        computron.set_input([x, y])
        computron.run_program()
        o = computron.output[-1]
        #if o == 1:
        #  logging.info("Beam at ({},{}): {}".format(x, y, o))
        m[(x, y)] = o
        if o == 1:
            line_affected += 1
        elif line_affected > 0:
            # Stop looking after the last affected space.
            stopped_short = True
            break
    # we're at the end of a line
    total_affected += line_affected
    if line_affected > max_line or max_square >= target_square:
Beispiel #4
0
class Droid:
  def __init__(self, control_program):
    self.original_program = control_program.program.copy()
    self.reset()

  def run(self):
    self.control_program.run_program()
    self.process_output()
    while True:
      self.update_screen()
      self.get_input()
      while self.control_program.step():
        pass
      self.process_output()

  def reset(self):
    self.control_program = IntcodeComputer(self.original_program.copy())
    self.position = (40,30)
    self.screen = {}
    self.control_program.run_program()
    self.moves = []
    self.process_output()

  def update_screen(self):
    for y in range(51):
      for x in range(81):
        if (x, y) == self.position:
          print("§", end="")
        else:
          print("{}".format(self.screen.get((x, y), UNKNOWN)), end="")
      print("")
    print("".join(['-']*80))

  def get_input(self):
    answer = input("north w, west a, south s, east d. Move? ")
    move = None
    if answer == 'w':
      move = NORTH
    elif answer == 's':
      move = SOUTH
    elif answer == 'a':
      move = WEST
    elif answer == 'd':
      move = EAST
    elif answer == 'R':
      self.reset()
    elif answer == 'P':
      logging.info("Current moves: {}".format(self.moves))
    else:
      logging.info("Ignoring unknown direction '{}'.".format(answer))
    if move is not None:
      self.last_direction = move
      self.control_program.input.append(move)

  def process_output(self):
    while len(self.control_program.output) > 0:
      status = self.control_program.output.pop(0)

      # repair droid hit a wall
      target_x, target_y = self.position
      if self.last_direction == NORTH:
        target_y -= 1
      elif self.last_direction == SOUTH:
        target_y += 1
      elif self.last_direction == WEST:
        target_x -= 1
      elif self.last_direction == EAST:
        target_x += 1

      if status == 0:
        # repair droid hit a wall
        self.screen[(target_x, target_y)] = WALL
      elif status == 1:
        # moved in requested direction
        self.screen[(target_x, target_y)] = PASSABLE
        self.moves.append(self.last_direction)
        self.position = (target_x, target_y)
      elif status == 2:
        # moved in requested direction and found the thingy
        self.position = (target_x, target_y)
        self.moves.append(self.last_direction)
        self.screen[(target_x, target_y)] = SENSOR
Beispiel #5
0
        # moved in requested direction
        self.screen[(target_x, target_y)] = PASSABLE
        self.moves.append(self.last_direction)
        self.position = (target_x, target_y)
      elif status == 2:
        # moved in requested direction and found the thingy
        self.position = (target_x, target_y)
        self.moves.append(self.last_direction)
        self.screen[(target_x, target_y)] = SENSOR


with open('input') as fin:
  program = fin.read().strip()
instructions = [int(s) for s in program.split(",")]

computron = IntcodeComputer(instructions)
droid = Droid(computron)
droid.run()

moves = [3, 3, 3, 3, 2, 2, 4, 4, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4, 4, 4, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 2, 2, 4, 4, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 4, 4, 1, 1, 1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 3, 3, 1, 1, 3, 3, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 4, 4, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 1, 1, 1, 1, 3, 3, 3, 3, 1, 1, 3, 3, 2, 2, 3, 3, 2, 2, 2, 2, 4, 4, 2, 2, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 1, 1, 4, 4, 1, 1, 3, 3, 3, 3, 1, 1, 4, 4, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 4, 4, 4, 4, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 2, 2, 4, 4, 1, 1, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 2, 2, 4, 4, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 3, 3, 1, 1, 4, 4, 1, 1, 3, 3, 3, 3]
print("It took {} moves to get to the sensor".format(len(moves)))
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░       █░█         █O    █░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█░ ░░░░░ ░░░░░░░░░ ░ ░████ █░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█   █░█   █░░░░░░   █   █   █░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░█░░░░░ ░█░░░ ░█░█░░░██ ░█░█░ █§██░░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░█       █     █░░     █ █     █   █░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░░░░░░██ ██░░░ ░░░ █ █ ░░░█░░░ ░░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░█         ░ █     █░░   █ ░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░██░░░░█░ ░ █ █░░█░█░███░ ░░░░░█░ ░░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█   █   █░█   ░░░░░ ░░░░░   █░░░░░░░░░░░░░░░░░░░░░
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░░██ ██░ ░ ░░░█░ ░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░
Beispiel #6
0
    parser.add_argument(
        "--bot-speed",
        dest='speed',
        type=int,
        help="1 - slowest, ..., 5 - fastest. default is 5",
        default=5,
        choices=[1, 2, 3, 4, 5],
    )
    args = parser.parse_args()

    intcode: List[str] = []
    with open(args.filename, "r") as f:
        for line in f.readlines():
            intcode += line.strip("\n").split(",")

    computer = IntcodeComputer(intcode)

    if args.solve_first:
        computer.io_wrapper = IOGame()
        computer.compute_all()
        print(
            len(
                list(
                    filter(
                        lambda tile: tile == Tile.Block,
                        computer.io_wrapper.tiles.values(),
                    ))))
        sys.exit(0)

    try:
        intcode[0] = "2"  # game mode
Beispiel #7
0
#!/usr/bin/env python
# Beniamin Dudek <[email protected], github.com/thinkofher>
import argparse
from typing import List

from computer import IntcodeComputer

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Solution for the 9th day of Advent of Code.", )
    parser.add_argument(
        "filename",
        type=str,
        help="name of the file with input data",
    )
    args = parser.parse_args()

    intcode = []  # type: List[str]
    with open(args.filename, "r") as f:
        for line in f.readlines():
            intcode += line.strip("\n").split(",")

    IntcodeComputer(intcode).compute_all()
Beispiel #8
0
        description="Solution for the 11th day of Advent of Code.",
    )
    parser.add_argument(
        "filename", type=str, help="name of the file with input data",
    )
    parser.add_argument(
        "default_color",
        type=str,
        help="set default color",
        choices=["black", "white"],
    )
    args = parser.parse_args()

    intcode = []  # type: List[str]
    with open(args.filename, "r") as f:
        for line in f.readlines():
            intcode += line.strip("\n").split(",")

    if args.default_color == "black":
        default_color = Color.Black
    else:
        default_color = Color.White

    robot = Robot(default_color=default_color)
    computer = IntcodeComputer(intcode)
    computer.io_wrapper = robot
    computer.compute_all()

    print('Robot did', len(robot.table.items()), 'operations.', end='\n'*2)
    visualize_table(robot.table)