2019 day 1
This commit is contained in:
@@ -45,7 +45,7 @@ export PATH := .:$(PATH)
|
||||
|
||||
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2
|
||||
|
||||
all: ex1
|
||||
all: ex1 ex2
|
||||
|
||||
memcheck: memcheck1
|
||||
|
||||
|
@@ -1,8 +1,4 @@
|
||||
* --- Day 1: The Tyranny of the Rocket Equation ---
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: day-1-the-tyranny-of-the-rocket-equation----
|
||||
:END:
|
||||
|
||||
** --- Day 1: The Tyranny of the Rocket Equation ---
|
||||
Santa has become stranded at the edge of the Solar System while
|
||||
delivering presents to other planets! To accurately calculate his
|
||||
position in space, safely align his warp drive, and return to Earth in
|
||||
@@ -38,6 +34,42 @@ it, individually calculate the fuel needed for the mass of each module
|
||||
/What is the sum of the fuel requirements/ for all of the modules on
|
||||
your spacecraft?
|
||||
|
||||
To begin, [[file:1/input][get your puzzle input]].
|
||||
Your puzzle answer was =3318604=.
|
||||
|
||||
Answer:
|
||||
** --- Part Two ---
|
||||
During the second Go / No Go poll, the Elf in charge of the Rocket
|
||||
Equation Double-Checker stops the launch sequence. Apparently, you
|
||||
forgot to include additional fuel for the fuel you just added.
|
||||
|
||||
Fuel itself requires fuel just like a module - take its mass, divide by
|
||||
three, round down, and subtract 2. However, that fuel /also/ requires
|
||||
fuel, and /that/ fuel requires fuel, and so on. Any mass that would
|
||||
require /negative fuel/ should instead be treated as if it requires
|
||||
/zero fuel/; the remaining mass, if any, is instead handled by /wishing
|
||||
really hard/, which has no mass and is outside the scope of this
|
||||
calculation.
|
||||
|
||||
So, for each module mass, calculate its fuel and add it to the total.
|
||||
Then, treat the fuel amount you just calculated as the input mass and
|
||||
repeat the process, continuing until a fuel requirement is zero or
|
||||
negative. For example:
|
||||
|
||||
- A module of mass =14= requires =2= fuel. This fuel requires no further
|
||||
fuel (2 divided by 3 and rounded down is =0=, which would call for a
|
||||
negative fuel), so the total fuel required is still just =2=.
|
||||
- At first, a module of mass =1969= requires =654= fuel. Then, this fuel
|
||||
requires =216= more fuel (=654 / 3 - 2=). =216= then requires =70=
|
||||
more fuel, which requires =21= fuel, which requires =5= fuel, which
|
||||
requires no further fuel. So, the total fuel required for a module of
|
||||
mass =1969= is =654 + 216 + 70 + 21 + 5 = 966=.
|
||||
- The fuel required by a module of mass =100756= and its fuel is:
|
||||
=33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346=.
|
||||
|
||||
/What is the sum of the fuel requirements/ for all of the modules on
|
||||
your spacecraft when also taking into account the mass of the added
|
||||
fuel? (Calculate the fuel requirements for each module separately, then
|
||||
add them all up at the end.)
|
||||
|
||||
Your puzzle answer was =4975039=.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/* aoc-c.c: Advent of Code 2021, day 1 parts 1 & 2
|
||||
/* aoc-c.c: Advent of Code 2019, day 1 parts 1 & 2
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
@@ -15,64 +15,28 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "bits.h"
|
||||
#include "pool.h"
|
||||
#include "list.h"
|
||||
#include "bits.h"
|
||||
|
||||
struct ranges {
|
||||
u32 val;
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
LIST_HEAD(list_head);
|
||||
|
||||
int ex1()
|
||||
static u64 part1()
|
||||
{
|
||||
u32 count = 0, res = 0, prev, cur;
|
||||
u64 res = 0;
|
||||
int cur;
|
||||
|
||||
while (scanf("%d", &cur) != EOF) {
|
||||
if (count && cur > prev)
|
||||
res++;
|
||||
count++;
|
||||
prev = cur;
|
||||
}
|
||||
while (scanf("%d", &cur) != EOF)
|
||||
res += cur / 3 - 2;
|
||||
return res;
|
||||
}
|
||||
|
||||
int ex2()
|
||||
static u64 part2()
|
||||
{
|
||||
u32 count = 0, res = 0;
|
||||
u32 val;
|
||||
pool_t *pool;
|
||||
struct ranges *input;
|
||||
struct ranges *list_cur;
|
||||
u64 res = 0;
|
||||
int cur;
|
||||
|
||||
if (!(pool = pool_create("pool", 10, sizeof (struct ranges))))
|
||||
return -1;
|
||||
|
||||
while (scanf("%d", &val) != EOF) {
|
||||
if (!(input = pool_get(pool)))
|
||||
return -1;
|
||||
input->val = val;
|
||||
list_add_tail(&input->list, &list_head);
|
||||
|
||||
if (count > 2) {
|
||||
u32 loop = 0, v1 = 0, v2 = 0;
|
||||
struct ranges *first = list_entry(list_head.next, struct ranges, list);
|
||||
|
||||
list_for_each_entry(list_cur, &list_head, list) {
|
||||
if (loop < 3)
|
||||
v1 += list_cur->val;
|
||||
if (loop > 0)
|
||||
v2 += list_cur->val;
|
||||
++loop;
|
||||
}
|
||||
list_del(&first->list);
|
||||
pool_add(pool, first);
|
||||
if (v2 > v1)
|
||||
res++;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
while (scanf("%d", &cur) != EOF)
|
||||
while ((cur = cur / 3 - 2) > 0)
|
||||
res += cur;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -84,8 +48,7 @@ static int usage(char *prg)
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int opt;
|
||||
u32 exercise = 1, res;
|
||||
int opt, part = 1;
|
||||
|
||||
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
||||
switch (opt) {
|
||||
@@ -93,7 +56,9 @@ int main(int ac, char **av)
|
||||
debug_level_set(atoi(optarg));
|
||||
break;
|
||||
case 'p': /* 1 or 2 */
|
||||
exercise = atoi(optarg);
|
||||
part = atoi(optarg);
|
||||
if (part < 1 || part > 2)
|
||||
return usage(*av);
|
||||
break;
|
||||
default:
|
||||
return usage(*av);
|
||||
@@ -103,13 +68,7 @@ int main(int ac, char **av)
|
||||
if (optind < ac)
|
||||
return usage(*av);
|
||||
|
||||
if (exercise == 1) {
|
||||
res = ex1();
|
||||
printf ("%s : res=%d\n", *av, res);
|
||||
} else {
|
||||
res = ex2();
|
||||
printf ("%s : res=%d\n", *av, res);
|
||||
}
|
||||
printf("%s : res=%lu\n", *av, part == 1? part1(): part2());
|
||||
|
||||
exit (0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user