Post

๐ŸฆŠ ๐Ÿ”„ ๊ท€๋„ ๋ฐ˜ ๋กœ์ธ์€ ํฌ๋ฆฌ์Šค๋งˆ์Šค๋‚  ์‹ฌ์‹ฌํ•˜๋‹ค๊ณ  ํŒŒ์ด์ฌ์„ ๋งŒ๋“ค์—ˆ๋‹ค

1. ๋ฌธ์ œ ๋งํฌ

6568๋ฒˆ: ๊ท€๋„ ๋ฐ˜ ๋กœ์ธ์€ ํฌ๋ฆฌ์Šค๋งˆ์Šค๋‚  ์‹ฌ์‹ฌํ•˜๋‹ค๊ณ  ํŒŒ์ด์ฌ์„ ๋งŒ๋“ค์—ˆ๋‹ค


2. ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
PC = 0
ADD = 0
MEM = [0] * 32

while True:
    try:
        inp = input()
        command = inp[:3]
        mem = inp[3:8]

        # ๋ช…๋ น์–ด ์‹คํ–‰ ์ „ PC ์ฆ๊ฐ€
        PC += 1
        if command == '000': # ๊ฐ€์‚ฐ๊ธฐ์— ์ €์žฅ
            address = int(mem, 2)
            MEM[address] = ADD
        elif command == '001': # ๊ฐ€์‚ฐ๊ธฐ๋กœ ๋ถˆ๋Ÿฌ์˜ด
            address = int(mem, 2)
            ADD = MEM[address]
        elif command == '010': # ๊ฐ€์‚ฐ๊ธฐ๊ฐ€ 0์ด๋ฉด PC์— mem ์ €์žฅ
            if ADD == 0:
                address = int(mem, 2)
                PC = MEM[address]
        elif command == '011': # nop
            continue
        elif command == '100': # ๊ฐ€์‚ฐ๊ธฐ --
            ADD -= 1
        elif command == '101': # ๊ฐ€์‚ฐ๊ธฐ ++
            ADD += 1
        elif command == '110': # PC ๋ฅผ x๋กœ ๋ฐ”๊พผ๋‹ค
            address = int(mem, 2)
            PC = MEM[address]
        elif command == '111': # ์ข…๋ฃŒ
            continue
    except:
        break # EOF

# print(int(ADD, 2))
binary_string = format(ADD, 'b')
binary_string = binary_string.zfill(8)
print(binary_string)


This post is licensed under CC BY 4.0 by the author.