๐ก ๋ฌธ์ ๋ ๋ค์๊ณผ ๊ฐ๋ค
ํ์ด
def emoji_stack_interpreter(program):
# Initialize the stack (256 cells) and the pointer
stack = [0] * 256
pointer = 0
output = []
# Create a pointer for reading the program
i = 0
# Helper function to get the current value at the pointer
def get_value():
return stack[pointer]
# Helper function to update the stack with modular arithmetic
def set_value(value):
stack[pointer] = value % 256
while i < len(program):
cmd = program[i]
if cmd == ':point_right:':
pointer = (pointer + 1) % 256 # Move right, wrap around if needed
elif cmd == ':point_left:':
pointer = (pointer - 1) % 256 # Move left, wrap around if needed
elif cmd == ':thumbsup:':
set_value(get_value() + 1) # Increment the current cell
elif cmd == ':thumbsdown:':
set_value(get_value() - 1) # Decrement the current cell
elif cmd == ':speech_balloon:':
output.append(chr(get_value())) # Print the ASCII value of the current cell
elif cmd == ':repeat:':
# Repeat the previous instruction (next two characters represent the hex value)
repeat_count = int(program[i+1:i+3], 16)
previous_cmd = program[i-1]
for _ in range(repeat_count):
if previous_cmd == ':thumbsup:':
set_value(get_value() + 1)
elif previous_cmd == ':thumbsdown:':
set_value(get_value() - 1)
elif previous_cmd == ':point_right:':
pointer = (pointer + 1) % 256
elif previous_cmd == ':point_left:':
pointer = (pointer - 1) % 256
i += 2 # Skip the next two hex characters
i += 1
return ''.join(output)
# Read the content of the input file
file_path = 'input.txt'
with open(file_path, 'r') as file:
program = file.read()
# Run the interpreter
flag = emoji_stack_interpreter(program)
print(flag)
1. . emoji_stack_interpreter ํจ์ ์ ์
• ์คํ ์ด๊ธฐํ:
• stack = [0] * 256: 256๊ฐ์ ์ ๋ก ๊ตฌ์ฑ๋ ์คํ์ 0์ผ๋ก ์ด๊ธฐํํฉ๋๋ค.
• pointer = 0: ์คํ ํฌ์ธํฐ๋ ์ฒ์์ ์ฒซ ๋ฒ์งธ ์ ์ ๊ฐ๋ฆฌํต๋๋ค.
• output = []: ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ๋ด์ ๋ฆฌ์คํธ์ ๋๋ค.
• get_value ํจ์: ํ์ฌ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ์ ์ ๊ฐ์ ๋ฐํํ๋ ํจ์์ ๋๋ค.
• set_value ํจ์: ํ์ฌ ์ ์ ๊ฐ์ ๋ฃ์ ๋ 256์ผ๋ก ๋๋ ๋๋จธ์ง๋ฅผ ์ ์ฅํฉ๋๋ค. ์ด๋ ์ ๊ฐ์ด 0~255 ์ฌ์ด์ ์๋๋ก ๋ณด์ฅํฉ๋๋ค.
2. while ๋ฃจํ:
ํ๋ก๊ทธ๋จ์ ๊ฐ ๋ช ๋ น์ ํ ๊ธ์์ฉ ์ฝ์ผ๋ฉด์ ์ฒ๋ฆฌํฉ๋๋ค.
• ํฌ์ธํฐ ์ด๋ (๐, ๐):
• ๐: ์คํ ํฌ์ธํฐ๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํฉ๋๋ค. pointer = (pointer + 1) % 256๋ก, 256์ ๋์ด๊ฐ๋ฉด ๋ค์ ์ฒ์์ผ๋ก ๋์์ต๋๋ค.
• ๐: ์คํ ํฌ์ธํฐ๋ฅผ ์ผ์ชฝ์ผ๋ก ์ด๋ํฉ๋๋ค. pointer = (pointer - 1) % 256๋ก, 0์์ ์ผ์ชฝ์ผ๋ก ๊ฐ๋ฉด ๋ง์ง๋ง ์ ๋ก ์ด๋ํฉ๋๋ค.
• ๊ฐ ์ฆ๊ฐ ๋ฐ ๊ฐ์ (๐, ๐):
• ๐: ํ์ฌ ์ ๊ฐ์ 1 ์ฆ๊ฐ์ํต๋๋ค.
• ๐: ํ์ฌ ์ ๊ฐ์ 1 ๊ฐ์์ํต๋๋ค.
• ์ถ๋ ฅ ๋ช ๋ น (๐ฌ):
• ํ์ฌ ์ ์ ๊ฐ์ ASCII ๋ฌธ์๋ก ๋ณํํด output ๋ฆฌ์คํธ์ ์ถ๊ฐํฉ๋๋ค. chr(get_value())๋ฅผ ํตํด ASCII ๋ณํ์ ์ํํฉ๋๋ค.
• ๋ฐ๋ณต ์ฒ๋ฆฌ (๐):
• ๐ ๋ค์์ ์ค๋ ๋ ์๋ฆฌ ์ซ์๋ 16์ง์๋ก ๋ช ๋ฒ ๋ฐ๋ณตํ ์ง ๋ํ๋ ๋๋ค. ์๋ฅผ ๋ค์ด ๐47์ ์ง์ ๋ช ๋ น์ 0x47(์ฆ, 71๋ฒ) ๋ฐ๋ณตํ๋ ๊ฒ์ ๋๋ค.
• ์ด ๋ ์ง์ ๋ช ๋ น(๐, ๐, ๐, ๐)์ ๋ฐ๋ณตํด์ ์คํํฉ๋๋ค.
3. ํ์ผ ์ฝ๊ธฐ:
input.txt ํ์ผ์ ์ฝ์ด Emoji Stack ํ๋ก๊ทธ๋จ ๋ด์ฉ์ ๊ฐ์ ธ์ต๋๋ค.
4. ํด์๊ธฐ ์คํ:
emoji_stack_interpreter(program) ํจ์์ ํ์ผ์์ ์ฝ์ ํ๋ก๊ทธ๋จ์ ์ ๋ฌํ์ฌ ์คํํ ํ, ์ถ๋ ฅ๊ฐ(ํ๋๊ทธ)์ ๋ฐํ
'CTF > Misc' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2024 snakectf - Unlucky Cactus (2) | 2024.09.08 |
---|---|
CCE 2024 Qulas - HWKborad (0) | 2024.08.04 |