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 (8543B)


      1--- Day 12: The N-Body Problem ---
      2
      3The space near Jupiter is not a very safe place; you need to be careful of a big distracting red
      4spot, extreme radiation, and a whole lot of moons swirling around.  You decide to start by tracking
      5the four largest moons: Io, Europa, Ganymede, and Callisto.
      6
      7After a brief scan, you calculate the position of each moon (your puzzle input). You just need to
      8simulate their motion so you can avoid them.
      9
     10Each moon has a 3-dimensional position (x, y, and z) and a 3-dimensional velocity.  The position of
     11each moon is given in your scan; the x, y, and z velocity of each moon starts at 0.
     12
     13Simulate the motion of the moons in time steps. Within each time step, first update the velocity of
     14every moon by applying gravity. Then, once all moons' velocities have been updated, update the
     15position of every moon by applying velocity. Time progresses by one step once all of the positions
     16are updated.
     17
     18To apply gravity, consider every pair of moons. On each axis (x, y, and z), the velocity of each
     19moon changes by exactly +1 or -1 to pull the moons together.  For example, if Ganymede has an x
     20position of 3, and Callisto has a x position of 5, then Ganymede's x velocity changes by +1 (because
     215 > 3) and Callisto's x velocity changes by -1 (because 3 < 5). However, if the positions on a given
     22axis are the same, the velocity on that axis does not change for that pair of moons.
     23
     24Once all gravity has been applied, apply velocity: simply add the velocity of each moon to its own
     25position. For example, if Europa has a position of x=1, y=2, z=3 and a velocity of x=-2, y=0,z=3,
     26then its new position would be x=-1, y=2, z=6. This process does not modify the velocity of any
     27moon.
     28
     29For example, suppose your scan reveals the following positions:
     30
     31<x=-1, y=0, z=2>
     32<x=2, y=-10, z=-7>
     33<x=4, y=-8, z=8>
     34<x=3, y=5, z=-1>
     35
     36Simulating the motion of these moons would produce the following:
     37
     38After 0 steps:
     39pos=<x=-1, y=  0, z= 2>, vel=<x= 0, y= 0, z= 0>
     40pos=<x= 2, y=-10, z=-7>, vel=<x= 0, y= 0, z= 0>
     41pos=<x= 4, y= -8, z= 8>, vel=<x= 0, y= 0, z= 0>
     42pos=<x= 3, y=  5, z=-1>, vel=<x= 0, y= 0, z= 0>
     43
     44After 1 step:
     45pos=<x= 2, y=-1, z= 1>, vel=<x= 3, y=-1, z=-1>
     46pos=<x= 3, y=-7, z=-4>, vel=<x= 1, y= 3, z= 3>
     47pos=<x= 1, y=-7, z= 5>, vel=<x=-3, y= 1, z=-3>
     48pos=<x= 2, y= 2, z= 0>, vel=<x=-1, y=-3, z= 1>
     49
     50After 2 steps:
     51pos=<x= 5, y=-3, z=-1>, vel=<x= 3, y=-2, z=-2>
     52pos=<x= 1, y=-2, z= 2>, vel=<x=-2, y= 5, z= 6>
     53pos=<x= 1, y=-4, z=-1>, vel=<x= 0, y= 3, z=-6>
     54pos=<x= 1, y=-4, z= 2>, vel=<x=-1, y=-6, z= 2>
     55
     56After 3 steps:
     57pos=<x= 5, y=-6, z=-1>, vel=<x= 0, y=-3, z= 0>
     58pos=<x= 0, y= 0, z= 6>, vel=<x=-1, y= 2, z= 4>
     59pos=<x= 2, y= 1, z=-5>, vel=<x= 1, y= 5, z=-4>
     60pos=<x= 1, y=-8, z= 2>, vel=<x= 0, y=-4, z= 0>
     61
     62After 4 steps:
     63pos=<x= 2, y=-8, z= 0>, vel=<x=-3, y=-2, z= 1>
     64pos=<x= 2, y= 1, z= 7>, vel=<x= 2, y= 1, z= 1>
     65pos=<x= 2, y= 3, z=-6>, vel=<x= 0, y= 2, z=-1>
     66pos=<x= 2, y=-9, z= 1>, vel=<x= 1, y=-1, z=-1>
     67
     68After 5 steps:
     69pos=<x=-1, y=-9, z= 2>, vel=<x=-3, y=-1, z= 2>
     70pos=<x= 4, y= 1, z= 5>, vel=<x= 2, y= 0, z=-2>
     71pos=<x= 2, y= 2, z=-4>, vel=<x= 0, y=-1, z= 2>
     72pos=<x= 3, y=-7, z=-1>, vel=<x= 1, y= 2, z=-2>
     73
     74After 6 steps:
     75pos=<x=-1, y=-7, z= 3>, vel=<x= 0, y= 2, z= 1>
     76pos=<x= 3, y= 0, z= 0>, vel=<x=-1, y=-1, z=-5>
     77pos=<x= 3, y=-2, z= 1>, vel=<x= 1, y=-4, z= 5>
     78pos=<x= 3, y=-4, z=-2>, vel=<x= 0, y= 3, z=-1>
     79
     80After 7 steps:
     81pos=<x= 2, y=-2, z= 1>, vel=<x= 3, y= 5, z=-2>
     82pos=<x= 1, y=-4, z=-4>, vel=<x=-2, y=-4, z=-4>
     83pos=<x= 3, y=-7, z= 5>, vel=<x= 0, y=-5, z= 4>
     84pos=<x= 2, y= 0, z= 0>, vel=<x=-1, y= 4, z= 2>
     85
     86After 8 steps:
     87pos=<x= 5, y= 2, z=-2>, vel=<x= 3, y= 4, z=-3>
     88pos=<x= 2, y=-7, z=-5>, vel=<x= 1, y=-3, z=-1>
     89pos=<x= 0, y=-9, z= 6>, vel=<x=-3, y=-2, z= 1>
     90pos=<x= 1, y= 1, z= 3>, vel=<x=-1, y= 1, z= 3>
     91
     92After 9 steps:
     93pos=<x= 5, y= 3, z=-4>, vel=<x= 0, y= 1, z=-2>
     94pos=<x= 2, y=-9, z=-3>, vel=<x= 0, y=-2, z= 2>
     95pos=<x= 0, y=-8, z= 4>, vel=<x= 0, y= 1, z=-2>
     96pos=<x= 1, y= 1, z= 5>, vel=<x= 0, y= 0, z= 2>
     97
     98After 10 steps:
     99pos=<x= 2, y= 1, z=-3>, vel=<x=-3, y=-2, z= 1>
    100pos=<x= 1, y=-8, z= 0>, vel=<x=-1, y= 1, z= 3>
    101pos=<x= 3, y=-6, z= 1>, vel=<x= 3, y= 2, z=-3>
    102pos=<x= 2, y= 0, z= 4>, vel=<x= 1, y=-1, z=-1>
    103
    104Then, it might help to calculate the total energy in the system. The total energy for a single moon
    105is its potential energy multiplied by its kinetic energy. A moon's potential energy is the sum of
    106the absolute values of its x, y, and z position coordinates. A moon's kinetic energy is the sum of
    107the absolute values of its velocity coordinates.  Below, each line shows the calculations for a
    108moon's potential energy (pot), kinetic energy (kin), and total energy:
    109
    110Energy after 10 steps:
    111pot: 2 + 1 + 3 =  6;   kin: 3 + 2 + 1 = 6;   total:  6 * 6 = 36
    112pot: 1 + 8 + 0 =  9;   kin: 1 + 1 + 3 = 5;   total:  9 * 5 = 45
    113pot: 3 + 6 + 1 = 10;   kin: 3 + 2 + 3 = 8;   total: 10 * 8 = 80
    114pot: 2 + 0 + 4 =  6;   kin: 1 + 1 + 1 = 3;   total:  6 * 3 = 18
    115Sum of total energy: 36 + 45 + 80 + 18 = 179
    116
    117In the above example, adding together the total energy for all moons after 10 steps produces the
    118total energy in the system, 179.
    119
    120Here's a second example:
    121
    122<x=-8, y=-10, z=0>
    123<x=5, y=5, z=10>
    124<x=2, y=-7, z=3>
    125<x=9, y=-8, z=-3>
    126
    127Every ten steps of simulation for 100 steps produces:
    128
    129After 0 steps:
    130pos=<x= -8, y=-10, z=  0>, vel=<x=  0, y=  0, z=  0>
    131pos=<x=  5, y=  5, z= 10>, vel=<x=  0, y=  0, z=  0>
    132pos=<x=  2, y= -7, z=  3>, vel=<x=  0, y=  0, z=  0>
    133pos=<x=  9, y= -8, z= -3>, vel=<x=  0, y=  0, z=  0>
    134
    135After 10 steps:
    136pos=<x= -9, y=-10, z=  1>, vel=<x= -2, y= -2, z= -1>
    137pos=<x=  4, y= 10, z=  9>, vel=<x= -3, y=  7, z= -2>
    138pos=<x=  8, y=-10, z= -3>, vel=<x=  5, y= -1, z= -2>
    139pos=<x=  5, y=-10, z=  3>, vel=<x=  0, y= -4, z=  5>
    140
    141After 20 steps:
    142pos=<x=-10, y=  3, z= -4>, vel=<x= -5, y=  2, z=  0>
    143pos=<x=  5, y=-25, z=  6>, vel=<x=  1, y=  1, z= -4>
    144pos=<x= 13, y=  1, z=  1>, vel=<x=  5, y= -2, z=  2>
    145pos=<x=  0, y=  1, z=  7>, vel=<x= -1, y= -1, z=  2>
    146
    147After 30 steps:
    148pos=<x= 15, y= -6, z= -9>, vel=<x= -5, y=  4, z=  0>
    149pos=<x= -4, y=-11, z=  3>, vel=<x= -3, y=-10, z=  0>
    150pos=<x=  0, y= -1, z= 11>, vel=<x=  7, y=  4, z=  3>
    151pos=<x= -3, y= -2, z=  5>, vel=<x=  1, y=  2, z= -3>
    152
    153After 40 steps:
    154pos=<x= 14, y=-12, z= -4>, vel=<x= 11, y=  3, z=  0>
    155pos=<x= -1, y= 18, z=  8>, vel=<x= -5, y=  2, z=  3>
    156pos=<x= -5, y=-14, z=  8>, vel=<x=  1, y= -2, z=  0>
    157pos=<x=  0, y=-12, z= -2>, vel=<x= -7, y= -3, z= -3>
    158
    159After 50 steps:
    160pos=<x=-23, y=  4, z=  1>, vel=<x= -7, y= -1, z=  2>
    161pos=<x= 20, y=-31, z= 13>, vel=<x=  5, y=  3, z=  4>
    162pos=<x= -4, y=  6, z=  1>, vel=<x= -1, y=  1, z= -3>
    163pos=<x= 15, y=  1, z= -5>, vel=<x=  3, y= -3, z= -3>
    164
    165After 60 steps:
    166pos=<x= 36, y=-10, z=  6>, vel=<x=  5, y=  0, z=  3>
    167pos=<x=-18, y= 10, z=  9>, vel=<x= -3, y= -7, z=  5>
    168pos=<x=  8, y=-12, z= -3>, vel=<x= -2, y=  1, z= -7>
    169pos=<x=-18, y= -8, z= -2>, vel=<x=  0, y=  6, z= -1>
    170
    171After 70 steps:
    172pos=<x=-33, y= -6, z=  5>, vel=<x= -5, y= -4, z=  7>
    173pos=<x= 13, y= -9, z=  2>, vel=<x= -2, y= 11, z=  3>
    174pos=<x= 11, y= -8, z=  2>, vel=<x=  8, y= -6, z= -7>
    175pos=<x= 17, y=  3, z=  1>, vel=<x= -1, y= -1, z= -3>
    176
    177After 80 steps:
    178pos=<x= 30, y= -8, z=  3>, vel=<x=  3, y=  3, z=  0>
    179pos=<x= -2, y= -4, z=  0>, vel=<x=  4, y=-13, z=  2>
    180pos=<x=-18, y= -7, z= 15>, vel=<x= -8, y=  2, z= -2>
    181pos=<x= -2, y= -1, z= -8>, vel=<x=  1, y=  8, z=  0>
    182
    183After 90 steps:
    184pos=<x=-25, y= -1, z=  4>, vel=<x=  1, y= -3, z=  4>
    185pos=<x=  2, y= -9, z=  0>, vel=<x= -3, y= 13, z= -1>
    186pos=<x= 32, y= -8, z= 14>, vel=<x=  5, y= -4, z=  6>
    187pos=<x= -1, y= -2, z= -8>, vel=<x= -3, y= -6, z= -9>
    188
    189After 100 steps:
    190pos=<x=  8, y=-12, z= -9>, vel=<x= -7, y=  3, z=  0>
    191pos=<x= 13, y= 16, z= -3>, vel=<x=  3, y=-11, z= -5>
    192pos=<x=-29, y=-11, z= -1>, vel=<x= -3, y=  7, z=  4>
    193pos=<x= 16, y=-13, z= 23>, vel=<x=  7, y=  1, z=  1>
    194
    195Energy after 100 steps:
    196pot:  8 + 12 +  9 = 29;   kin: 7 +  3 + 0 = 10;   total: 29 * 10 = 290
    197pot: 13 + 16 +  3 = 32;   kin: 3 + 11 + 5 = 19;   total: 32 * 19 = 608
    198pot: 29 + 11 +  1 = 41;   kin: 3 +  7 + 4 = 14;   total: 41 * 14 = 574
    199pot: 16 + 13 + 23 = 52;   kin: 7 +  1 + 1 =  9;   total: 52 *  9 = 468
    200Sum of total energy: 290 + 608 + 574 + 468 = 1940
    201
    202What is the total energy in the system after simulating the moons given in your scan for 1000 steps?
    203
    204