Ejemplo n.º 1
0
def part1():
    in_q = Queue()
    out_q = Queue()
    comp = IntCodeComputer(get_input(), in_q, out_q)

    program = [
        # 3-wide hole -> J
        'NOT A J',
        'NOT B T',
        'AND T J',
        'NOT C T',
        'AND T J',
        'AND D J',
        # Three after is a hole
        'NOT C T',
        'OR T J',
        # One after is a hole
        'NOT A T',
        'OR T J',
        # ground 4 squares in front
        'AND D J',
        'WALK'
    ]
    for line in program:
        input_line(line, in_q)

    comp.execute(blocking=False)
    print_output(out_q)
Ejemplo n.º 2
0
def get_map():
    mem = get_input()
    input_q = Queue()
    output_q = Queue()
    comp = IntCodeComputer(mem, input_q, output_q)
    comp.execute()
    cs = []
    while not output_q.empty():
        cs.append(chr(output_q.get()))
    s = ''.join(cs)

    m = defaultdict(lambda: '.')
    for y, line in enumerate(s.splitlines()):
        for x, c in enumerate(line):
            if c != '.' and c != '#':
                pos = (x, y)
                if c == '^':
                    facing = 'N'
                elif c == 'v':
                    facing = 'S'
                elif c == '<':
                    facing = 'W'
                elif c == '>':
                    facing = 'E'
                else:
                    raise Exception("Invalid character {:}".format(c))
                c = '#'
            m[(x, y)] = c

    return m, pos, facing
Ejemplo n.º 3
0
def is_pulled(x, y):
    comp = IntCodeComputer(mem, input_q, output_q)
    input_q.put(x)
    input_q.put(y)
    comp.execute(blocking=False)
    res = output_q.get()
    if res == 1:
        return True
    else:
        return False
Ejemplo n.º 4
0
def part2():
    in_q = Queue()
    in_q.put(2)
    out_q = Queue()
    comp = IntCodeComputer(get_input(), in_q, out_q)

    comp.execute()

    while not out_q.empty():
        print(out_q.get())
Ejemplo n.º 5
0
  def __init__(self, mem, in_q, out_q):
    self._in_q = in_q
    self._out_q = out_q
    self._comp = IntCodeComputer(mem, in_q, out_q)
    self._state = defaultdict(lambda: EMPTY)
    self._prev_ball = None
    self._ball = None
    self._paddle = None
    self._score = None

    tiles = self.run_tick()
Ejemplo n.º 6
0
def find_noun_verb(in_line, output):
    for noun in range(100):
        for verb in range(100):
            instructions = list(map(int, in_line.strip().split(',')))
            instructions[1], instructions[2] = noun, verb
            in_line = ','.join(map(str, instructions))
            try:
                program_state = IntCodeComputer(in_line).run()
                out = program_state.split(',')[0]
                if out == str(output):
                    return noun, verb
            except Exception:
                pass
def part1():
    strengths = []
    for perm in permutations([0, 1, 2, 3, 4]):
        output_q = Queue()
        output_q.put(0)
        for i, phase in enumerate(perm):
            input_q = Queue()
            input_q.put(phase)
            input_q.put(output_q.get())
            output_q = Queue()
            computer = IntCodeComputer(get_input(), input_q, output_q)
            computer.execute()
        strengths.append(output_q.get())

    return max(strengths)
Ejemplo n.º 8
0
def part2():
    mem = get_input()
    input_q = Queue()
    output_q = Queue()
    comp = IntCodeComputer(mem, input_q, output_q)
    computer_thread = Thread(target=comp.execute)
    computer_thread.start()

    m = defaultdict(lambda: ' ')
    position = (0, 0)
    facing = E
    m[position] = 'D'
    status = None
    visited = set()
    visited.add(position)

    while True:
        move = decide_move(position, facing, m)
        input_q.put(move)
        status = output_q.get()
        if status == MOVED:
            facing = move
        new_position = update_position(position, move, status)
        update_map(m, position, move, status)
        position = new_position
        if position in visited and walls_on_three_sides(m, position):
            break

    return time_to_flood_fill(m)
Ejemplo n.º 9
0
def part1():
    mem = get_input()
    input_q = Queue()
    output_q = Queue()
    comp = IntCodeComputer(mem, input_q, output_q)
    computer_thread = Thread(target=comp.execute)
    computer_thread.start()

    m = defaultdict(lambda: ' ')
    position = (0, 0)
    facing = E
    m[position] = 'D'
    status = None

    i = 0
    while status != FOUND_OXYGEN:
        move = decide_move(position, facing, m)
        input_q.put(move)
        status = output_q.get()
        if status == MOVED:
            facing = move
        new_position = update_position(position, move, status)
        m[position] = '.'
        m[new_position] = 'D'
        update_map(m, position, move, status)
        position = new_position
        i += 1

    return find_path_length(m, (0, 0), find_oxygen(m))
Ejemplo n.º 10
0
def part2():
    m, pos, facing = get_map()
    path = plan_path(m, pos, facing)

    # This part I massaged by hand after finding the path by program
    A = [
        'L',
        '10',
        'R',
        '8',
        'R',
        '6',
        'R',
        '10',
    ]
    B = [
        'L',
        '12',
        'R',
        '8',
        'L',
        '12',
    ]
    C = ['L', '10', 'R', '8', 'R', '8']
    routine = ['A', 'B', 'A', 'B', 'C', 'C', 'B', 'A', 'C', 'A']

    mem = get_input()
    mem[0] = 2
    input_q = Queue()
    output_q = Queue()
    comp = IntCodeComputer(mem, input_q, output_q)

    run_until_blocks(comp)
    while not output_q.empty():
        print_line(output_q)
    insert_line(input_q, ','.join(routine))
    run_until_blocks(comp)
    print_line(output_q)
    insert_line(input_q, ','.join(A))
    run_until_blocks(comp)
    print_line(output_q)
    insert_line(input_q, ','.join(B))
    run_until_blocks(comp)
    print_line(output_q)
    insert_line(input_q, ','.join(C))
    run_until_blocks(comp)
    print_line(output_q)
    insert_line(input_q, 'n')
    run_until_blocks(comp)

    output = []
    dust_collected = None
    while not output_q.empty():
        c = output_q.get()
        if c <= 255:
            output.append(chr(c))
        else:
            dust_collected = c
    print(''.join(output))
    return dust_collected
Ejemplo n.º 11
0
def part2():
  pos = (0, 0)
  direc = 'N'
  panel_is_painted = defaultdict(lambda: False)
  panel_color = defaultdict(lambda: 'b')
  panel_color[pos] = 'w'
  
  input_queue = Queue()
  output_queue = Queue()

  computer = IntCodeComputer(get_input(), input_queue, output_queue)

  computer_thread = Thread(target=computer.execute)

  computer_thread.start()

  while not computer.halted:
    if panel_color[pos] == 'w':
      val = 1
    else:
      val = 0
    input_queue.put(val)
    color = output_queue.get()
    turn = output_queue.get()
    if color == 0:
      panel_is_painted[pos] = True
      panel_color[pos] = 'b'
    elif color == 1:
      panel_is_painted[pos] = True
      panel_color[pos] = 'w'

    direc = rotate(direc, turn)
    pos = move(pos, direc)

  return defaultdict_to_string(panel_color, {'b': ' ', 'w': '#'})
Ejemplo n.º 12
0
def part2():
    strengths = []
    for perm in permutations([5, 6, 7, 8, 9]):
        pipes = [Queue() for _ in perm]

        states = [get_input() for _ in perm]

        for i, phase in enumerate(perm):
            pipes[i].put(phase)

        # Starting input
        pipes[0].put(0)

        computers = []
        for i, state in enumerate(states):
            computers.append(
                IntCodeComputer(state, pipes[i], pipes[(i + 1) % 5]))

        threads = []
        for computer in computers:
            threads.append(Thread(target=computer.execute))

        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()

        strengths.append(pipes[0].get())

    return max(strengths)
Ejemplo n.º 13
0
def part1():
  mem = get_input()
  input_q = Queue()
  output_q = Queue()
  comp = IntCodeComputer(mem, input_q, output_q)

  comp.execute()

  tiles = []
  while not output_q.empty():
    x, y, tile = output_q.get(), output_q.get(), output_q.get()
    tiles.append((x, y, tile))

  blocks = 0
  for x, y, tile in tiles:
    if tile == 2:
      blocks += 1

  print(blocks)
Ejemplo n.º 14
0
def part2():
    in_q = Queue()
    out_q = Queue()
    comp = IntCodeComputer(get_input(), in_q, out_q)

    # (D && !(A && B && (C || !H)))
    # mostly represented right to left
    program = [
        'NOT H T',
        'OR C T',
        'AND B T',
        'AND A T',
        'NOT T J',
        'AND D J',
        'RUN',
    ]
    for line in program:
        input_line(line, in_q)

    comp.execute(blocking=False)
Ejemplo n.º 15
0
def start_computers():
    program = get_input()
    router = Router()
    computers = []
    for addr in range(50):
        computers.append(
            IntCodeComputer(program.copy(),
                            router.get_queue_to_computer(addr),
                            router.get_queue_from_computer(addr),
                            default_input=-1))
    threads = []
    threads.append(Thread(target=router.execute))
    for computer in computers:
        threads.append(
            Thread(target=computer.execute, kwargs={'blocking': False}))
    for thread in threads:
        thread.start()
    return router, threads
Ejemplo n.º 16
0
def answer(in_line):
    instructions = list(map(int, next(in_line).strip().split(',')))
    instructions[1], instructions[2] = 12, 2
    in_line = ','.join(map(str, instructions))
    program_state = IntCodeComputer(in_line).run()
    return program_state.split(',')[0]
Ejemplo n.º 17
0
class Game():
  def __init__(self, mem, in_q, out_q):
    self._in_q = in_q
    self._out_q = out_q
    self._comp = IntCodeComputer(mem, in_q, out_q)
    self._state = defaultdict(lambda: EMPTY)
    self._prev_ball = None
    self._ball = None
    self._paddle = None
    self._score = None

    tiles = self.run_tick()

  def __repr__(self):
    to_print = copy(self._state)
    to_print[self._ball] = BALL
    to_print[self._paddle] = PADDLE
    print('High Score: {:}'.format(self._score))
    return defaultdict_to_string(to_print,
      {
        EMPTY: ' ',
        WALL: '#',
        BLOCK: '@',
        PADDLE: '—',
        BALL: 'O'
      })

  @property
  def halted(self):
    return self._comp.halted

  @property
  def ball(self):
    return self._ball

  @property
  def prev_ball(self):
    return self._prev_ball

  @property
  def paddle(self):
    return self._paddle
  
  

  def update_state(self, tiles):
    for x, y, tile in tiles:
      if x == -1 and y == 0:
        self._score = tile
        continue
      elif tile == BALL:
        self._prev_ball = self._ball
        self._ball = (x, y)
      elif tile == PADDLE:
        self._paddle = (x, y)
      else:
        self._state[(x,y)] = tile

  def run_tick(self, joystick_in=0):
    self._in_q.put(joystick_in)

    try:
      self._comp.execute(blocking=False)
    except Empty:
      pass
    # print('halted = {:}'.format(self._comp.halted))
    tiles = get_tiles(self._out_q) 
    self.update_state(tiles)