2022/day 1 (C) - save from laptop

This commit is contained in:
Bruno
2022-12-02 10:32:02 +01:00
parent ce446be296
commit 6012d4d375
3 changed files with 118 additions and 2 deletions

View File

@@ -71,11 +71,11 @@ assembly: aoc-c.s
part1: aoc-c part1: aoc-c
@$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1 @$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1
@#$(TIME) aoc-c -p 1 < $(INPUT) @$(TIME) aoc-c -p 1 < $(INPUT)
part2: aoc-c part2: aoc-c
@$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1 @$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1
@#$(TIME) aoc-c -p 2 < $(INPUT) @$(TIME) aoc-c -p 2 < $(INPUT)
ccls: $(CCLSFILE) ccls: $(CCLSFILE)

99
2022/day01/aoc-c.c Normal file
View File

@@ -0,0 +1,99 @@
/* aoc-c.c: Advent of Code 2022, day 1
*
* Copyright (C) 2022 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 <unistd.h>
#include "plist.h"
#include "debug.h"
#include "pool.h"
PLIST_HEAD(plist);
static int calc_top_plist(int n)
{
int res = 0;
struct plist_node *node;
plist_for_each_reverse(node, &plist) {
res += node->prio;
if (!--n)
break;
}
return res;
}
static void parse(pool_t *pool)
{
size_t alloc = 0;
char *buf = NULL;
ssize_t buflen;
int total = 0;
struct plist_node *node;
while (1) {
buflen = getline(&buf, &alloc, stdin);
switch (buflen) {
case 1:
case -1:
node = pool_get(pool);
plist_node_init(node, total);
plist_add(node, &plist);
total = 0;
if (buflen == -1)
goto end;
break;
default:
total += atoi(buf);
}
}
end:
free(buf);
return;
}
static int usage(char *prg)
{
fprintf(stderr, "Usage: %s [-d debug_level] [-p part] [-i input]\n", prg);
return 1;
}
int main(int ac, char **av)
{
int opt, part = 1;
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)
return usage(*av);
break;
default:
return usage(*av);
}
}
if (optind < ac)
return usage(*av);
pool_t *pool_tot = pool_create("total", 128, sizeof(struct plist_node));
parse(pool_tot);
printf("%s : res=%d\n", *av, calc_top_plist(part == 1? 1: 3));
pool_destroy(pool_tot);
exit(0);
}

View File

@@ -152,6 +152,14 @@ extern void plist_requeue(struct plist_node *node, struct plist_head *head);
#define plist_for_each(pos, head) \ #define plist_for_each(pos, head) \
list_for_each_entry(pos, &(head)->node_list, node_list) list_for_each_entry(pos, &(head)->node_list, node_list)
/**
* plist_for_each_reverse - iterate backwards over the plist
* @pos: the type * to use as a loop counter
* @head: the head for your list
*/
#define plist_for_each_reverse(pos, head) \
list_for_each_entry_reverse(pos, &(head)->node_list, node_list)
/** /**
* plist_for_each_continue - continue iteration over the plist * plist_for_each_continue - continue iteration over the plist
* @pos: the type * to use as a loop cursor * @pos: the type * to use as a loop cursor
@@ -182,6 +190,15 @@ extern void plist_requeue(struct plist_node *node, struct plist_head *head);
#define plist_for_each_entry(pos, head, mem) \ #define plist_for_each_entry(pos, head, mem) \
list_for_each_entry(pos, &(head)->node_list, mem.node_list) list_for_each_entry(pos, &(head)->node_list, mem.node_list)
/**
* plist_for_each_entry_reverse - iterate backwards over list of given type
* @pos: the type * to use as a loop counter
* @head: the head for your list
* @mem: the name of the list_head within the struct
*/
#define plist_for_each_entry_reverse(pos, head, mem) \
list_for_each_entry(pos, &(head)->node_list, mem.node_list)
/** /**
* plist_for_each_entry_continue - continue iteration over list of given type * plist_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor * @pos: the type * to use as a loop cursor