22_crab_combat.rb
# The game runs fast enough that no particular optimisation is needed.
The game runs fast enough that no particular optimisation is needed.
The game runs fast enough that no particular optimisation is needed .
def game(deck1, deck2)
  seen = Set.new
  while true
    return [:loop, []] unless seen.add?(compress_decks(deck1, deck2))
    card1 = deck1.shift
    card2 = deck2.shift
    if card1 > card2
      deck1 << card1
      deck1 << card2
      return [1, deck1] if deck2.empty?
    else
      deck2 << card2
      deck2 << card1
      return [2, deck2] if deck1.empty?
    end
  end
end
def game ( deck1 , deck2 ) seen = Set . new while true return [ :loop , [ ] ] unless seen . add? ( compress_decks ( deck1 , deck2 ) ) card1 = deck1 . shift card2 = deck2 . shift if card1 > card2 deck1 << card1 deck1 << card2 return [ 1 , deck1 ] if deck2 . empty? else deck2 << card2 deck2 << card1 return [ 2 , deck2 ] if deck1 . empty? end end end
The game runs fast enough that no particular optimisation is needed.
The game runs fast enough that no particular optimisation is needed .
# Wasn't asked of us in part 1, but I sometimes manually test inputs that cause it.
deck1
<not_specific>
deck2
<not_specific>