2020: Tentative C & Bash frameworks (common functions)
This commit is contained in:
@@ -7,8 +7,15 @@ aoc.bash: res=66719
|
|||||||
time: 0:00.03 real, 0.02 user, 0.00 sys
|
time: 0:00.03 real, 0.02 user, 0.00 sys
|
||||||
context-switch: 0+1, page-faults: 0+274
|
context-switch: 0+1, page-faults: 0+274
|
||||||
|
|
||||||
|
aoc-c : res=66719
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 0+1, page-faults: 0+89
|
||||||
|
|
||||||
+++++++++++++++++ part 2
|
+++++++++++++++++ part 2
|
||||||
aoc.bash: res=198551
|
aoc.bash: res=198551
|
||||||
time: 0:00.03 real, 0.03 user, 0.00 sys
|
time: 0:00.03 real, 0.03 user, 0.00 sys
|
||||||
context-switch: 2+1, page-faults: 0+283
|
context-switch: 2+1, page-faults: 0+283
|
||||||
|
|
||||||
|
aoc-c : res=198551
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 5+1, page-faults: 0+87
|
||||||
|
@@ -82,9 +82,9 @@ ccls: $(CCLSFILE)
|
|||||||
clean:
|
clean:
|
||||||
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json
|
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json
|
||||||
|
|
||||||
.c:
|
aoc-c: aoc-c.c common.c
|
||||||
@echo compiling $<
|
@echo compiling $<
|
||||||
@$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@
|
$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $^ $(LDLIB) -o $@
|
||||||
|
|
||||||
# generate pre-processed file (.i) and assembler (.s)
|
# generate pre-processed file (.i) and assembler (.s)
|
||||||
%.i: %.c
|
%.i: %.c
|
||||||
|
@@ -17,6 +17,8 @@
|
|||||||
#include "plist.h"
|
#include "plist.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "pool.h"
|
#include "pool.h"
|
||||||
|
#include "aoc.h"
|
||||||
|
|
||||||
|
|
||||||
PLIST_HEAD(plist);
|
PLIST_HEAD(plist);
|
||||||
|
|
||||||
@@ -45,7 +47,7 @@ static void parse(pool_t *pool)
|
|||||||
buflen = getline(&buf, &alloc, stdin);
|
buflen = getline(&buf, &alloc, stdin);
|
||||||
switch (buflen) {
|
switch (buflen) {
|
||||||
case 1:
|
case 1:
|
||||||
case -1:
|
case -1: /* EOF */
|
||||||
node = pool_get(pool);
|
node = pool_get(pool);
|
||||||
plist_node_init(node, total);
|
plist_node_init(node, total);
|
||||||
plist_add(node, &plist);
|
plist_add(node, &plist);
|
||||||
@@ -62,35 +64,11 @@ end:
|
|||||||
return;
|
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 main(int ac, char **av)
|
||||||
{
|
{
|
||||||
int opt, part = 1;
|
int part = parseargs(ac, av);
|
||||||
|
|
||||||
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));
|
pool_t *pool_tot = pool_create("total", 128, sizeof(struct plist_node));
|
||||||
|
|
||||||
parse(pool_tot);
|
parse(pool_tot);
|
||||||
|
|
||||||
printf("%s : res=%d\n", *av, calc_top_plist(part == 1? 1: 3));
|
printf("%s : res=%d\n", *av, calc_top_plist(part == 1? 1: 3));
|
||||||
|
@@ -13,41 +13,44 @@
|
|||||||
|
|
||||||
. common.bash
|
. common.bash
|
||||||
|
|
||||||
declare -a total
|
declare -a elf
|
||||||
declare max=0
|
declare -i maxwanted=3
|
||||||
declare -i res=0
|
|
||||||
|
|
||||||
parse() {
|
parse() {
|
||||||
local -i elf=0
|
local -i tot=0 i res
|
||||||
|
|
||||||
while read -r line; do
|
while true; do
|
||||||
if [[ -n $line ]]; then
|
read -r line
|
||||||
(( total[elf] += line))
|
res=$?
|
||||||
else
|
# number found: sum & continue
|
||||||
(( total[elf] > max )) && (( max = total[elf] ))
|
[[ -n $line ]] && (( tot += line)) && continue
|
||||||
((elf++))
|
# first elf: we set it and continue
|
||||||
fi
|
(( !${#elf[@]} )) && (( elf[0] = tot )) && continue
|
||||||
done
|
# find a place to (maybe) insert new high (keep array sorted)
|
||||||
}
|
for (( i = 0; i < ${#elf[@]}; ++i )); do
|
||||||
|
if (( tot > elf[i] )); then # normal insert (tot > old value)
|
||||||
part1() {
|
elf=( "${elf[@]:0:i}" "$tot" "${elf[@]:i}" )
|
||||||
res=$max
|
break
|
||||||
}
|
elif (( i == ${#elf[@]}-1 )); then # insert at end
|
||||||
|
elf+=( "$tot" )
|
||||||
part2() {
|
break
|
||||||
local -i i elf newbest
|
fi
|
||||||
|
|
||||||
for ((i=0; i<3; ++i)); do
|
|
||||||
newbest=0
|
|
||||||
for ((elf=0; elf<${#total[@]}; ++elf)); do
|
|
||||||
(( total[elf] > total[newbest] )) && newbest=$elf
|
|
||||||
done
|
done
|
||||||
(( res+=total[newbest] ))
|
# keep array size <= maxwanted
|
||||||
unset "total[$newbest]" # remove current max
|
elf=( "${elf[@]:0:maxwanted}" )
|
||||||
total=("${total[@]}") # pack array
|
(( tot = 0 ))
|
||||||
|
((res > 0)) && break # EOF
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
solve() {
|
||||||
|
local -i part="$1"
|
||||||
|
if ((part == 1)); then
|
||||||
|
(( res = elf[0] ))
|
||||||
|
else
|
||||||
|
(( res = elf[0] + elf[1] + elf[2] ))
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
printf "%s: res=%s\n" "$cmdname" "$res"
|
|
||||||
exit 0
|
exit 0
|
||||||
|
17
2022/day01/aoc.h
Normal file
17
2022/day01/aoc.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/* aoc.c: Advent of Code 2022
|
||||||
|
*
|
||||||
|
* 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>
|
||||||
|
*/
|
||||||
|
#ifndef _AOC_H_
|
||||||
|
#define _AOC_H_
|
||||||
|
|
||||||
|
extern int parseargs(int ac, char**av);
|
||||||
|
|
||||||
|
#endif /* _AOC_H_ */
|
@@ -14,17 +14,18 @@
|
|||||||
# shellcheck disable=2034
|
# shellcheck disable=2034
|
||||||
export cmdname=${0##*/}
|
export cmdname=${0##*/}
|
||||||
export debug=0
|
export debug=0
|
||||||
export part=1
|
export res
|
||||||
export res=""
|
|
||||||
|
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
set -o noglob
|
set -o noglob
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
printf "usage: %s [-d DEBUG] [-p PART]\n" "$cmdname"
|
printf "usage: %s [-d DEBUG] [-p PART]\n" "$cmdname"
|
||||||
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
checkargs() {
|
checkargs() {
|
||||||
|
local part
|
||||||
while getopts p:d: todo; do
|
while getopts p:d: todo; do
|
||||||
case "$todo" in
|
case "$todo" in
|
||||||
d)
|
d)
|
||||||
@@ -45,7 +46,6 @@ checkargs() {
|
|||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
usage
|
usage
|
||||||
exit 1
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
@@ -53,14 +53,15 @@ checkargs() {
|
|||||||
shift $((OPTIND - 1))
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
(( $# > 1 )) && usage
|
(( $# > 1 )) && usage
|
||||||
|
return "$part"
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
local -i part
|
||||||
|
|
||||||
checkargs "$@"
|
checkargs "$@"
|
||||||
parse
|
part=$?
|
||||||
if ((part == 1)); then
|
parse "$part"
|
||||||
part1
|
solve "$part"
|
||||||
else
|
printf "%s: res=%s\n" "$cmdname" "$res"
|
||||||
part2
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
49
2022/day01/common.c
Normal file
49
2022/day01/common.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/* common.c: Advent of Code 2022, common functions
|
||||||
|
*
|
||||||
|
* 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 "aoc.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
|
static int usage(char *prg)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [-d debug_level] [-p part] [-i input]\n", prg);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int parseargs(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;
|
||||||
|
case 'i':
|
||||||
|
|
||||||
|
default:
|
||||||
|
return usage(*av);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (optind < ac)
|
||||||
|
return usage(*av);
|
||||||
|
return part;
|
||||||
|
}
|
Reference in New Issue
Block a user