Esempio n. 1
0
 def __init__(self, data):
     self.data = data
     self.computer = Intcoder(self.data, 0)
     self.items = [
         "cake", "prime number", "mutex", "dehydrated water", "coin",
         "manifold", "candy cane", "fuel cell", ""
     ]
     self.out = 0
     self.res = ""
Esempio n. 2
0
 def __init__(self, data):
     self.data = data
     self.computer = Intcoder(data, 0)
     self.arrays = []  # used for animation
     self.data[0] = 2
     self.current_score, self.paddle_x, self.ball_x = 0, 0, 0
     self.grid_x = 45  # hardcoded from part1
     self.grid_y = 23  # hardcoded from part1
     self.arr = np.zeros((self.grid_y, self.grid_x))
Esempio n. 3
0
 def __init__(self, data):
     self.grid = 0
     self.directions = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}
     self.backtracking = {1: 2, 2: 1, 3: 4, 4: 3}
     self.visited = set()
     self.computer = Intcoder(data, 0)
     self.max_x = 41  #fetched from part 1
     self.max_y = 41  #fetched from part 1
     self.base_x = (self.max_x // 2) + 1
     self.base_y = (self.max_y // 2) + 1
     self.grid = [[0] * self.max_x for i in range(self.max_y)]
     self.arrays = [deepcopy(self.grid)]
Esempio n. 4
0
def part1(data, part=1):
    computer = Intcoder(data, 0)
    if part == 1:
        ascii_in = [ord(c) for c in program1()]
    else:
        ascii_in = [ord(c) for c in program2()]
    out = 1
    while out != -1:
        out = computer.eval(ascii_in)
        if out > 255:
            return out
    return -1
Esempio n. 5
0
    def part1(self):
        C = []
        for i in range(self.n):
            C.append(Intcoder(self.data.copy(), i))

        idle = 0
        while True:
            for i, c in enumerate(C):
                a = c.eval(self.get_input)
                x = c.eval(self.get_input)
                y = c.eval(self.get_input)
                print(a, x, y)
                if a == None:
                    continue
                idle = 0

                if a == 255:
                    self.NAT = (x, y)
                    continue
                elif a not in range(50):
                    time.sleep(.4)
                    continue
                self.inputs[a].append(x)
                self.inputs[a].append(y)
            if idle > 2:
                self.inputs[0].append(self.NAT[0])
                self.inputs[0].append(self.NAT[1])
                if len(self.SEEN) > 0 and self.NAT[1] == self.SEEN[-1]:
                    print(f"result: {self.NAT[1]}")
                    time.sleep(10)
                self.SEEN.append(self.NAT[1])
            idle += 1
Esempio n. 6
0
class Breakout:
    def __init__(self, data):
        self.data = data
        self.computer = Intcoder(data, 0)
        self.arrays = []  # used for animation
        self.data[0] = 2
        self.current_score, self.paddle_x, self.ball_x = 0, 0, 0
        self.grid_x = 45  # hardcoded from part1
        self.grid_y = 23  # hardcoded from part1
        self.arr = np.zeros((self.grid_y, self.grid_x))

    # AI plays a good game of breakout
    def play(self):
        self.data[0] = 2  # play for free mode
        inn = None
        while True:
            x = self.computer.eval(inn)
            y = self.computer.eval(inn)
            tile_id = self.computer.eval(inn)
            inn = self.parse_output(x, y, tile_id)

            if x == -1 and y == 0:
                self.current_score = tile_id
                continue
            elif tile_id == -1:
                return self.current_score

            self.arr[y, x] = tile_id
            self.arrays.append(self.arr.copy())

    # Parses the output from intcode computer and acts accordingly
    def parse_output(self, x, y, tile_id):
        # moving paddle and ball
        if tile_id == 4:
            self.ball_x = x
        elif tile_id == 3:
            self.paddle_x = x

        # getting the next input for intcoder
        if self.paddle_x < self.ball_x:
            return 1
        elif self.paddle_x > self.ball_x:
            return -1
        else:
            return 0
Esempio n. 7
0
def part1(data, val):
    dim = val * 12
    arr = np.zeros((dim, dim))
    arrays = []
    start_x = 0
    last_xs = np.zeros((dim), dtype=int)
    print(last_xs)
    i = 0
    ship_loc = False
    while i < dim:
        if i % 3 == 0:
            arrays.append(deepcopy(arr))
        found = False
        for j in range(start_x, dim):
            if found == True and j < last_xs[i - 1] - 2:
                arr[i, j] = 1
                continue
            else:
                computer = Intcoder(data.copy(), 0)
                out = computer.eval([j, i])
                arr[i, j] = out
            if out and not found:
                found = True
                start_x = j
            if found and not out:
                last_xs[i] = j - 1
                break
        if not ship_loc:
            done, res_i, res_j = test_array(arr,
                                            arrays,
                                            i,
                                            last_xs[i - val],
                                            val=val)
        if done:
            ship_loc = True
            print(done, res_i, res_j)
        i += 1

    for _ in range(10):
        arrays.append(deepcopy(arr))
    Animater(arrays)
    return (10000 * res_j) + res_i
Esempio n. 8
0
def part_n(data, inn):
    computer = Intcoder(data, 0)
    directions = [(0, -1), (1, 0), (0, 1), (-1, 0)]
    minmax = [0, 0, 0, 0]
    x, y, color, dir_index = 0, 0, 0, 0
    painted = defaultdict(int)

    # While intcode program is not done paint new square and move
    while color != -1:
        color = computer.eval(inn)
        painted[(x, y)] = color
        test_new_minmax(x, y, minmax)

        turn_dir = computer.eval(inn)
        dir_index += 1 if turn_dir else -1
        x += directions[dir_index % 4][0]
        y += directions[dir_index % 4][1]
        inn.append(painted[(x, y)])

    return painted, len(painted), minmax
Esempio n. 9
0
def part_n(data, part=1):
    computer = Intcoder(data, 0)
    if part == 2:
        data[0] = 2

    arr = [[]]
    current_y = 0
    res = ""
    while True:
        out = computer.eval(main)
        if out == -1:
            break
        if out == 10:
            arr.append([])
            current_y += 1
        else:
            arr[current_y].append(chr(out))
        if part == 1:
            res += chr(out)
        else:
            res = out
    return arr, res
Esempio n. 10
0
def part_n(data, inn):
    computer = Intcoder(data, 0)
    directions = [(0, -1), (1, 0), (0, 1), (-1, 0)]
    x, y, color, dir_index = 0, 0, 0, 0
    arr = np.full((6, 43), -1)
    arrays = [arr]
    iterations = 0
    # While intcode program is not done paint new square and move
    while color != -1:
        new_arr = arrays[-1].copy()
        color = computer.eval(inn)
        new_arr[y][x] = color
        arrays.append(new_arr)

        turn_dir = computer.eval(inn)
        dir_index += 1 if turn_dir else -1
        x += directions[dir_index % 4][0]
        y += directions[dir_index % 4][1]
        inn.append(new_arr[y][x])
        iterations += 1

    return arrays, iterations
Esempio n. 11
0
class Maze_solver:
    def __init__(self, data):
        self.grid = 0
        self.directions = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}
        self.backtracking = {1: 2, 2: 1, 3: 4, 4: 3}
        self.visited = set()
        self.computer = Intcoder(data, 0)
        self.max_x = 41  #fetched from part 1
        self.max_y = 41  #fetched from part 1
        self.base_x = (self.max_x // 2) + 1
        self.base_y = (self.max_y // 2) + 1
        self.grid = [[0] * self.max_x for i in range(self.max_y)]
        self.arrays = [deepcopy(self.grid)]

    def draw_frame(self, arr):
        #os.system("clear")
        ret = ""
        for i in range(len(arr)):
            for j in range(len(arr[0])):
                if arr[i][j] == 0: ret += " "
                elif arr[i][j] == 1: ret += "."
                elif arr[i][j] == 2: ret += "D"
                elif arr[i][j] == 3: ret += "#"
                elif arr[i][j] == 4: ret += "O"
                else: ret += "B"
            ret += "\n"
        print(ret)
        #time.sleep(0.1)

    def part1(self, data):
        results = []

        res = self.rec(self.base_x, self.base_y, 1, results)
        depth = self.get_depth(9, 3, 0) - 1

        return results, depth

    def get_depth(self, x, y, depth):
        self.grid[y][x] = 10 - (depth * 0.01)
        self.arrays.append(deepcopy(self.grid))
        self.draw_frame(self.grid)
        time.sleep(0.001)
        depths = [0, 0, 0, 0]
        if self.grid[y][x] == 1:
            return 0
        for i in range(1, 5):
            new_x = x + self.directions[i][0]
            new_y = y + self.directions[i][1]
            if self.grid[new_y][new_x] == 1:
                depths[i - 1] = self.get_depth(new_x, new_y, depth + 1)
        return max(depths) + 1

    def rec(self, x, y, steps, results):
        self.draw_frame(self.grid)
        self.arrays.append(deepcopy(self.grid))
        time.sleep(.001)
        self.grid[y][x] = 2
        self.arrays.append(deepcopy(self.grid))
        self.visited.add((x, y))
        for i in range(1, 5):
            new_x = x + self.directions[i][0]
            new_y = y + self.directions[i][1]
            if (new_x, new_y) in self.visited:
                continue
            out = self.computer.eval(i)
            if out == 0:
                self.grid[new_y][new_x] = 3
                continue
            elif out == 1:
                self.grid[new_y][new_x] = 2
                self.grid[y][x] = 1
                self.rec(new_x, new_y, steps + 1, results)
                self.computer.eval(self.backtracking[i])
                self.grid[y][x] = 2
            elif out == 2:
                self.grid[new_y][new_x] = 4
                self.computer.eval(self.backtracking[i])
                results.append(steps)
        self.grid[y][x] = 1
Esempio n. 12
0
class Zork:
    def __init__(self, data):
        self.data = data
        self.computer = Intcoder(self.data, 0)
        self.items = [
            "cake", "prime number", "mutex", "dehydrated water", "coin",
            "manifold", "candy cane", "fuel cell", ""
        ]
        self.out = 0
        self.res = ""

    def take_drop_items(self, items, keyword="take"):
        next_in = self.string_to_ascii(keyword + items[0] + "\n")
        self.computer.set_input(next_in)
        print("take " + items[0] + "\n")
        index = 1
        while index < len(items):
            self.out = self.computer.eval()
            self.res += chr(self.out)
            if "Command?" in self.res:
                print(self.res)
                self.res = ""
                self.computer.set_input(
                    self.string_to_ascii(keyword + items[index] + "\n"))
                index += 1

    def break_door(self):
        last_perm = self.items
        for r in range(1, len(self.items)):
            all_inns = list(combinations(self.items, r))
            for perm in all_inns:
                self.take_drop_items(last_perm, keyword="drop ")
                self.take_drop_items(perm, keyword="take ")
                self.computer.set_input(self.string_to_ascii("west\n"))
                while "Command?" not in self.res:
                    self.out = self.computer.eval()
                    if self.out == -1:
                        return self.res
                        time.sleep(1)
                    self.res += chr(self.out)
                print(self.res)
                self.res = ""
                last_perm = perm

    def solve(self):
        inputs = [line for line in open("path.txt").read().split("\n")]
        #print(inputs2)
        #time.sleep(100)
        items = [
            "cake", "prime number", "mutex", "dehydrated water", "coin",
            "manifold", "candy cane", "fuel cell", ""
        ]
        index = 0
        while self.out != -1:
            self.out = self.computer.eval()
            if self.out == -1:
                print("out = -1", self.res)
                break
            self.res += chr(self.out)
            if "Command?" in self.res:
                print(self.res)
                #inn = self.string_to_ascii(random.choice(self.directions))
                if index < len(inputs):
                    inn = self.string_to_ascii(
                        input("enter direction: ") + "\n")
                    #inn = self.string_to_ascii(inputs[index] + "\n")
                else:
                    return self.break_door()
                self.computer.set_input(inn)
                self.res = ""
                index += 1
                print(self.res)

    def string_to_ascii(self, s):
        return [ord(c) for c in s]