Пример #1
0
def part1(data):
  cpu = Intcode()
  cpu.load_program(data)
  cpu.ram[1] = 12
  cpu.ram[2] = 2
  cpu.run()
  return cpu.ram[0]
Пример #2
0
def part1(data):
    cpu = Intcode()
    cpu.set_ascii_mode(True)
    cpu.load_program(data)
    while True:
        try:
            cpu.run()
        except KeyboardInterrupt:
            pass
        print(''.join([chr(c) for c in cpu.read_output()]))
Пример #3
0
def part1(data):
    signals = []
    for perm in permutations((0, 1, 2, 3, 4)):
        out = 0
        for p in perm:
            cpu = Intcode(inputs=[p, out])
            cpu.load_program(data)
            cpu.run()
            out = cpu.outputs[-1]
        signals.append(out)
    return max(signals)
Пример #4
0
def part2(data, target):
  cpu = Intcode()
  cpu.load_program(data)

  for i, j in product(range(100), range(100)):
    cpu.reset()
    cpu.ram[1] = i
    cpu.ram[2] = j
    cpu.run()
    if cpu.ram[0] == target:
      return 100 * i + j
Пример #5
0
def part1(data):
    cpu = Intcode()
    cpu.load_program(data)
    cpu.run()
    scaf = ''.join([chr(c) for c in cpu.read_output()]).strip().split('\n')

    scafpts = [(x, y) for y, line in enumerate(scaf)
               for x, c in enumerate(line) if c == '#']
    isect = []
    for pt in scafpts:
        ptx, pty = pt
        if all([(x, y) in scafpts
                for x, y in ((ptx + 1, pty), (ptx - 1, pty), (ptx, pty - 1),
                             (ptx, pty + 1))]):
            isect.append((ptx, pty))
    return sum((x * y for x, y in isect))
Пример #6
0
def part1(data):
    prog = '\n'.join((
        'NOT C J',
        'AND D J',
        'NOT A T',
        'OR T J',
        'WALK\n',
    ))

    cpu = Intcode()
    cpu.load_program(data)
    cpu.set_ascii_mode(True)
    cpu.feed_inputs(*prog)
    cpu.run()
    out = cpu.read_output()

    try:
        print(''.join([chr(c) for c in out]))
    except ValueError:
        return (out[-1])
Пример #7
0
def part2(data):
    prog = '\n'.join((
        'NOT I J',
        'AND H J',
        'NOT F T',
        'AND G T',
        'OR T J',
        'AND D J',
        'AND H J',
        'NOT A T',
        'OR T J',
        'NOT C T',
        'AND D T',
        'OR T J',
        #    'NOT C T',
        #    'AND D T',
        #    'OR T J',
        'RUN\n',
    ))

    prog = '\n'.join(('NOT A T', 'OR T J', 'AND G T', 'AND D T', 'AND H T',
                      'OR T J', 'NOT C T', 'AND D T', 'OR T J', 'RUN\n'))

    prog = '\n'.join(('NOT A J', 'OR E T', 'OR C T', 'NOT T T', 'AND D T',
                      'OR T J', 'OR B T', 'OR E T', 'NOT T T', 'OR T J',
                      'NOT I T', 'AND D T', 'OR T J', 'RUN\n'))

    cpu = Intcode()
    cpu.load_program(data)
    cpu.set_ascii_mode(True)
    cpu.feed_inputs(*prog)
    cpu.run()
    out = cpu.read_output()

    try:
        print(''.join([chr(c) for c in out]))
    except ValueError:
        return (out[-1])
    test_springscript(prog)
Пример #8
0
def part2(data):
    cpu = Intcode(wfi_mode=True)
    cpu.set_ascii_mode(True)
    cpu.load_program(data)
    cpu.ram[0] = 2
    cpu.run()

    cpu.feed_inputs(
        *
        'A,B,A,B,C,C,B,A,C,A\nL,10,R,8,R,6,R,10\nL,12,R,8,L,12\nL,10,R,8,R,8\nn\n'
    )
    cpu.run()

    #cam = ''.join([chr(x) if chr(x).isprintable() or x == 10 and x < 0x110000 else str(x) for x in cpu.read_output() ]).strip().split('\n')
    cam = cpu.read_output()
    print(cam)
    '''
  from PIL import Image
  img = Image.new('RGB', (len(cam[0]), len(cam)), 'black')
  pix = img.load()
  for y, line in enumerate(cam):
    print(line)

    for x, char in enumerate(line):
      if char == '#':
        pix[x,y] = (0, 255, 128)

  img.save('day17.bmp')
  '''

    # L10 R8 R6 R10 L12 R8 L12 L10 R8 R6 R10 L12 R8 L12 L10 R8 R8 L10 R8 R8 L12 R8 L12 L10 R8 R6 R10 L10 R8 R8 L10 R8 R6 R10
    # L10 R8 R6 R10  = A
    # L12 R8 L12 = B
    # L10 R8 R8 = C
    # A B A B C C B A C A

    print(cpu.outputs)
Пример #9
0
def get_map(data):
    cpu = Intcode(debug=False, wfi_mode=True)
    cpu.load_program(data)
    droid = Droid(cpu)
    map_ = droid.get_map()
    return map_
Пример #10
0
def part1(data):
    cpu = Intcode(inputs=[1], debug=False)
    cpu.load_program(data)
    cpu.run()
    return cpu.outputs[-1]
Пример #11
0
def part2(data):
    cpu = Intcode(inputs=[5])
    cpu.load_program(data)
    cpu.run()
    return cpu.outputs[-1]
Пример #12
0
def part2(data):
    cpu = Intcode(wfi_mode=True, debug=False)
    cpu.load_program(data)
    robot = Robot(cpu)
    robot.start(1)
    robot.print_hull(black=' ')
Пример #13
0
def part1(data):
    cpu = Intcode(wfi_mode=True, debug=False)
    cpu.load_program(data)
    robot = Robot(cpu)
    robot.start(0)
    return len(robot.hull)
Пример #14
0
    while True:
        y += 1

        x = xmin
        directly_down = check_beam(cpu, x, y)
        if not directly_down:
            x += 1
            while not check_beam(cpu, x, y):
                x += 1
            xmin = x

        x = xmax
        directly_down = check_beam(cpu, x, y)
        if directly_down:
            x += 1
            while check_beam(cpu, x, y):
                x += 1
            xmax = x

        if xmax - xmin > 100:
            if check_beam(cpu, xmax - 100, y + 99):
                return 10000 * (xmax - 100) + y


if __name__ == '__main__':
    data = readlines(rpath('day19.txt', 'aoc2019'), conv=int, sep=',')
    cpu = Intcode()
    cpu.load_program(data)
    print(part1(cpu))
    print(part2(cpu))