示例#1
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    memory[0] = 2
    ic = IntCode(memory)
    score = -1
    paddle_x = 0
    while True:
        x = ic.get_output()
        y = ic.get_output()
        tile = ic.get_output()
        if x[0] or y[0] or tile[0]:
            return score
        if x[1] == -1 and y[1] == 0:
            score = tile[1]
        elif tile[1] == 3:
            paddle_x = x[1]
        elif tile[1] == 4:
            ball_x = x[1]
            if ball_x > paddle_x:
                ic.add_inputs(1)
            elif ball_x < paddle_x:
                ic.add_inputs(-1)
            else:
                ic.add_inputs(0)
示例#2
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    perms = permutations(range(5, 10))
    max_val = -1
    for perm in perms:
        a, b, c, d, e = perm
        a_comp = IntCode(memory, a)
        b_comp = IntCode(memory, b)
        c_comp = IntCode(memory, c)
        d_comp = IntCode(memory, d)
        e_comp = IntCode(memory, e)
        finished = False
        prev_e_res = 0
        while not finished:
            a_comp.add_inputs(prev_e_res)
            a_res = a_comp.get_output()
            b_comp.add_inputs(a_res[1])
            b_res = b_comp.get_output()
            c_comp.add_inputs(b_res[1])
            c_res = c_comp.get_output()
            d_comp.add_inputs(c_res[1])
            d_res = d_comp.get_output()
            e_comp.add_inputs(d_res[1])
            e_res = e_comp.get_output()
            if e_res[0]:
                finished = True
                max_val = max(max_val, prev_e_res)
            else:
                prev_e_res = e_res[1]
    return max_val
示例#3
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    grid = defaultdict(int)
    coord = (0, 0)
    grid[(0, 0)] = 1
    direction = (0, -1)
    ic = IntCode(memory, 1)
    while True:
        new_color = ic.get_output()
        if new_color[0]:
            break
        grid[coord] = new_color[1]
        turn = ic.get_output()
        if turn[0]:
            break
        direction = new_direction(direction, turn[1])
        coord = coord[0] + direction[0], coord[1] + direction[1]
        ic.add_inputs(1 if grid[coord] else 0)
    min_x = min(grid.keys(), key=lambda p: p[0])[0]
    min_y = min(grid.keys(), key=lambda p: p[1])[1]
    max_x = max(grid.keys(), key=lambda p: p[0])[0]
    max_y = max(grid.keys(), key=lambda p: p[1])[1]
    rows = []
    for y in range(min_y, max_y + 1):
        row = ''
        for x in range(min_x, max_x + 1):
            if grid[(x, y)]:
                row += '#'
            else:
                row += ' '
        rows.append(row)
    print('\n'.join(rows))
def main2():
    program = open_program('input')

    program[0] = 2
    computer = IntCode(program, default_memory=8000)

    halted = False
    grid = defaultdict(lambda: 0)
    while not halted:
        halted = computer.run()

        while computer.has_output():
            x_pos = computer.get_output()
            y_pos = computer.get_output()
            tile_id = computer.get_output()

            if tile_id == 3:
                paddle_x = x_pos
            elif tile_id == 4:
                ball_x = x_pos

            grid[Point(x_pos, y_pos)] = tile_id

        print_grid(grid)

        if paddle_x > ball_x:
            user_input = -1
        elif paddle_x < ball_x:
            user_input = 1
        else:
            user_input = 0

        computer.add_input(user_input)

    return grid[Point(-1, 0)]
示例#5
0
文件: day15.py 项目: kohrongying/aoc
def build_map():
  initialise_queue()
  while len(queue) > 0:
    curr_move = queue.pop(0)
    comp = IntCode('../input/day15.txt')
    comp, curr_pos = walk_curr_move(comp, curr_move)
    for m in movement_commands:
      output = comp.get_output(m, 1)[0]
      if output == 0:
        new_pos = get_coor(m, curr_pos)
        walls.add(new_pos)
      else:
        if output == 2:
          found = get_coor(m, curr_pos)
        new_move = copy.deepcopy(curr_move)
        new_move.append(m)
        new_pos = get_coor(m, curr_pos)
        if new_pos not in seen:
          seen.append(new_pos)
          queue.append(new_move)
        comp.get_output(opp[m], 1)
  print('found', found)

  f = open('day15-out2.txt', 'w')
  f.write(f'{found[0]} {found[1]} O\n')
  for i in walls:
    f.write(f'{i[0]} {i[0]} #\n')
  for j in seen:
    f.write(f'{i[0]} {i[0]} .\n')
示例#6
0
文件: 13.py 项目: PiErr0r/aoc
def part_2(data):
	data[0] = 2
	game = IntCode(data, [])
	game.calculate()
	

	i = 0
	cnt = 0
	GRID = [[' . ' for i in range(44)] for j in range(23)]
	while not game.is_halted() and cnt < 10:
		screen = game.get_output(-1)
		i = 0
		while i < len(screen):
			[x, y, tile] = screen[i:i + 3] 
			if x == -1 and y == 0:
				print(tile)
				i += 3
				continue
			GRID[y][x] = t[tile]
			i += 3
		disp(GRID)
		game.unpause([0, 0, 0])
		game.calculate()
		cnt += 1

	print('END OF PART2')
	return 
示例#7
0
文件: day13.py 项目: kohrongying/aoc
def solve2():
    comp = IntCode('../input/day13.txt')
    paddle = (None, None)
    ball = (None, None)
    score = 0
    joystick = 0
    while True:
        outputs = comp.get_output(joystick, 3)
        if len(outputs) != 3:
            break
        x, y, tile_id = outputs
        if x == -1 and y == 0:
            score = tile_id
        if tile_id == 3:
            paddle = (x, y)
        elif tile_id == 4:
            ball = (x, y)

        if ball[0] is not None and paddle[0] is not None:
            if ball[0] < paddle[0]:
                joystick = -1
            elif ball[0] > paddle[0]:
                joystick = 1
            else:
                joystick = 0
        else:
            joystick = 0

    print('score', score)
def check_point(program: Tuple[int], point: Point):
    computer = IntCode(list(program))
    computer.add_input(point.x)
    computer.add_input(point.y)
    computer.run()

    output = computer.get_output()
    return output
示例#9
0
def day_15(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    grid = {(0, 0): '.', (1, 0): '?', (-1, 0): '?', (0, 1): '?', (0, -1): '?'}
    droid = (0, 0)
    move_to_dir = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}
    ic = IntCode(memory)
    while '?' in grid.values():
        path = find_nearest_unknown(grid, droid)
        for m in path[:-1]:
            ic.add_inputs(m)
            ic.get_output()
            droid = (droid[0] + move_to_dir[m][0],
                     droid[1] + move_to_dir[m][1])
        m = path[-1]
        ic.add_inputs(m)
        status = ic.get_output()[1]
        if status == 0:
            wall = (droid[0] + move_to_dir[m][0], droid[1] + move_to_dir[m][1])
            grid[wall] = '#'
        else:
            droid = (droid[0] + move_to_dir[m][0],
                     droid[1] + move_to_dir[m][1])
            if status == 1:
                grid[droid] = '.'
            else:
                grid[droid] = 'O'
                target = droid
            for adj_cell, _ in adjacent(droid):
                if adj_cell not in grid:
                    grid[adj_cell] = '?'
    p1_res = shortest_path(grid, (0, 0), target)
    time = 0
    with_oxygen = [target]
    while '.' in grid.values():
        new_oxygen = []
        for cell in with_oxygen:
            all_adj = adjacent(cell)
            for adj_cell, _ in all_adj:
                if grid[adj_cell] == '.':
                    grid[adj_cell] = 'O'
                    new_oxygen.append(adj_cell)
        with_oxygen = new_oxygen
        time += 1
    return p1_res, time
示例#10
0
def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    s = 0
    for x in range(50):
        for y in range(50):
            ic = IntCode(memory.copy(), x, y)
            if ic.get_output()[1] == 1:
                s += 1
    return s
示例#11
0
文件: day13.py 项目: kohrongying/aoc
def solve():
    # Change memory address 0 to 1
    comp = IntCode('../input/day13.txt')
    outputs = comp.get_output(0, -1)
    num_tiles = 0
    for i in range(0, len(outputs) - 3, 3):
        x, y, tile_id = outputs[i:i + 3]
        if tile_id == 2:
            num_tiles += 1
    print(num_tiles)
def main():
    program = open_program('input')

    computer = IntCode(program, default_memory=8000)

    halted = False
    grid = defaultdict(lambda: 0)
    while not halted:
        halted = computer.run()

    while computer.has_output():
        x_pos = computer.get_output()
        y_pos = computer.get_output()
        tile_id = computer.get_output()

        grid[Point(x_pos, y_pos)] = tile_id

    print_grid(grid)

    return len([item for item in grid.values() if item == 2])
示例#13
0
def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    grid = defaultdict(int)
    coord = (0, 0)
    direction = (0, -1)
    painted = set()
    ic = IntCode(memory, 0)
    while True:
        new_color = ic.get_output()
        if new_color[0]:
            break
        grid[coord] = new_color[1]
        painted.add(coord)
        turn = ic.get_output()
        if turn[0]:
            break
        direction = new_direction(direction, turn[1])
        coord = coord[0] + direction[0], coord[1] + direction[1]
        ic.add_inputs(grid[coord])
    return len(painted)
示例#14
0
def main(initial_color=0, filename='input'):
    program = open_program(filename)
    computer = IntCode(program)

    halted = False
    grid = defaultdict(lambda: 0)
    location = Point(0, 0)
    grid[location] = initial_color
    facing = Direction.UP
    while not halted:
        computer.add_input(grid[location])
        halted = computer.run()

        color = computer.get_output()
        direction = computer.get_output()

        grid[location] = color
        location, facing = move_robot(location, facing, direction)

    print_grid(grid)

    return len(grid.keys())
示例#15
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    x = 0
    y = 99
    while True:
        ic = IntCode(memory.copy(), x, y)
        if ic.get_output()[1] == 0:
            x += 1
        else:
            if IntCode(memory.copy(), x + 99, y - 99).get_output()[1] == 1:
                return x * 10000 + (y - 99)
            y += 1
示例#16
0
文件: day15.py 项目: kohrongying/aoc
def initialise_queue():
  curr_pos = (0, 0)
  for m in movement_commands:
    comp = IntCode('../input/day15.txt')
    output = comp.get_output(m, 1)[0]
    if output == 2:
      new_pos = get_coor(m, curr_pos)
      found = new_pos
      print('found')
    elif output == 1:
      new_pos = get_coor(m, curr_pos)
      seen.append(new_pos)
      queue.append([m])
示例#17
0
文件: day15.py 项目: kohrongying/aoc
def solve():
  initialise_queue()
  while len(queue) > 0:
    curr_move = queue.pop(0)
    comp = IntCode('../input/day15.txt')

    comp, curr_pos = walk_curr_move(comp, curr_move)

    for m in movement_commands:
      output = comp.get_output(m, 1)[0]
      if output == 2:
        found = get_coor(m, curr_pos)
        print('num moves', len(curr_move) + 1)
        queue = []
        break
      elif output == 1:
        new_move = copy.deepcopy(curr_move)
        new_move.append(m)
        new_pos = get_coor(m, curr_pos)
        if new_pos not in seen:
          seen.append(new_pos)
          queue.append(new_move)
        comp.get_output(opp[m], 1)
示例#18
0
文件: 13.py 项目: PiErr0r/aoc
def part_1(data):
	game = IntCode(data, [])
	game.calculate()
	screen = game.get_output(-1)

	i = 0
	cnt = 0
	while i < len(screen):
		[x, y, tile] = screen[i:i + 3] 
		if tile == 2:
			cnt += 1
		i += 3
	print(cnt)
	# disp(GRID)
	print('END OF PART1')
	return
def part1():
    program = open_program('input')
    # computer = IntCode(program)

    count = 0
    for y in range(50):
        for x in range(50):
            computer = IntCode(program[:])
            computer.add_input(x)
            computer.add_input(y)
            computer.run()

            output = computer.get_output()
            if output:
                count += 1
            else:
                pass
                # print(x,y)

    return count
示例#20
0
文件: 07.py 项目: PiErr0r/aoc
def part_1(data):

    perms = it.permutations(orig_list_1)
    thruster = 0
    for p in perms:
        output = 0
        for phase in p:
            amp_data = [phase]
            if output is not None:
                amp_data += [output]
            amp = IntCode(data, amp_data)
            amp.calculate()
            output = amp.get_output()

        if output > thruster:
            thruster = output

    print(thruster)
    print('END OF PART1')
    return
示例#21
0
def part_2(data):
    GRID = [['.' for i in range(100)] for j in range(100)]
    code = IntCode(data)
    r_pos = (0, 0)
    r_d = 'N'
    panels = {r_pos: 1}
    while not code.is_halted():
        try:
            in_data = [panels[r_pos]]
        except:
            in_data = [0]

        code.unpause(in_data)
        code.calculate()
        [color, d] = code.get_output(2)
        panels[r_pos] = color
        GRID[50 + r_pos[1]][50 + r_pos[0]] = '#' if color == 1 else '.'
        r_pos, r_d = get_new_pos(r_pos, r_d, d)
    disp(GRID)
    print('END OF PART2')
    #CBLPJZCU
    return
示例#22
0
def part_1(data):

    code = IntCode(data)
    r_pos = (0, 0)
    r_d = 'N'
    panels = {r_pos: 0}
    painted = set()
    while not code.is_halted():
        try:
            in_data = [panels[r_pos]]
        except:
            in_data = [0]

        code.unpause(in_data)
        code.calculate()
        [color, d] = code.get_output(2)
        painted |= {r_pos}
        panels[r_pos] = color
        r_pos, r_d = get_new_pos(r_pos, r_d, d)

    print(len(painted))
    print('END OF PART1')
    return
示例#23
0
文件: day11.py 项目: kohrongying/aoc
    new_pos = (x, y-1)
    return new_dir, new_pos
  elif new_dir == 4:
    new_pos = (x-1, y)
    return new_dir, new_pos
  else:
    return ValueError("Invalid direction")
    
panels = {}
comp = IntCode('../input/day11.txt')
curr_position = (0,0)
curr_color = 1
curr_direction = 1

while True:
  outputs = comp.get_output(curr_color, 2)
  if len(outputs) != 2:
    break
  color, turn = outputs
  panels[curr_position] = color
  curr_direction, curr_position = get_new_position(curr_position, curr_direction, turn)
  if curr_position in panels:
    curr_color = panels[curr_position]
  else:
    curr_color = 0

# PART 1
print(len(panels))

# PART 2
for k in panels: