Skip to content

Instantly share code, notes, and snippets.

@aadishv
Created December 28, 2025 01:04
Show Gist options
  • Select an option

  • Save aadishv/3661bcf18f52ae997d13659bef120033 to your computer and use it in GitHub Desktop.

Select an option

Save aadishv/3661bcf18f52ae997d13659bef120033 to your computer and use it in GitHub Desktop.
ACSL 2024 contest 2 programming problem senior div. I wrote a way more complex, 51-line mess and rewrote it from scratch to be simpler after it was failing some test cases and I had no idea why. Fun fact: that mess was also a valid solution, it was failing because the test cases I were pasting in had invalid formatting (curse you, PDF text wrapp…
def matches(card1: str, card2: str) -> bool:
if len(card1) == 1 or len(card2) == 1: # 1+1 | 1+3 | 3+1
return (card1 in card2) or (card2 in card1)
else: # 3+3
return sum([1 if i == j else 0 for i, j in zip(card1, card2)]) == 2
piles = input().split()
hand = input().split()
draw_pile = input().split()
while True:
# one turn
try:
pile = next(pile_index[0] for pile_index in [
[i for i in range(2) if matches(card, piles[i])] for card in hand
] if pile_index)
except Exception:
# there are no more matching cards
break
while True:
try:
i, card = next((i, card) for i, card in enumerate(hand) if matches(card, piles[pile]))
hand.pop(i)
piles[pile] = card
except Exception: break
while len(hand) < 7 and draw_pile:
hand.append(draw_pile.pop(0))
print(len(hand), *piles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment