aoc-2019-c

Advent of Code 2019 Solutions in C
git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

part1 (5111B)


      1--- Day 22: Slam Shuffle ---
      2
      3There isn't much to do while you wait for the droids to repair your ship.  At least you're drifting
      4in the right direction.  You decide to practice a new card shuffle you've been working on.
      5
      6Digging through the ship's storage, you find a deck of space cards! Just like any deck of space
      7cards, there are 10007 cards in the deck numbered 0 through 10006. The deck must be new - they're
      8still in factory order, with 0 on the top, then 1, then 2, and so on, all the way through to 10006
      9on the bottom.
     10
     11You've been practicing three different techniques that you use while shuffling. Suppose you have a
     12deck of only 10 cards (numbered 0 through 9):
     13
     14To deal into new stack, create a new stack of cards by dealing the top card of the deck onto the top
     15of the new stack repeatedly until you run out of cards:
     16
     17Top          Bottom
     180 1 2 3 4 5 6 7 8 9   Your deck
     19                      New stack
     20
     21  1 2 3 4 5 6 7 8 9   Your deck
     22                  0   New stack
     23
     24    2 3 4 5 6 7 8 9   Your deck
     25                1 0   New stack
     26
     27      3 4 5 6 7 8 9   Your deck
     28              2 1 0   New stack
     29
     30Several steps later...
     31
     32                  9   Your deck
     33  8 7 6 5 4 3 2 1 0   New stack
     34
     35                      Your deck
     369 8 7 6 5 4 3 2 1 0   New stack
     37
     38Finally, pick up the new stack you've just created and use it as the deck for the next technique.
     39
     40To cut N cards, take the top N cards off the top of the deck and move them as a single unit to the
     41bottom of the deck, retaining their order. For example, to cut 3:
     42
     43Top          Bottom
     440 1 2 3 4 5 6 7 8 9   Your deck
     45
     46      3 4 5 6 7 8 9   Your deck
     470 1 2                 Cut cards
     48
     493 4 5 6 7 8 9         Your deck
     50              0 1 2   Cut cards
     51
     523 4 5 6 7 8 9 0 1 2   Your deck
     53
     54You've also been getting pretty good at a version of this technique where N is negative! In that
     55case, cut (the absolute value of) N cards from the bottom of the deck onto the top.  For example, to
     56cut -4:
     57
     58Top          Bottom
     590 1 2 3 4 5 6 7 8 9   Your deck
     60
     610 1 2 3 4 5           Your deck
     62            6 7 8 9   Cut cards
     63
     64        0 1 2 3 4 5   Your deck
     656 7 8 9               Cut cards
     66
     676 7 8 9 0 1 2 3 4 5   Your deck
     68
     69To deal with increment N, start by clearing enough space on your table to lay out all of the cards
     70individually in a long line.  Deal the top card into the leftmost position. Then, move N positions
     71to the right and deal the next card there. If you would move into a position past the end of the
     72space on your table, wrap around and keep counting from the leftmost card again.  Continue this
     73process until you run out of cards.
     74
     75For example, to deal with increment 3:
     76
     77
     780 1 2 3 4 5 6 7 8 9   Your deck
     79. . . . . . . . . .   Space on table
     80^                     Current position
     81
     82Deal the top card to the current position:
     83
     84  1 2 3 4 5 6 7 8 9   Your deck
     850 . . . . . . . . .   Space on table
     86^                     Current position
     87
     88Move the current position right 3:
     89
     90  1 2 3 4 5 6 7 8 9   Your deck
     910 . . . . . . . . .   Space on table
     92      ^               Current position
     93
     94Deal the top card:
     95
     96    2 3 4 5 6 7 8 9   Your deck
     970 . . 1 . . . . . .   Space on table
     98      ^               Current position
     99
    100Move right 3 and deal:
    101
    102      3 4 5 6 7 8 9   Your deck
    1030 . . 1 . . 2 . . .   Space on table
    104            ^         Current position
    105
    106Move right 3 and deal:
    107
    108        4 5 6 7 8 9   Your deck
    1090 . . 1 . . 2 . . 3   Space on table
    110                  ^   Current position
    111
    112Move right 3, wrapping around, and deal:
    113
    114          5 6 7 8 9   Your deck
    1150 . 4 1 . . 2 . . 3   Space on table
    116    ^                 Current position
    117
    118And so on:
    119
    1200 7 4 1 8 5 2 9 6 3   Space on table
    121
    122Positions on the table which already contain cards are still counted; they're not skipped.  Of
    123course, this technique is carefully designed so it will never put two cards in the same position or
    124leave a position empty.
    125
    126Finally, collect the cards on the table so that the leftmost card ends up at the top of your deck,
    127the card to its right ends up just below the top card, and so on, until the rightmost card ends up
    128at the bottom of the deck.
    129
    130The complete shuffle process (your puzzle input) consists of applying many of these techniques. 
    131Here are some examples that combine techniques; they all start with a factory order deck of 10
    132cards:
    133
    134deal with increment 7
    135deal into new stack
    136deal into new stack
    137Result: 0 3 6 9 2 5 8 1 4 7
    138
    139cut 6
    140deal with increment 7
    141deal into new stack
    142Result: 3 0 7 4 1 8 5 2 9 6
    143
    144deal with increment 7
    145deal with increment 9
    146cut -2
    147Result: 6 3 0 7 4 1 8 5 2 9
    148
    149deal into new stack
    150cut -2
    151deal with increment 7
    152cut 8
    153cut -4
    154deal with increment 7
    155cut 3
    156deal with increment 9
    157deal with increment 3
    158cut -1
    159Result: 9 2 5 8 1 4 7 0 3 6
    160
    161Positions within the deck count from 0 at the top, then 1 for the card immediately below the top
    162card, and so on to the bottom.  (That is, cards start in the position matching their number.)
    163
    164After shuffling your factory order deck of 10007 cards, what is the position of card 2019?
    165
    166