Skip to content

Instantly share code, notes, and snippets.

@wirepair
Created June 16, 2024 12:58
Show Gist options
  • Save wirepair/8db6f2b8d37e4f0186a8d172799cebb3 to your computer and use it in GitHub Desktop.
Save wirepair/8db6f2b8d37e4f0186a8d172799cebb3 to your computer and use it in GitHub Desktop.
create set of visibility for each character
characters = [
{'id': 1, 'visible': set()},
{'id': 2, 'visible': set()},
{'id': 3, 'visible': set()},
{'id': 4, 'visible': set()},
{'id': 5, 'visible': set()},
{'id': 6, 'visible': set()},
]
mapchunks = [
set((1,2,3)),
set((3,4,6)),
set((5,2,1,3))
]
for ch in characters:
for chunk in mapchunks:
if ch['id'] in chunk:
ch['visible'] |= chunk
print(f"player: {ch['id']} has {len(ch['visible'])} visible players: {ch['visible']}")
# output:
# player: 1 has 4 visible players: {1, 2, 3, 5}
# player: 2 has 4 visible players: {1, 2, 3, 5}
# player: 3 has 6 visible players: {1, 2, 3, 4, 5, 6}
# player: 4 has 3 visible players: {3, 4, 6}
# player: 5 has 4 visible players: {1, 2, 3, 5}
# player: 6 has 3 visible players: {3, 4, 6}
# Copilot recommended a slight improvement:
# # Create a dictionary mapping each character id to its visible set
# visible_dict = {ch['id']: ch['visible'] for ch in characters}
# # Iterate over the mapchunks once
# for chunk in mapchunks:
# for id in chunk:
# if id in visible_dict:
# visible_dict[id] |= chunk
# # Print the results
# for ch in characters:
# print(f"player: {ch['id']} has {len(ch['visible'])} visible players: {ch['visible']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment