diff --git a/2019/RESULTS.txt b/2019/RESULTS.txt index 119045e..8ec6845 100644 --- a/2019/RESULTS.txt +++ b/2019/RESULTS.txt @@ -39,3 +39,17 @@ aoc-c : res=860 aoc-c : res=9238 time: 0:00.00 real, 0.00 user, 0.00 sys context-switch: 0+1, page-faults: 0+98 + +========================================= +================= day04 ================= +========================================= + ++++++++++++++++++ part 1 +aoc-c : res=2090 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 2+1, page-faults: 0+88 + ++++++++++++++++++ part 2 +aoc-c : res=1419 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 3+1, page-faults: 0+91 diff --git a/2019/day03/aoc-c.c b/2019/day03/aoc-c.c index 7d51f68..fc84ab2 100644 --- a/2019/day03/aoc-c.c +++ b/2019/day03/aoc-c.c @@ -56,7 +56,8 @@ static struct list_head *parse(struct list_head *wires) int totdist = 0; if ((buflen = getline(&buf, &alloc, stdin)) <= 0) { - fprintf(stderr, "error %d reading file.\n", errno); + fprintf(stderr, "error %d reading input.\n", errno); + wires = NULL; goto end; } diff --git a/2019/day04/README.org b/2019/day04/README.org index 0d91e7a..18ae1ae 100644 --- a/2019/day04/README.org +++ b/2019/day04/README.org @@ -21,4 +21,23 @@ Other than the range rule, the following are true: /How many different passwords/ within the range given in your puzzle input meet these criteria? -Your puzzle input is =130254-678275=. +Your puzzle answer was =2090=. + +** --- Part Two --- +An Elf just remembered one more important detail: the two adjacent +matching digits /are not part of a larger group of matching digits/. + +Given this additional criterion, but still ignoring the range rule, the +following are now true: + +- =112233= meets these criteria because the digits never decrease and + all repeated digits are exactly two digits long. +- =123444= no longer meets the criteria (the repeated =44= is part of a + larger group of =444=). +- =111122= meets the criteria (even though =1= is repeated more than + twice, it still contains a double =22=). + +/How many different passwords/ within the range given in your puzzle +input meet all of the criteria? + +Your puzzle answer was =1419=. diff --git a/2019/day04/aoc-c.c b/2019/day04/aoc-c.c new file mode 100644 index 0000000..147b13b --- /dev/null +++ b/2019/day04/aoc-c.c @@ -0,0 +1,103 @@ +/* aoc-c.c: Advent of Code 2019, day 3 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 +#include +#include + +#include "debug.h" + +static int is_valid(int number, int part) +{ + int valid = 0, dups[10] = { 0 }; + int digit, dec; + + for (digit = number % 10; number > 10; digit = dec) { + number /= 10; + dec = number % 10; + if (dec > digit) + return 0; + if (dec == digit) { + valid = 1; + dups[digit] += 2; + } + } + if (!valid) + return 0; + if (part == 2) { + valid = 0; + for (int i = 0; i < 10; ++i) { + if (dups[i] == 2) + return 1; + } + } + return valid; +} + +static int doit(int *nums, int part) +{ + int res = 0; + + /* There is surely a way to avoid 99% of useless calls to is_valid. + */ + for (int i = nums[0]; i < nums[1]; ++i) + if (is_valid(i, part)) + res++; + return res; +} + +static int *parse(int *res) +{ + size_t alloc = 0; + char *buf = NULL; + + getline(&buf, &alloc, stdin); + *res = atoi(strtok(buf, "-")); + *(res+1) = atoi(strtok(NULL, "-")); + free(buf); + return res; +} + +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, part = 1; + int nums[2]; + + while ((opt = getopt(ac, av, "d:p:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'p': /* 1 or 2 */ + part = atoi(optarg); + if (part < 1 || part > 2) + default: + return usage(*av); + } + } + if (optind < ac) + return usage(*av); + + parse(nums); + printf("%s : res=%d\n", *av, doit(nums, part)); + + exit (0); +}