카테고리 없음

[Rankk]Level2/5: Mastermind

ab0utcom 2026. 7. 8. 14:53

4자리 숫자야구를 180초동안 2번 성공해야 한다.

근데 6번 틀리면 초기화된다.

머리로 하면 아무리봐도 못할 것 같고, 페이지 코드나 쿠키 이런거 봐도 모르겠어서 코드를 짰다.

 

import itertools
from collections import Counter

def get_hints(guess, secret):
    """현재 추측값과 비밀값 간의 A(스트라이크)와 B(볼) 개수를 계산합니다."""
    A = sum(1 for g, s in zip(guess, secret) if g == s)
    g_remain = [g for g, s in zip(guess, secret) if g != s]
    s_remain = [s for g, s in zip(guess, secret) if g != s]
    
    g_cnt = Counter(g_remain)
    s_cnt = Counter(s_remain)
    B = sum(min(g_cnt[k], s_cnt[k]) for k in g_cnt)
    
    return A, B

def main():
    print("====================================================")
    print("   Rankk Mastermind Solver (넌스톱 무한 연타 버전)")
    print("====================================================")
    
    digits = "123456789"
    
    while True:
        pool = ["".join(p) for p in itertools.product(digits, repeat=4)]
        turn = 1
        
        print("\n" + "="*55)
        print("▶ 새로운 게임 세션 시작 (총 후보군: 6,561개)")
        print("="*55)
        
        while turn <= 6:
            if turn == 1:
                guess = "1122"
            else:
                if not pool:
                    print("\n❌ 데이터 오류: 조건에 맞는 숫자가 없습니다. 자동 리셋합니다.")
                    break
                guess = pool[0]
                
            print(f"\n[{turn} / 6 회차] 추천 입력값 👉 [ {guess} ]")
            
            # 6회차 특수 분기: 입력 후 엔터만 치면 조건 없이 즉시 새 게임으로 이동
            if turn == 6:
                input("  👉 마지막 6회차입니다. 사이트 입력 후 [Enter]를 누르면 다음 판이 바로 시작됩니다.")
                break
                
            print(f"  (현재 필터링되어 남은 정답 후보: {len(pool)}개)")
            
            # 1~5회차 정상 입력 프로세스
            try:
                A = int(input("  사이트 결과 'A' 개수 입력: "))
                B = int(input("  사이트 결과 'B' 개수 입력: "))
            except ValueError:
                print("  ⚠️ 숫자로만 정확히 입력해 주세요. 이번 회차를 다시 진행합니다.")
                continue
                
            # 1~5회차 중 정답을 맞춘 경우 (4A)
            if A == 4:
                print(f"\n🎉 성공! 4A 정답입니다. 다음 게임으로 즉시 넘어갑니다.")
                break
                
            pool = [candidate for candidate in pool if get_hints(guess, candidate) == (A, B)]
            turn += 1

if __name__ == "__main__":
    main()

 현재 상황에서 추천 번호 조합을 만들어주고, 그걸 입력했을 때

Correct digit and correct position: A

Correct digit but wrong position: B로 간주하고 각각의 개수를 적으면 된다.

 

실패할 때도 있는데, 운이 좋으면 성공할 수 있다.

 

진행하다가 웃는 얼굴의 파라오 아이콘이 뜨면 1번 성공한거다. 그 상태에서 1번 더 성공하면 자동으로 클리어된다.