2019 day 4, parts 1 and 2
This commit is contained in:
@@ -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
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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=.
|
||||
|
103
2019/day04/aoc-c.c
Normal file
103
2019/day04/aoc-c.c
Normal file
@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#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);
|
||||
}
|
Reference in New Issue
Block a user