Skip to content

Instantly share code, notes, and snippets.

@vincentwoo
Last active December 22, 2021 01:02
Show Gist options
  • Save vincentwoo/7fa2e268e38fb4f24ff574bb7aafa5cf to your computer and use it in GitHub Desktop.
Save vincentwoo/7fa2e268e38fb4f24ff574bb7aafa5cf to your computer and use it in GitHub Desktop.
def run input
p search([input[0] - 1, 0], [input[1] - 1, 0]).max
end
$rolls = [1, 2, 3]
$rolls = $rolls.product($rolls, $rolls).map(&:sum)
$memo = {}
def search *players
return $memo[players] if $memo[players]
(position, score), (_, p2_score) = players
return [0, 1] if p2_score >= 21
wins = $rolls.map do |roll|
_position = (position + roll) % 10
search players.last, [_position, score + _position + 1]
end
$memo[players] = wins.transpose.reverse.map(&:sum)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment