It's that time again, time for a new post in the chronicle of me teaching myself the Rust programming language by solving programming puzzles from Advent of Code 2020.
Day 22, Part 1
Today's puzzle is about the card game of Combat 1 , a two-player game with a numbered deck of cards. Each player takes a card off the top of the deck, and whoever has the highest card takes both. When one player has no cards left, the other player wins. The input to the puzzle is the cards in each deck, and the answer is the winning player's score: the bottom card of their deck times 1, plus the next bottom-most card times 2, plus the next bottom-most card times 3, etc.
I read about VecDeque at the same time I read about Vec , on the very first day I started learning Rust with these puzzles, but I haven't had an opportunity to use it yet. However, this seems like one. The program is quite straightforward:
I'm also happy that I get all the operators right this time. Sure, it's a small program, but the achievement feels good.
Day 22, Part 2
Part 2 is a bit more complicated: we have to play Recursive Combat. Each player draws a card. If both players have enough cards in their e of Recursive Combat with the top cards in their deck, as many as the number on the card they drew, and the winner of the sub-game takes both cards. If either player doesn't have enough cards, then whoever had the higher card takes both cards. In the case of an infinite loop, Player 1 wins outright. The scoring rules are the same.
I'm again able to write this fairly straightforwardly. What I write at first has a few bugs, but once again writing inline tests based on the examples in the puzzle description https://homeloansplus.org/payday-loans-ak/ helped me debug them. Here's the code:
Day 23, Part 1
Today's puzzle is simulating the outcome of yet another game. There are 10 cups, arranged in a circle, each labelled with a number, and one is the “current cup”. The three cups after the current cup are picked up, and inserted after the “destination cup”, which is the cup labelled with the current cup's number minus 1. (If that cup is one of the ones picked up, then the destination cup is the current cup minus 2, and so on.) Then the current cup shifts to the next in the circle.