Ejemplo n.º 1
0
def easy():
    state = get_inp()
    game = list(intcode.Runner(state))
    tiles = defaultdict(int)
    for i in range(0, len(game), 3):
        tiles[(game[i], game[i + 1])] = game[i + 2]
    return list(tiles.values()).count(2)
Ejemplo n.º 2
0
def hard():
    state = get_inp()
    computers = [intcode.Runner(state[:], Queue([i])) for i in range(50)]
    last_sent_y = None
    nat = None
    is_idle = False
    while True:
        for num, comp in enumerate(computers):
            sent = list(comp)
            for i in range(0, len(sent), 3):
                dest, x, y = sent[i:i + 3]
                if dest == 255:
                    nat = [x, y]
                else:
                    computers[dest].stdin.data += [x, y]
            if len(sent):
                is_idle = False
                break
        else:
            if is_idle:
                if nat[1] == last_sent_y:
                    return last_sent_y
                computers[0].stdin.data += nat
                last_sent_y = nat[1]
            else:
                is_idle = True
                for comp in computers:
                    comp.stdin.data.append(-1)
Ejemplo n.º 3
0
def easy():
    state = get_inp()
    stdin = ((x, y) for x in range(50) for y in range(50))
    result = 0
    for x, y in stdin:
        result += next(intcode.Runner(state[:], [x, y]))
    return result
Ejemplo n.º 4
0
def hard():
  state = get_inp()
  game = intcode.Runner(state)
  # Flood to find Oxygen tank
  game = flood(game)[1]
  # Flood from oxygen tank
  return flood(game)[0]
Ejemplo n.º 5
0
def easy():
    result = float("-inf")
    code = get_inp()
    for perm in permutations(range(5)):
        val = 0
        for p in perm:
            val = next(intcode.Runner(code[:], [p, val]))
        result = max(val, result)
    return result
Ejemplo n.º 6
0
def hard():
    inp = get_inp()
    view = ''.join(map(chr, intcode.Runner(inp))).strip().split('\n')
    view = ['.' + line + '.' for line in view]
    view = ['.' * len(view[0])] + view + ['.' * len(view[0])]
    pos = [(x, y) for y, line in enumerate(view) for x, c in enumerate(line)
           if c in '^<>v']
    pos = pos[0]
    direction = '^>v<'.index(view[pos[1]][pos[0]])
    instructions = []
    while True:
        f, l, r = get_nei(direction, *pos)
        if view[f[1]][f[0]] == '#':
            pos = f
            instructions[-1] += 1
        elif view[l[1]][l[0]] == '#':
            pos = l
            instructions += ['L', 1]
            direction = (direction - 1) % 4
        elif view[r[1]][r[0]] == '#':
            pos = r
            direction = (direction + 1) % 4
            instructions += ['R', 1]
        else:
            break

    main = ','.join(map(str, instructions))
    # Find these manually ¯\_(ツ)_/¯
    A = 'L,10,R,8,R,6,R,10'
    B = 'L,12,R,8,L,12'
    C = 'L,10,R,8,R,8'
    main = main.replace(A, 'A')
    main = main.replace(B, 'B')
    main = main.replace(C, 'C')
    stdin = list(map(ord, '{}\n{}\n{}\n{}\nn\n'.format(main, A, B, C)))

    inp = get_inp()
    inp[0] = 2
    return list(intcode.Runner(inp, stdin))[-1]
Ejemplo n.º 7
0
def easy():
    state = get_inp()
    pos = (0, 0)
    fac = 0  # up
    colors = defaultdict(int)
    prog = intcode.Runner(state, [0])
    for color in prog:
        colors[pos] = color
        fac += next(prog) * 2 - 1
        fac %= 4
        pos = move(pos, fac)
        prog.stdin.append(colors[pos])
    return len(colors)
Ejemplo n.º 8
0
def easy():
    state = get_inp()
    # J = (!A OR !B OR !C) AND D
    stdin = '''
NOT A T
NOT B J
OR T J
NOT C T
OR T J
AND D J
WALK
'''.strip() + '\n'
    runner = intcode.Runner(state, list(map(ord, stdin)))
    return list(runner)[-1]
Ejemplo n.º 9
0
def hard():
    state = get_inp()
    starty = 10
    startx, stopx = None, None
    for x in range(starty):
        beam = next(intcode.Runner(state[:], [x, starty]))
        if beam and startx is None:
            startx = [x]
        elif not beam and startx is not None:
            stopx = [x]
            break

    for y in range(starty + 1, 1100):
        for expected, store in enumerate([stopx, startx]):
            x = store[-1]
            beam = next(intcode.Runner(state[:], [x, y]))
            if beam != expected:
                x += 1
            store.append(x)

    for y, (start, stop) in enumerate(zip(startx[99:], stopx)):
        if stop - start >= 100:
            return 10000 * start + y + starty
Ejemplo n.º 10
0
def hard():
    result = float("-inf")
    code = get_inp()
    for perm in permutations(range(5, 10)):
        val = 0
        gens = [intcode.Runner(code[:], [p]) for p in perm]
        try:
            while True:
                for i in range(5):
                    gens[i].stdin.append(val)
                    val = next(gens[i])
        except StopIteration:
            pass
        result = max(val, result)
    return result
Ejemplo n.º 11
0
def easy():
    inp = get_inp()
    view = list(intcode.Runner(inp))
    view = ''.join(map(chr, view)).strip().split('\n')
    result = 0
    for i, line in enumerate(view):
        if i == 0 or i == len(view) - 1:
            continue
        for j, c in enumerate(line):
            if j == 0 or j == len(line) - 1:
                continue
            if c == line[j - 1] == line[j + 1] == view[i + 1][j] == view[
                    i - 1][j] == '#':
                result += j * i
    return result
Ejemplo n.º 12
0
def easy():
    state = get_inp()
    computers = [intcode.Runner(state[:], Queue([i])) for i in range(50)]
    while True:
        for num, comp in enumerate(computers):
            sent = list(comp)
            for i in range(0, len(sent), 3):
                dest, x, y = sent[i:i + 3]
                if dest == 255:
                    return y
                computers[dest].stdin.data += [x, y]
            if len(sent):
                break
        else:
            for comp in computers:
                comp.stdin.data.append(-1)
Ejemplo n.º 13
0
def hard():
    state = get_inp()
    # J = (!A OR !B OR !C) AND D AND (E OR H)
    stdin = '''
NOT A T
NOT B J
OR T J
NOT C T
OR T J
AND D J
NOT D T
OR E T
OR H T
AND T J
RUN
'''.strip() + '\n'
    runner = intcode.Runner(state, list(map(ord, stdin)))
    return list(runner)[-1]
Ejemplo n.º 14
0
def flood(game):
  visited = set([(0, 0)])
  q = deque([(0, (0, 0), game)])
  while len(q):
    l, (x, y), game = q.popleft()
    for i in range(4):
      nx, ny = mv(x, y, i+1)
      if (nx, ny) in visited:
        continue
      subgame = intcode.Runner(game)
      subgame.stdin = [i+1]
      success = next(subgame)
      visited.add((nx, ny))
      if success == 1:
        q.append((l+1, (nx, ny), subgame))
      elif success == 2:
        return l+1, subgame
  return l, subgame
Ejemplo n.º 15
0
def hard(debug=False):
    state = get_inp()
    pos = (0, 0)
    fac = 0  # up
    colors = defaultdict(int)
    colors[pos] = 1
    prog = intcode.Runner(state, [1])
    for color in prog:
        colors[pos] = color
        fac += next(prog) * 2 - 1
        fac %= 4
        pos = move(pos, fac)
        prog.stdin.append(colors[pos])
    result = '\n'
    xs, ys = zip(*list(colors))
    for y in range(min(ys), max(ys) + 1):
        for x in range(min(xs), max(xs) + 1):
            result += ' #'[colors[(x, y)]]
        result += '\n'
    return result
Ejemplo n.º 16
0
def hard():
    state = get_inp()
    state[0] = 2
    game = intcode.Runner(state)
    tiles = defaultdict(int)
    ballx, paddlex = -1, -1
    buf = []

    class Stdin(object):
        def __init__(self):
            pass

        def __len__(self):
            return 1

        def __getitem__(self, key):
            if isinstance(key, slice):
                return self
            if ballx < paddlex:
                return -1
            if ballx > paddlex:
                return 1
            return 0

    game.stdin = Stdin()
    for n in game:
        if n is not None:
            buf.append(n)
        else:
            assert (False)
        if len(buf) == 3:
            x, y, tile = buf
            tiles[(x, y)] = tile
            ballx = x if tile == 4 else ballx
            paddlex = x if tile == 3 else paddlex
            buf = []
    return tiles[(-1, 0)]
Ejemplo n.º 17
0
def easy():
    return next(intcode.Runner(get_inp(), [1]))
Ejemplo n.º 18
0
def hard():
    return next(intcode.Runner(get_inp(), [2]))
Ejemplo n.º 19
0
 def __init__(self, state):
     self.runner = intcode.Runner(state)
     self.load_room()
     self.pos = 0, 0
     self.visited = set([self.pos])
     self.screen = None
Ejemplo n.º 20
0
def manual(runner):
    runner = runner or intcode.Runner(get_inp())
    runner.stdin = Queue()
    for c in runner:
        print(chr(c), end='')
        sys.stdout.flush()
Ejemplo n.º 21
0
def easy():
  state = get_inp()
  visited = set([(0, 0)])
  return flood(intcode.Runner(state))[0]