init 2019

This commit is contained in:
2022-09-18 13:35:09 +02:00
parent c56f9ca769
commit 378df8cf5b
23 changed files with 3261 additions and 0 deletions

100
2019/day01/INPUT.txt Normal file
View File

@@ -0,0 +1,100 @@
123457
98952
65241
62222
144922
111868
71513
74124
140122
133046
65283
107447
144864
136738
118458
91049
71486
100320
143765
88677
62034
139946
81017
128668
126450
56551
136839
64516
91821
139909
52907
78846
102008
58518
128627
71256
133546
90986
50808
139055
88769
94491
128902
55976
103658
123605
113468
128398
61725
100388
96763
101378
139952
138298
87171
51840
64828
58250
88273
136781
120097
127291
143752
117291
100023
147239
71296
100907
127612
122424
62942
95445
74040
118994
81810
146408
98939
71359
112120
100630
139576
98998
92481
53510
76343
125428
73447
62472
91370
73506
126539
50739
73133
81906
100856
52758
142303
107605
77797
124355

77
2019/day01/Makefile Normal file
View File

@@ -0,0 +1,77 @@
# 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.html>.
#
# 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) -lm
LDLIB := -l$(LIB)
export LD_LIBRARY_PATH = $(LIBDIR)
CFLAGS += -std=gnu99
CFLAGS += -O2
CFLAGS += -g
# for gprof
#CFLAGS += -pg
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
# Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm
CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result
CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c)
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 assembly memcheck memcheck1 memcheck2 ex1 ex2
all: ex1
memcheck: memcheck1
memcheck1:
@valgrind -q -s --track-origins=yes aoc-c -p 1 < $(INPUT)
memcheck2:
@valgrind -q -s --track-origins=yes aoc-c -p 2 < $(INPUT)
compile: aoc-c
assembly: aoc-c.s
ex1: aoc-c
@$(TIME) aoc-c -p 1 < $(INPUT)
ex2: aoc-c
@$(TIME) aoc-c -p 2 < $(INPUT)
clean:
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s
.c:
@echo compiling $<
@$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
.c.s:
@echo generating $@
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@

43
2019/day01/README.org Normal file
View File

@@ -0,0 +1,43 @@
* --- Day 1: The Tyranny of the Rocket Equation ---
:PROPERTIES:
:CUSTOM_ID: day-1-the-tyranny-of-the-rocket-equation----
:END:
Santa has become stranded at the edge of the Solar System while
delivering presents to other planets! To accurately calculate his
position in space, safely align his warp drive, and return to Earth in
time to save Christmas, he needs you to bring him measurements from
/fifty stars/.
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!
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel
Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given /module/ is based on its /mass/.
Specifically, to find the fuel required for a module, take its mass,
divide by three, round down, and subtract 2.
For example:
- For a mass of =12=, divide by 3 and round down to get =4=, then
subtract 2 to get =2=.
- For a mass of =14=, dividing by 3 and rounding down still yields =4=,
so the fuel required is also =2=.
- For a mass of =1969=, the fuel required is =654=.
- For a mass of =100756=, the fuel required is =33583=.
The Fuel Counter-Upper needs to know the total fuel requirement. To find
it, individually calculate the fuel needed for the mass of each module
(your puzzle input), then add together all the fuel values.
/What is the sum of the fuel requirements/ for all of the modules on
your spacecraft?
To begin, [[file:1/input][get your puzzle input]].
Answer:

115
2019/day01/aoc-c.c Normal file
View File

@@ -0,0 +1,115 @@
/* aoc-c.c: Advent of Code 2021, 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.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 "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_create("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 = 1, 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);
} else {
res = ex2();
printf ("%s : res=%d\n", *av, res);
}
exit (0);
}