Last active
December 22, 2021 01:02
-
-
Save vincentwoo/7fa2e268e38fb4f24ff574bb7aafa5cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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