Esempio n. 1
0
def part1():
    real = list(eval(open('day13.txt').read()))
    pgm = Program(real)
    pgm.run()
    queue = pgm.dump()
    print(len(queue))
    twos = [1 for k in take(queue, 3) if k[2] == 2]
    print("Part 1:", len(twos))  # 228
Esempio n. 2
0
def part2():
    program = Program("17")
    program.program[0] = 2
    main = "A,B,A,B,C,B,C,A,B,C\n"
    a = "R,4,R,10,R,8,R,4\n"
    b = "R,10,R,6,R,4\n"
    c = "R,4,L,12,R,6,L,12\n"
    camera = "n\n"

    input_buffer = []
    [input_buffer.extend(ord(c) for c in routine) for routine in [main, a, b, c, camera]]

    program.run(input_buffer)
    return program.output_buffer[-1]
Esempio n. 3
0
def part2():
    pgm = Program(real)
    pgm.pgm[0] = 2
    instructions = (
        "A,B,A,C,C,A,B,C,B,B\n",
        "L,8,R,10,L,8,R,8\n",
        "L,12,R,8,R,8\n",
        "L,8,R,6,R,6,R,10,L,8\n",
        "n\n"
    )
    for c in ''.join(instructions):
        pgm.push(ord(c))
    pgm.run()
    d = pgm.dump()
    if TRACE:
        print( Maze(d[:-1]).string )
    return d[-1]
Esempio n. 4
0
def test_sample():
    programs = (
        [3, 0, 4, 0, 99],
        [1101, 100, -1, 4, 0],
        [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8],
        [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8],
        [3, 3, 1108, -1, 8, 3, 4, 3, 99],
        [3, 3, 1107, -1, 8, 3, 4, 3, 99],
        [
            3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
            1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104,
            999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99
        ],
    )
    for prog in programs:
        p = Program(prog, [9])
        p.run()
        print(p.outputs)
    exit(0)
Esempio n. 5
0
def part2():
    real = list(eval(open('day13.txt').read()))

    # Make one run to determine the limits of the grid.

    pgm = Program(real)
    pgm.run()
    queue = list(take(pgm.dump(), 3))
    w = max(k[0] for k in queue) + 1
    h = max(k[1] for k in queue) + 1
    print(w, h)

    # Create the printable grid.

    grid = []
    for i in range(h):
        grid.append([' '] * w)

    # Populate it from the part 1 output.

    for x, y, t in queue:
        grid[y][x] = ' #x-o'[t]

    display(grid)

    # Insert a quarter.

    real[0] = 2

    # Go run the app.

    pgm = Prog13(real)
    pgm.grid = grid
    pgm.run()

    # Pop any unfinished output.

    pgm.clear_queue()

    display(grid)

    print("Part 2:", pgm.score)  # 10776
Esempio n. 6
0
def part1():
    program = Program("17")
    program.run()

    x, y = (0, 0)
    scaffold = []
    for c in program.output_buffer:
        print(chr(c), end='')
        if chr(c) == "\n":
            y += 1
            x = 0
            continue
        elif chr(c) in ["#", "^", "v", "<", ">"]:
            scaffold.append((x, y))
        x += 1

    intersections = [(x, y) for (x, y) in scaffold if
                     (all(neighbour in scaffold for neighbour in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]))]

    return sum([(x * y) for (x, y) in intersections])
Esempio n. 7
0
def start_computer(address):
    computer = Program("23")
    computer.run([address])
    return computer
Esempio n. 8
0
from intcode import Program

if __name__ == "__main__":
    total = 0
    for x in range(50):
        for y in range(50):
            bot = Program("input.txt")
            bot.add_input(x)
            bot.add_input(y)
            bot.run()
            total += bot.last_output

    print(total)
Esempio n. 9
0
def test_point(row, col):
    bot = Program("input.txt")
    bot.add_input(row)
    bot.add_input(col)
    bot.run()
    return bot.last_output
Esempio n. 10
0
class Search:
    def __init__(self, inp, start = (0, 0)):
        self.bot = Program(inp)
        self.curr = start
        self.dirList = {self.curr: 0}
        self.expandList = [(0, self.curr, d, self.bot.get_state()) for d in range(len(DIRS))]
        self.goalPos = None

    def expand_next(self, sort = True):
        # Find closest point to expand
        if sort:
            self.expandList.sort(reverse = True, key = lambda x: x[0])
        nextTile = None
        dist = None
        delta = None

        while (nextTile == 0 or nextTile is None) and len(self.expandList) > 0:
            dist, pos, delta, bot_state = self.expandList.pop()

            # Skip if this tile has already been expanded
            self.curr = (pos[0] + DIRS[delta][0], pos[1] + DIRS[delta][1])
            if self.curr in self.dirList.keys():
                continue
            
            self.bot.program = bot_state[2]
            self.bot.base = bot_state[1]
            self.bot.pos = bot_state[0]
            self.bot.input = delta + 1

            # Run intcode to find response
            self.bot.run(stop = 4)
            self.bot.run(stop = 3)
            nextTile = self.bot.output

        if nextTile is not None and nextTile != 0:
            # Set optimal direction to get back to start
            self.dirList[self.curr] = DIRS[delta + (1 if delta % 2 == 0 else -1)]
            
            if nextTile == 2: # set goalpos if found
                self.goalPos = self.curr

            # Add adjascent tiles (if they haven't already been expanded)
            for i in range(len(DIRS)):
                nextPos = (self.curr[0] + DIRS[i][0], DIRS[i][1] + self.curr[1])
                if nextPos not in self.dirList.keys():
                    self.expandList.append((dist + 1, self.curr, i, self.bot.get_state()))

        return nextTile

    def expand_front(self, limit):
        self.expandList.sort(reverse = True, key = lambda x: x[0])
        while len(self.expandList) > 0 and self.expandList[-1][0] <= limit:
            expand_next(sort = false)

    def find_min_dist(self):
        # stop when list empty, or goal found
        while len(self.expandList) > 0:
            if self.expand_next() == 2:
                break

        # Backtrace to find distance
        dist = 0
        curr = self.goalPos
        while curr != (0, 0):
            curr = (curr[0] + self.dirList[curr][0], curr[1] + self.dirList[curr][1])
            dist = dist + 1

        return dist

    def find_max_dist(self):
        maxDist = 0
        # Explore all possible parts of the list
        while len(self.expandList) > 0:
            result = self.expand_next()
            if result == None or result == 0:
                break
            else:
                maxDist = self.expandList[-1][0]

        # The thinking here is that the last place to be explored will be a wall which is next to the furtherest point
        return maxDist
Esempio n. 11
0
    def __getitem__(self,pt):
        """ Allow indexing by a Point. """
        if pt.x < 0 or pt.x >= self.size or pt.y < 0 or pt.y >= self.size:
            return '.'
        return self.maze[pt.y][pt.x]


TRACE = 'trace' in sys.argv

# Run the program once to get the path.

real = list(eval(open('day17.txt').read()))

pgm = Program(real)
pgm.run()
maze = Maze( pgm.dump() )
print(maze.string)

def part1():
    # Find the robot.

    robot = Point(maze.maze[-1].find('^'), len(maze.maze)-1)
    lastturn = robot

    # Robot starts facing north.

    facing = Point(0,-1) 

    crossings = set()
    turns = []
Esempio n. 12
0
from intcode import Program

# solved on paper
main = "A,B,A,B,C,A,B,C,A,C\n"
A = "R,6,L,6,L,10\n"
B = "L,8,L,6,L,10,L,6\n"
C = "R,6,L,8,L,10,R,6\n"

if __name__ == "__main__":
    camera = Program("input.txt")
    camera.program[0] = 2

    [camera.add_input(ord(c)) for c in main]
    [camera.add_input(ord(c)) for c in A]
    [camera.add_input(ord(c)) for c in B]
    [camera.add_input(ord(c)) for c in C]

    # no camera output
    camera.add_input(ord("n"))
    camera.add_input(ord("\n"))

    camera.run()
    print(camera.get_outputs()[-1])
Esempio n. 13
0
def inside(x, y):
    pgm = Program(real)
    pgm.push(x)
    pgm.push(y)
    pgm.run()
    return pgm.pop()
Esempio n. 14
0
def solve_hard(data):
    p = Program(data, [5])
    p.run()
    return p.outputs[0]
Esempio n. 15
0
def solve_easy(data):
    p = Program(data, [1])
    p.run()
    return p.outputs[-1]
Esempio n. 16
0
from intcode import Program

if __name__ == "__main__":
    camera = Program("input.txt")
    camera.run(stop = 3)

    grid = camera.get_outputs()
    gridStr = ''.join([chr(x) for x in grid])
    print(gridStr)

    # assumes bot doesnt start on intersection
    gridRows = gridStr.strip().split('\n')
    total = 0
    for row in range(1, len(gridRows) - 1):
        for col in range(1, len(gridRows[row]) - 1):
            if gridRows[row][col] == '#':
                if (gridRows[row + 1][col] == '#' and
                    gridRows[row - 1][col] == '#' and
                    gridRows[row][col - 1] == '#' and
                    gridRows[row][col + 1] == '#'):
                    total += row * col

    print(total)
Esempio n. 17
0
def run_boost(data, input_value):
    p = Program(data.copy(), [input_value])
    p.run()
    assert p.halted
    assert len(p.outputs) == 1
    return p.outputs[0]