Skip to content

Instantly share code, notes, and snippets.

@t-ob
Created September 19, 2023 19:01
Show Gist options
  • Save t-ob/6b30ddb19b5aafbdfc786bddb32edf2f to your computer and use it in GitHub Desktop.
Save t-ob/6b30ddb19b5aafbdfc786bddb32edf2f to your computer and use it in GitHub Desktop.
import sys
import random
if __name__ == "__main__":
code = random.choices([1, 2, 3, 4, 5, 6], k=4)
print(f"{code=}")
turns_remaining = 10
for line in sys.stdin:
input_nums = [int(c) for c in line.strip().split()]
perfect_matches = [x == y for x, y in zip(code, input_nums)]
num_perfect = sum(1 for c in perfect_matches if c)
remaining_indexes = []
for idx, matches in enumerate(perfect_matches):
if not matches:
remaining_indexes.append(idx)
remaining_code = [code[idx] for idx in remaining_indexes]
remaining_input = [input_nums[idx] for idx in remaining_indexes]
num_imperfect = 0
for i in [1, 2, 3, 4, 5, 6]:
remaining_code_equal_i = sum(1 for c in remaining_code if c == i)
remaining_input_equal_i = sum(1 for c in remaining_input if c == i)
num_imperfect += min(remaining_code_equal_i, remaining_input_equal_i)
white_pegs = "O" * num_imperfect
black_pegs = "*" * num_perfect
num_blank_pegs = 4 - (num_imperfect + num_perfect)
blank_pegs = "_" * num_blank_pegs
print(f"{black_pegs}{white_pegs}{blank_pegs}")
if num_perfect == 4:
print("you win!")
break
turns_remaining -= 1
if turns_remaining == 0:
print("you lose!")
break
print(
f"{line=}, {input_nums=}, {perfect_matches=}, {remaining_code=}, {remaining_input=}, {num_imperfect=} "
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment