0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Hit&Blow(ヒットアンドブロー)のソルバーをpythonで書いてみた

Last updated at Posted at 2021-08-11

はじめに

先日、世界のアソビ大全をやっていたのですが、「ヒット&ブロー」っていうNumer0n(ヌメロン)みたいなやつが面白かったのでソルバーを作ってみました。

仕様

青→1 赤→2 緑→3 黄→4 紫→5 白→6
で4つの数字とヒット・ブローの判定結果を入力することで、正解の候補を出力します。

コード

言語はPythonを使用しました。
※答えの同色はなしがデフォルトで設定されています。

solver.py
import copy

def same_number_check(lst):
    for i in lst:
        if lst.count(i) >= 2:
            return True
    return False

def h_and_b_check(a_lst, b_lst):
    x = 0
    y = 0
    for i in range(4):
        if a_lst[i] == b_lst[i]:
            x += 1
            y -= 1
    for i in a_lst:
        if i in b_lst:
            y += 1
    
    return x, y

def answer_check(n_lst, h, b): 
    for i in range(len(answer)):
        if flag[i]:
            x, y = h_and_b_check(answer[i], n_lst)
            if h == x and b == y:
                pass
            else:
                flag[i] = False

answer = []

for i in range(5556):
    t = list(str('{0:04d}'.format(i)))
    t_int = [int(s) for s in t]
    if 6 in t_int or 7 in t_int or 8 in t_int or 9 in t_int:
        pass
    else:
        answer.append(t_int)

flag = [True]*len(answer)

# 同色なしの場合
for i in range(len(answer)):
    if same_number_check(answer[i]):
        flag[i] = False

while True:
    numbers = list(map(int, input().split()))
    h, b = list(map(int, input().split()))
    plus_numbers = list(map(lambda x: x-1, numbers))
    answer_check(plus_numbers, h, b)
    for i in range(len(answer)):
        if flag[i]:
            plus_answer = list(map(lambda x: x+1, answer[i]))
            print(plus_answer)

Github↓
kk0195736/hit-and-blow-solver

おわりに

これで無双できる。

0
1
16

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?