New Makefile, day 1 solution (C)
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,3 +1,8 @@
|
||||
ex*-c
|
||||
core
|
||||
ex*-cob
|
||||
lib/
|
||||
GPATH
|
||||
GRTAGS
|
||||
GTAGS
|
||||
2020/day23/
|
4
2021/.dir-locals.el
Normal file
4
2021/.dir-locals.el
Normal file
@@ -0,0 +1,4 @@
|
||||
((nil . ((eval . (let ((root (expand-file-name "2021/" (projectile-project-root))))
|
||||
(setq-local
|
||||
flycheck-gcc-include-path (list (concat root "include"))
|
||||
compile-command (concat "make -C " root " all")))))))
|
0
2021/.projectile
Normal file
0
2021/.projectile
Normal file
@@ -1,15 +1,53 @@
|
||||
# AOC Makefile - GNU make only.
|
||||
#
|
||||
# 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.htmlL>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
#
|
||||
|
||||
|
||||
SUBDIRS := $(shell echo day??)
|
||||
|
||||
EXCLUDE := --exclude 'cob-01/'
|
||||
CC = gcc
|
||||
|
||||
.PHONY: clean $(SUBDIRS)
|
||||
#LIBS = -lreadline -lncurses
|
||||
CFLAGS += -std=gnu99
|
||||
|
||||
#CFLAGS += -O2
|
||||
CFLAGS += -g
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -march=native
|
||||
|
||||
INCDIR := ./include
|
||||
LIBSRCDIR := ./libsrc
|
||||
LIBDIR := ./lib
|
||||
LIB := libaoc_$(shell uname -m)
|
||||
SLIB := $(LIBDIR)/$(LIB).a
|
||||
DLIB := $(LIBDIR)/$(LIB).so
|
||||
LIBSRC := $(wildcard $(LIBSRCDIR)/*.c)
|
||||
LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC))
|
||||
LDFLAGS := -L$(LIBDIR)
|
||||
LDLIB := -l$(LIB)
|
||||
|
||||
.PHONY: clean cleanall all redo output lib $(SUBDIRS)
|
||||
|
||||
all: lib $(SUBDIRS)
|
||||
|
||||
clean:
|
||||
for dir in $(SUBDIRS) ; do \
|
||||
make -C $$dir clean ; \
|
||||
$(MAKE) -C $$dir clean ; \
|
||||
done
|
||||
|
||||
all: $(SUBDIRS)
|
||||
cleanall: clean
|
||||
echo AAAAAAAAAAAAAAAAAAAAA
|
||||
$(RM) -f $(SLIB) $(DLIB)
|
||||
|
||||
redo: cleanall all
|
||||
|
||||
$(SUBDIRS):
|
||||
@echo "========================================="
|
||||
@@ -17,9 +55,27 @@ $(SUBDIRS):
|
||||
@echo "========================================="
|
||||
@echo
|
||||
@echo "+++++++++++++++++ ex1"
|
||||
@$(MAKE) --no-print-directory -C $@ ex1 2>&1
|
||||
+$(MAKE) --no-print-directory -C $@ ex1 2>&1
|
||||
@echo "+++++++++++++++++ ex2"
|
||||
@$(MAKE) --no-print-directory -C $@ ex2 2>&1
|
||||
+$(MAKE) --no-print-directory -C $@ ex2 2>&1
|
||||
|
||||
output:
|
||||
@$(MAKE) --no-print-directory all >OUTPUT 2>&1
|
||||
|
||||
lib: $(DLIB) $(SLIB)
|
||||
|
||||
$(SLIB): $(LIBOBJ)
|
||||
@#echo ZZZZZZZZZZZZZZZZZZZZ $(SLIB) $(DLIB)
|
||||
mkdir -p $(LIBDIR)
|
||||
$(AR) $(ARFLAGS) -o $@ $^
|
||||
|
||||
$(DLIB): CFLAGS += -fPIC
|
||||
$(DLIB): LDFLAGS += -shared
|
||||
$(DLIB): $(LIBOBJ)
|
||||
@#echo YYYYYYYYYYYYYYYYYYYY $(SLIB) $(DLIB)
|
||||
mkdir -p $(LIBDIR)
|
||||
$(CC) $(LDFLAGS) $^ -o $@
|
||||
|
||||
.c.o:
|
||||
echo TOTO
|
||||
$(CC) -c $(CFLAGS) $(LDFLAGS) -I $(INCDIR) -o $@ $<
|
||||
|
10
2021/day01/EXAMPLE.txt
Normal file
10
2021/day01/EXAMPLE.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
2000
2021/day01/INPUT.txt
Normal file
2000
2021/day01/INPUT.txt
Normal file
File diff suppressed because it is too large
Load Diff
53
2021/day01/Makefile
Normal file
53
2021/day01/Makefile
Normal file
@@ -0,0 +1,53 @@
|
||||
# AOC daily Makefile - GNU make only.
|
||||
#
|
||||
# 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.htmlL>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
#
|
||||
|
||||
INPUT := INPUT.txt
|
||||
SHELL := /bin/bash
|
||||
|
||||
CC := gcc
|
||||
|
||||
LIB := aoc_$(shell uname -m)
|
||||
INCDIR := ../include
|
||||
LIBDIR := ../lib
|
||||
LDFLAGS := -L$(LIBDIR)
|
||||
LDLIB := -l$(LIB)
|
||||
|
||||
export LD_LIBRARY_PATH = $(LIBDIR)
|
||||
|
||||
CFLAGS += -std=gnu99
|
||||
CFLAGS += -O2
|
||||
CFLAGS += -g
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -march=native
|
||||
|
||||
CFLAGS += -DDEBUG_POOL # memory pools management
|
||||
|
||||
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||
export PATH := .:$(PATH)
|
||||
|
||||
.PHONY: clean all compile deploy ex1 ex2
|
||||
|
||||
all: ex1 ex2
|
||||
|
||||
compile: aoc-c
|
||||
|
||||
ex1: aoc-c
|
||||
@$(TIME) aoc-c -d 5 -p 1 < $(INPUT)
|
||||
|
||||
ex2: aoc-c
|
||||
@$(TIME) aoc-c -d 5 -p 2 < $(INPUT)
|
||||
|
||||
clean:
|
||||
@rm -f aoc-c core*
|
||||
|
||||
.c:
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
|
104
2021/day01/README.txt
Normal file
104
2021/day01/README.txt
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
Advent of Code
|
||||
|
||||
[About][Events][Shop][Settings][Log Out]
|
||||
|
||||
Bruno Raoult 2*
|
||||
{:year 2021}
|
||||
|
||||
[Calendar][AoC++][Sponsors][Leaderboard][Stats]
|
||||
|
||||
Our sponsors help make Advent of Code possible:
|
||||
Ximedes - From Kotlin in the cloud to NodeJS in the terminal, our software powers payments and public transport ticketing all over the world.
|
||||
--- Day 1: Sonar Sweep ---
|
||||
|
||||
You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!
|
||||
|
||||
Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars.
|
||||
|
||||
Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th.
|
||||
|
||||
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
|
||||
|
||||
As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.
|
||||
|
||||
For example, suppose you had the following report:
|
||||
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
||||
|
||||
This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.
|
||||
|
||||
The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something.
|
||||
|
||||
To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows:
|
||||
|
||||
199 (N/A - no previous measurement)
|
||||
200 (increased)
|
||||
208 (increased)
|
||||
210 (increased)
|
||||
200 (decreased)
|
||||
207 (increased)
|
||||
240 (increased)
|
||||
269 (increased)
|
||||
260 (decreased)
|
||||
263 (increased)
|
||||
|
||||
In this example, there are 7 measurements that are larger than the previous measurement.
|
||||
|
||||
How many measurements are larger than the previous measurement?
|
||||
|
||||
Your puzzle answer was 1195.
|
||||
--- Part Two ---
|
||||
|
||||
Considering every single measurement isn't as useful as you expected: there's just too much noise in the data.
|
||||
|
||||
Instead, consider sums of a three-measurement sliding window. Again considering the above example:
|
||||
|
||||
199 A
|
||||
200 A B
|
||||
208 A B C
|
||||
210 B C D
|
||||
200 E C D
|
||||
207 E F D
|
||||
240 E F G
|
||||
269 F G H
|
||||
260 G H
|
||||
263 H
|
||||
|
||||
Start by comparing the first and second three-measurement windows. The measurements in the first window are marked A (199, 200, 208); their sum is 199 + 200 + 208 = 607. The second window is marked B (200, 208, 210); its sum is 618. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased.
|
||||
|
||||
Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum.
|
||||
|
||||
In the above example, the sum of each three-measurement window is as follows:
|
||||
|
||||
A: 607 (N/A - no previous sum)
|
||||
B: 618 (increased)
|
||||
C: 618 (no change)
|
||||
D: 617 (decreased)
|
||||
E: 647 (increased)
|
||||
F: 716 (increased)
|
||||
G: 769 (increased)
|
||||
H: 792 (increased)
|
||||
|
||||
In this example, there are 5 sums that are larger than the previous sum.
|
||||
|
||||
Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?
|
||||
|
||||
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.
|
116
2021/day01/aoc-c.c
Normal file
116
2021/day01/aoc-c.c
Normal file
@@ -0,0 +1,116 @@
|
||||
/* aoc-c: Advent2021 game, day 1 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.htmlL>.
|
||||
*
|
||||
* 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 "debug.h"
|
||||
#include "bits.h"
|
||||
#include "pool.h"
|
||||
|
||||
struct ranges {
|
||||
u32 val;
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
LIST_HEAD(list_head);
|
||||
|
||||
int ex1()
|
||||
{
|
||||
u32 count = 0, res = 0, prev, cur;
|
||||
|
||||
while (scanf("%d", &cur) != EOF) {
|
||||
if (count && cur > prev)
|
||||
res++;
|
||||
count++;
|
||||
prev = cur;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int ex2()
|
||||
{
|
||||
u32 count = 0, res = 0;
|
||||
u32 val;
|
||||
pool_t *pool;
|
||||
struct ranges *input;
|
||||
struct ranges *list_cur;
|
||||
|
||||
if (!(pool = pool_init("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++;
|
||||
}
|
||||
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;
|
||||
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);
|
||||
}
|
36
2021/include/pool.h
Normal file
36
2021/include/pool.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/* pool.h - A simple memory pool manager.
|
||||
*
|
||||
* 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.htmlL>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef POOL_H
|
||||
#define POOL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "list.h"
|
||||
#include "bits.h"
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
u32 available;
|
||||
u32 allocated;
|
||||
u32 growsize;
|
||||
size_t eltsize;
|
||||
struct list_head head;
|
||||
} pool_t;
|
||||
|
||||
void pool_stats(pool_t *pool);
|
||||
pool_t *pool_init(const char *name, u32 grow, size_t size);
|
||||
void *pool_get(pool_t *pool);
|
||||
u32 pool_add(pool_t *pool, void *elt);
|
||||
|
||||
#endif
|
BIN
2021/libsrc/bits.o
Normal file
BIN
2021/libsrc/bits.o
Normal file
Binary file not shown.
BIN
2021/libsrc/debug.o
Normal file
BIN
2021/libsrc/debug.o
Normal file
Binary file not shown.
180
2021/libsrc/pool.c
Normal file
180
2021/libsrc/pool.c
Normal file
@@ -0,0 +1,180 @@
|
||||
/* pool.c - A simple pool manager.
|
||||
*
|
||||
* 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.htmlL>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "list.h"
|
||||
#include "pool.h"
|
||||
#include "debug.h"
|
||||
#include "bits.h"
|
||||
|
||||
void pool_stats(pool_t *pool)
|
||||
{
|
||||
if (pool) {
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "[%s] pool [%p]: avail:%u alloc:%u grow:%u eltsize:%lu\n",
|
||||
pool->name, (void *)pool, pool->available, pool->allocated,
|
||||
pool->growsize, pool->eltsize);
|
||||
# endif
|
||||
}
|
||||
}
|
||||
|
||||
pool_t *pool_init(const char *name, u32 growsize, size_t eltsize)
|
||||
{
|
||||
pool_t *pool;
|
||||
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "name=[%s] growsize=%u eltsize=%lu\n",
|
||||
name, growsize, eltsize);
|
||||
# endif
|
||||
/* we need at least this space in struct */
|
||||
if (eltsize < sizeof (struct list_head))
|
||||
return NULL;
|
||||
if ((pool = malloc(sizeof (*pool)))) {
|
||||
pool->name = strdup(name);
|
||||
pool->growsize = growsize;
|
||||
pool->eltsize = eltsize;
|
||||
pool->available = 0;
|
||||
pool->allocated = 0;
|
||||
INIT_LIST_HEAD(&pool->head);
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
static u32 _pool_add(pool_t *pool, struct list_head *elt)
|
||||
{
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(10, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n",
|
||||
(void *)pool,
|
||||
(void *)&pool->head,
|
||||
(void *)elt,
|
||||
(void *)&pool->head-(void *)pool,
|
||||
offsetof(pool_t, head));
|
||||
# endif
|
||||
|
||||
list_add(elt, &pool->head);
|
||||
return ++pool->available;
|
||||
}
|
||||
|
||||
u32 pool_add(pool_t *pool, void *elt)
|
||||
{
|
||||
return _pool_add(pool, elt);
|
||||
}
|
||||
|
||||
static struct list_head *_pool_get(pool_t *pool)
|
||||
{
|
||||
struct list_head *res = pool->head.next;
|
||||
pool->available--;
|
||||
list_del(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
void *pool_get(pool_t *pool)
|
||||
{
|
||||
if (!pool)
|
||||
return NULL;
|
||||
if (!pool->available) {
|
||||
void *alloc = malloc(pool->eltsize * pool->growsize);
|
||||
void *cur;
|
||||
u32 i;
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "[%s]: growing pool from %u to %u elements.\n",
|
||||
pool->name,
|
||||
pool->allocated,
|
||||
pool->allocated + pool->growsize);
|
||||
# endif
|
||||
if (!alloc)
|
||||
return NULL;
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(5, " (old=%u)\n", pool->allocated);
|
||||
# endif
|
||||
pool->allocated += pool->growsize;
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(5, " (new=%u)\n", pool->allocated);
|
||||
# endif
|
||||
for (i = 0; i < pool->growsize; ++i) {
|
||||
cur = alloc + i * pool->eltsize;
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(5, "alloc=%p cur=%p\n", alloc, cur);
|
||||
# endif
|
||||
_pool_add(pool, (struct list_head *)cur);
|
||||
}
|
||||
pool_stats(pool);
|
||||
}
|
||||
/* this is the effective address if the object (and also the
|
||||
* pool list_head address)
|
||||
*/
|
||||
return _pool_get(pool);
|
||||
}
|
||||
|
||||
#ifdef BIN_pool
|
||||
struct d {
|
||||
u16 data1;
|
||||
char c;
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
static LIST_HEAD (head);
|
||||
|
||||
int main(int ac, char**av)
|
||||
{
|
||||
pool_t *pool;
|
||||
int total;
|
||||
int action=0;
|
||||
u16 icur=0;
|
||||
char ccur='z';
|
||||
struct d *elt;
|
||||
|
||||
debug_init(3);
|
||||
log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
|
||||
sizeof(*elt), offsetof(struct d, list));
|
||||
|
||||
if ((pool = pool_init("dummy", 3, sizeof(*elt)))) {
|
||||
pool_stats(pool);
|
||||
for (int cur=1; cur<ac; ++cur) {
|
||||
total = atoi(av[cur]);
|
||||
if (action == 0) { /* add elt to list */
|
||||
log_f(2, "adding %d elements\n", total);
|
||||
for (int i = 0; i < total; ++i) {
|
||||
elt = pool_get(pool);
|
||||
elt->data1 = icur++;
|
||||
elt->c = ccur--;
|
||||
list_add(&elt->list, &head);
|
||||
}
|
||||
pool_stats(pool);
|
||||
action = 1;
|
||||
} else { /* remove one elt from list */
|
||||
log_f(2, "deleting %d elements\n", total);
|
||||
for (int i = 0; i < total; ++i) {
|
||||
if (!list_empty(&head)) {
|
||||
elt = list_last_entry(&head, struct d, list);
|
||||
printf("elt=[%d, %c]\n", elt->data1, elt->c);
|
||||
list_del(&elt->list);
|
||||
pool_add(pool, elt);
|
||||
}
|
||||
}
|
||||
pool_stats(pool);
|
||||
action = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
pool_stats(pool);
|
||||
}
|
||||
#endif
|
BIN
2021/libsrc/pool.o
Normal file
BIN
2021/libsrc/pool.o
Normal file
Binary file not shown.
@@ -1,26 +1,53 @@
|
||||
INPUT := INPUT.txt
|
||||
SHELL := /bin/bash
|
||||
CFLAGS := -w
|
||||
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||
# AOC daily Makefile - GNU make only.
|
||||
#
|
||||
# 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.htmlL>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
#
|
||||
|
||||
INPUT := INPUT.txt
|
||||
SHELL := /bin/bash
|
||||
|
||||
CC := gcc
|
||||
|
||||
LIB := aoc_$(shell uname -m)
|
||||
INCDIR := ../include
|
||||
LIBDIR := ../lib
|
||||
LDFLAGS := -L$(LIBDIR)
|
||||
LDLIB := -l$(LIB)
|
||||
|
||||
export LD_LIBRARY_PATH = $(LIBDIR)
|
||||
|
||||
CFLAGS += -std=gnu99
|
||||
CFLAGS += -O2
|
||||
CFLAGS += -g
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -march=native
|
||||
|
||||
CFLAGS += -DDEBUG_POOL # memory pools management
|
||||
|
||||
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||
export PATH := .:$(PATH)
|
||||
|
||||
.PHONY: clean all compile deploy ex1 ex2
|
||||
|
||||
all: ex1 ex2
|
||||
|
||||
compile: ex1-c ex2-c
|
||||
compile: aoc-c
|
||||
|
||||
ex1: ex1-c
|
||||
@$(TIME) ex1.bash < $(INPUT)
|
||||
@$(TIME) ex1-c < $(INPUT)
|
||||
ex1: aoc-c
|
||||
@$(TIME) aoc-c -d 5 -p 1 < $(INPUT)
|
||||
|
||||
ex2: ex2-c
|
||||
@$(TIME) ex2.bash < $(INPUT)
|
||||
@#$(TIME) ex2-sort.bash < $(INPUT)
|
||||
@$(TIME) ex2-c < $(INPUT)
|
||||
ex2: aoc-c
|
||||
@$(TIME) aoc-c -d 5 -p 2 < $(INPUT)
|
||||
|
||||
clean:
|
||||
@rm -f ex1-c ex2-c core
|
||||
@rm -f aoc-c core*
|
||||
|
||||
deploy:
|
||||
@$(MAKE) -C .. deploy
|
||||
.c:
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
|
||||
|
116
2021/templates/aoc-c.c
Normal file
116
2021/templates/aoc-c.c
Normal file
@@ -0,0 +1,116 @@
|
||||
/* aoc-c: Advent2021 game, day 1 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.htmlL>.
|
||||
*
|
||||
* 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 "debug.h"
|
||||
#include "bits.h"
|
||||
#include "pool.h"
|
||||
|
||||
struct ranges {
|
||||
u32 val;
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
LIST_HEAD(list_head);
|
||||
|
||||
int ex1()
|
||||
{
|
||||
u32 count = 0, res = 0, prev, cur;
|
||||
|
||||
while (scanf("%d", &cur) != EOF) {
|
||||
if (count && cur > prev)
|
||||
res++;
|
||||
count++;
|
||||
prev = cur;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int ex2()
|
||||
{
|
||||
u32 count = 0, res = 0;
|
||||
u32 val;
|
||||
pool_t *pool;
|
||||
struct ranges *input;
|
||||
struct ranges *list_cur;
|
||||
|
||||
if (!(pool = pool_init("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++;
|
||||
}
|
||||
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;
|
||||
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);
|
||||
}
|
@@ -1,58 +0,0 @@
|
||||
/* ex1-c: Advent2021 game, day 1/game 1
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
#define XMOVE 3
|
||||
|
||||
int my_strlen(str)
|
||||
char *str;
|
||||
{
|
||||
int i;
|
||||
for (i=0; *str; ++i, ++str)
|
||||
;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int usage(char *prg)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [-ilw] [file...]\n", prg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int opt;
|
||||
char str[80];
|
||||
|
||||
while ((opt = getopt(ac, av, "d:f:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
debug_level_set(atoi(optarg));
|
||||
break;
|
||||
default:
|
||||
return usage(*av);
|
||||
}
|
||||
}
|
||||
printf("optind = %d ac = %d\n", optind, ac);
|
||||
|
||||
if (optind < ac)
|
||||
return usage(*av);
|
||||
|
||||
scanf("%s", str); /* ignore 1st line */
|
||||
|
||||
while (scanf("%s", str) != EOF) {
|
||||
line++;
|
||||
col+=XMOVE;
|
||||
linelen=my_strlen(str);
|
||||
mod=col%linelen;
|
||||
if (*(str+mod) == '#')
|
||||
count++;
|
||||
}
|
||||
printf ("%s : lines:%d pos:%d found:%d\n", *av, line, col, count);
|
||||
exit (0);
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex1.bash: Advent2020 game, day 3/game 1.
|
||||
|
||||
CMD=${0##*/}
|
||||
|
||||
XMOVE=3 # 3 right
|
||||
|
||||
declare -i xpos=0 ypos=0 count=0 linelen=0 modpos=0
|
||||
|
||||
read -r line # ignore 1st line
|
||||
|
||||
while read -r line; do
|
||||
(( ypos++ ))
|
||||
(( xpos += XMOVE )) # move right
|
||||
(( linelen = ${#line} ))
|
||||
(( modpos = (xpos % linelen) ))
|
||||
[[ ${line:modpos:1} = \# ]] && ((count++))
|
||||
done
|
||||
printf "%s : lines:%d pos=%d found:%d\n" "$CMD" $ypos $xpos $count
|
||||
exit 0
|
@@ -1,60 +0,0 @@
|
||||
/* ex1-c: Advent2020 game, day 3/game 2
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct {
|
||||
int dx;
|
||||
int dy;
|
||||
int pos;
|
||||
int count;
|
||||
} set[] = {
|
||||
{ 1, 1, 0, 0},
|
||||
{ 3, 1, 0, 0},
|
||||
{ 5, 1, 0, 0},
|
||||
{ 7, 1, 0, 0},
|
||||
{ 1, 2, 0, 0},
|
||||
{-1, -1, 0, 0} /* end marker - not necessary */
|
||||
};
|
||||
|
||||
int my_strlen(str)
|
||||
char *str;
|
||||
{
|
||||
int i;
|
||||
for (i=0; *str; ++i, ++str)
|
||||
;
|
||||
return i;
|
||||
}
|
||||
|
||||
int main(ac, av)
|
||||
int ac;
|
||||
char **av;
|
||||
{
|
||||
int line=0, linelen=0, mod=0, i;
|
||||
unsigned long res=1;
|
||||
char str[80];
|
||||
|
||||
scanf("%s", str); /* ignore 1st line */
|
||||
|
||||
while (scanf("%s", str) != EOF) {
|
||||
line++;
|
||||
linelen=my_strlen(str);
|
||||
for (i=0; set[i].dx >= 0; ++i) {
|
||||
if (! (line % set[i].dy )) { /* line ok for this set */
|
||||
set[i].pos += set[i].dx; /* increment set column */
|
||||
mod = set[i].pos % linelen;
|
||||
if ( str[mod] == '#')
|
||||
set[i].count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%s : ", *av);
|
||||
for (i=0; set[i].dx != -1; ++i) {
|
||||
printf("[%d]=[%d, %d, %d, %d] ", i,
|
||||
set[i].dx, set[i].dy, set[i].pos, set[i].count);
|
||||
res*=set[i].count;
|
||||
}
|
||||
printf ("res:%lu\n", res);
|
||||
exit (0);
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex1.bash: Advent2020 game, day 3/game 2.
|
||||
|
||||
CMD=${0##*/}
|
||||
|
||||
XMOVES=(1 3 5 7 1) # moves right
|
||||
YMOVES=(1 1 1 1 2) # moves down
|
||||
declare -i linelen=0 modpos=0 res=1
|
||||
declare -a xpos xcount
|
||||
|
||||
read -r line # ignore 1st line
|
||||
|
||||
while read -r line; do
|
||||
(( ypos++ ))
|
||||
(( linelen = ${#line} ))
|
||||
|
||||
for ((i=0; i<${#XMOVES[@]}; ++i)); do
|
||||
if ((ypos % YMOVES[i] == 0)); then
|
||||
(( xpos[i] += XMOVES[i] )) # move right
|
||||
(( modpos = (xpos[i] % linelen) ))
|
||||
[[ ${line:modpos:1} = \# ]] && ((xcount[i]++))
|
||||
fi
|
||||
done
|
||||
done
|
||||
echo "xpos=${xpos[*]} xcount=${xcount[*]}"
|
||||
printf "%s : " "$CMD"
|
||||
for ((i=0; i<${#XMOVES[@]}; ++i)); do
|
||||
printf "[%d]=[%d, %d, %d, %d] " $i "${XMOVES[$i]}" "${YMOVES[$i]}" \
|
||||
"${xpos[$i]}" "${xcount[$i]}"
|
||||
(( res *= xcount[i] ))
|
||||
done
|
||||
printf "res:%d\n " $res
|
||||
exit 0
|
Reference in New Issue
Block a user