コード例 #1
0
ファイル: day13.py プロジェクト: shoblin/Advent-of-Code-2019
def main():
    opcode_list = fa.read_input_values('day13_puzzle_input.txt')
    opcode_list = [int(thing) for thing in opcode_list]
    computer = Computer(opcode_list)

    blocks_num = 0

    game_ch = {0: ' ', 1: '|', 2: '#', 3: '-', 4: '0'}

    game_code = []
    while not computer.done:
        output = computer.calculate()
        if not computer.done:
            game_code.append(output)

    game_map = [['.' for _ in range(43)] for _ in range(23)]
    for idx in range(0, len(game_code), 3):
        if game_code[idx + 2] == 2: blocks_num += 1
        x, y, ch = game_code[idx], game_code[idx + 1], game_ch[game_code[idx +
                                                                         2]]

        ##        print(idx, x,y,ch)
        game_map[y][x] = ch
    result = 0
    for row in game_map:
        print(row)
        result += row.count('#')

    print(result, blocks_num)
コード例 #2
0
def main():
    opcode_list = fa.read_input_values('day13_puzzle_input.txt')
    opcode_list = [int(thing) for thing in opcode_list]
    opcode_list[0] = 2
    computer = Computer(opcode_list)

    ball_x = 0
    paddle_x = 0
    joystick_input = 0
    scrole = 0

    game_ch = {0: ' ', 1: '|', 2: '#', 3: '-', 4: '0'}
    game_code = {}

    blocks_num = 0
    ball_draw = False
    paddle_draw = False

    while not computer.done:
        x = computer.calculate()
        y = computer.calculate()
        title = computer.calculate()
        if not (x == -1 and y == 0):
            if title != None:
                game_code[(x, y)] = game_ch[title]
        elif x == -1 and y == 0:
            scrole = title

        if title == 3:
            paddle_x = x
            paddle_draw = True
            if ball_draw == True:
                ball_draw = False
                paddle_draw = False
                joystick_position(ball_x, paddle_x, computer)

        elif title == 4:
            ball_x = x
            ball_draw = True
            if paddle_draw == True or paddle_x != 0:
                ball_draw = False
                paddle_draw = False
                joystick_position(ball_x, paddle_x, computer)

        elif title == 2:
            blocks_num += 1


##        if blocks_num == 0:
##            break

##        computer.done = False

    print('*' * 20)
    print('* Scrole:', f'{scrole:8}', '*')
    print('*' * 20)
コード例 #3
0
def main():
    current_position = 0
    opcode_list = fa.read_input_values('day5_puzzle_input.txt')
    opcode_list = [int(thing) for thing in opcode_list]

    computer = Computer(opcode_list)

    while not computer.done:
        computer.calculate(5)
    print(computer.output)
コード例 #4
0
ファイル: day11.py プロジェクト: shoblin/Advent-of-Code-2019
class Robot_painter:
    '''
    Class of Robot
    Attributes:
    '''
    def __init__(self, input_vals, init_color=0):
        self.computer = Computer(input_vals)
        self.direction = 0
        self.x, self.y = 0, 0
        self.painted = {(self.x, self.y): init_color}

    def paint(self):
        while not self.computer.done:
            current_color = self.painted[(self.x, self.y)] if (
                self.x, self.y) in self.painted else 0
            self.painted[(self.x,
                          self.y)] = self.computer.calculate(current_color)
            self.change_direction(self.computer.calculate())
            self.rotate()

    def change_direction(self, rotate_direction):
        if rotate_direction == 0:
            self.direction = (self.direction - 1) % 4
        else:
            self.direction = (self.direction + 1) % 4

    def rotate(self):
        if self.direction == 0:
            self.y += 1
        elif self.direction == 1:
            self.x += 1
        elif self.direction == 2:
            self.y -= 1
        elif self.direction == 3:
            self.x -= 1

    def show_painting(self):
        data = [["." for _ in range(50)] for _ in range(6)]
        for x, y in self.painted.keys():
            color = self.painted[(x, y)]
            data[abs(y)][x] = "." if color == 0 else "#"
        for row in data:
            print(''.join(row))
コード例 #5
0
ファイル: day17.py プロジェクト: shoblin/Advent-of-Code-2019
def create_map(optcode):
    '''
    Create map: calculate map. Divide into lines, 10 - starts a new line
    '''
    map_codes = []
    line_code = []

    computer = Computer(optcode)
    while not computer.done:
        computer.calculate()
        if not computer.done:

            if computer.output == 10:
                if len(line_code) != 0:
                    map_codes.append(line_code[:])
                line_code = []
            else:
                line_code.append(computer.output)

    return map_codes
コード例 #6
0
def main():
    opcode_list = fa.read_input_values('day9_puzzle_input.txt')
    opcode_list = opcode_list + [0] * 2000
    opcode_list = [int(thing) for thing in opcode_list]

    input_signal = 1
    amp1 = Computer(opcode_list[:])
    output_signal1 = amp1.calculate(input_signal)
    print(output_signal1)

    input_signal = 2
    amp2 = Computer(opcode_list[:])
    output_signal2 = amp2.calculate(input_signal)
    print(output_signal2)
コード例 #7
0
ファイル: day7.py プロジェクト: shoblin/Advent-of-Code-2019
def main():
    opcode_list = fa.read_input_values('day7_puzzle_input.txt')
    opcode_list = [int(thing) for thing in opcode_list]

    ls = [x for x in range(5)]
    ls_full = [ls[:]]
    while next_step(ls):
        ls_full.append(ls[:])

    max_output_signal = 0
    for ls_permutation in ls_full:
        output_signal = 0
        for phase in ls_permutation:
            amp = Computer(opcode_list[:])
            amp.inputs.append(phase)
            output_signal = amp.calculate(output_signal)

        max_output_signal = max(max_output_signal, output_signal)

    print('Part1:', max_output_signal)

    ls = [x for x in range(5, 10)]
    ls_full = [ls[:]]
    while next_step(ls):
        ls_full.append(ls[:])

    max_output_signal = 0
    for ls_permutation in ls_full:
        output_signal = 0
        amps = [Computer(opcode_list) for _ in range(5)]
        for amp, phase_setting in zip(amps, ls_permutation):
            amp.inputs.append(phase_setting)

        while amps[-1].done == False:
            for amp in amps:
                amp.calculate(output_signal)
                output_signal = amp.output

        max_output_signal = max(output_signal, max_output_signal)
    print(f"Part 2: {max_output_signal}")
コード例 #8
0
ファイル: day11.py プロジェクト: shoblin/Advent-of-Code-2019
 def __init__(self, input_vals, init_color=0):
     self.computer = Computer(input_vals)
     self.direction = 0
     self.x, self.y = 0, 0
     self.painted = {(self.x, self.y): init_color}