diff --git a/2021/day01/README.txt b/2021/day01/README.txt index eb87d4c..317aaa1 100644 --- a/2021/day01/README.txt +++ b/2021/day01/README.txt @@ -84,9 +84,3 @@ Consider sums of a three-measurement sliding window. How many sums are larger th Your puzzle answer was 1235. Both parts of this puzzle are complete! They provide two gold stars: ** - -At this point, you should return to your Advent calendar and try another puzzle. - -If you still want to see it, you can get your puzzle input. - -You can also [Shareon Mastodon] this puzzle. diff --git a/2021/day02/README.txt b/2021/day02/README.txt index 91d8a7d..d9ec5e6 100644 --- a/2021/day02/README.txt +++ b/2021/day02/README.txt @@ -32,8 +32,36 @@ After following these instructions, you would have a horizontal position of 15 a Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth? -To begin, get your puzzle input. +Your puzzle answer was 1636725. -Answer: +The first half of this puzzle is complete! It provides one gold star: * +--- Part Two --- -You can also [Shareon Mastodon] this puzzle. +Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated. + +In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought: + + down X increases your aim by X units. + up X decreases your aim by X units. + forward X does two things: + It increases your horizontal position by X units. + It increases your depth by your aim multiplied by X. + +Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction. + +Now, the above example does something different: + + forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change. + down 5 adds 5 to your aim, resulting in a value of 5. + forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40. + up 3 decreases your aim by 3, resulting in a value of 2. + down 8 adds 8 to your aim, resulting in a value of 10. + forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60. + +After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.) + +Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth? + +Your puzzle answer was 1872757425. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2021/day02/aoc-c.c b/2021/day02/aoc-c.c new file mode 100644 index 0000000..2eb7e50 --- /dev/null +++ b/2021/day02/aoc-c.c @@ -0,0 +1,110 @@ +/* aoc-c: Advent2021 game, day 2 parts 1 & 2 + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#include "debug.h" +#include "bits.h" +#include "pool.h" + +struct ranges { + u32 val; + struct list_head list; +}; + +LIST_HEAD(list_head); + +int ex1() +{ + u32 val; + char buffer[80]; + s32 forward = 0, depth = 0; + + while (scanf("%s %d", buffer, &val) != EOF) { + switch (*buffer) { + case 'f': /* forward */ + forward += val; + break; + case 'd': /* down */ + depth += val; + break; + case 'u': /* up */ + depth -= val; + break; + } + } + return depth * forward; +} + +int ex2() +{ + u32 val; + char buffer[80]; + s32 aim = 0, forward = 0, depth = 0; + + while (scanf("%s %d", buffer, &val) != EOF) { + switch (*buffer) { + case 'f': /* forward */ + forward += val; + depth += val * aim; + break; + case 'd': /* down */ + aim += val; + break; + case 'u': /* up */ + aim -= val; + break; + } + } + return depth * forward; +} + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg); + return 1; +} + +int main(int ac, char **av) +{ + int opt; + u32 exercise = 2, res; + + while ((opt = getopt(ac, av, "d:p:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'p': /* 1 or 2 */ + exercise = atoi(optarg); + break; + default: + return usage(*av); + } + } + + if (optind < ac) + return usage(*av); + + if (exercise == 1) { + res = ex1(); + printf ("%s : res=%d\n", *av, res); + } + if (exercise == 2) { + res = ex2(); + printf ("%s : res=%d\n", *av, res); + } + + exit (0); +}