C 2022/Day 3 final ľeanup and comments
This commit is contained in:
111
2022/day04/Makefile
Normal file
111
2022/day04/Makefile
Normal file
@@ -0,0 +1,111 @@
|
||||
# AOC daily Makefile - GNU make only.
|
||||
#
|
||||
# Copyright (C) 2021-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>
|
||||
#
|
||||
|
||||
INPUT := input/input.txt
|
||||
SHELL := /bin/bash
|
||||
|
||||
CC := gcc
|
||||
BEAR := bear
|
||||
CCLSFILE:= compile_commands.json
|
||||
|
||||
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=gnu11
|
||||
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
|
||||
|
||||
VALGRIND := valgrind
|
||||
VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all --track-origins=yes \
|
||||
--sigill-diagnostics=yes --quiet --show-error-list=yes
|
||||
|
||||
|
||||
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 part1 part2 ccls bear org
|
||||
|
||||
all: README.org ccls part1 part2
|
||||
|
||||
memcheck: memcheck1 memcheck2
|
||||
|
||||
memcheck1: aoc-c
|
||||
@$(VALGRIND) $(VALGRINDFLAGS) aoc-c -p 1 < $(INPUT)
|
||||
|
||||
memcheck2: aoc-c
|
||||
@$(VALGRIND) $(VALGRINDFLAGS) aoc-c -p 2 < $(INPUT)
|
||||
@#@valgrind -s --track-origins=yes aoc-c -p 2 < $(INPUT)
|
||||
|
||||
compile: aoc-c
|
||||
|
||||
cpp: aoc-c.i
|
||||
|
||||
assembly: aoc-c.s
|
||||
|
||||
part1: aoc-c
|
||||
@$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1
|
||||
@$(TIME) aoc-c -p 1 < $(INPUT)
|
||||
|
||||
part2: aoc-c
|
||||
@$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1
|
||||
@$(TIME) aoc-c -p 2 < $(INPUT)
|
||||
|
||||
ccls: $(CCLSFILE)
|
||||
|
||||
clean:
|
||||
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json
|
||||
|
||||
aoc-c: aoc-c.c common.c
|
||||
@echo compiling $<
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $^ $(LDLIB) -o $@
|
||||
|
||||
# generate pre-processed file (.i) and assembler (.s)
|
||||
%.i: %.c
|
||||
@echo generating $@
|
||||
@$(CC) -E $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||
|
||||
%.s: %.c
|
||||
@echo generating $@
|
||||
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||
|
||||
# generate README.org from README.html (must cleanup !)
|
||||
org: README.org
|
||||
|
||||
%.org: %.html
|
||||
@echo generating $@. Cleanup before commit !
|
||||
@pandoc $< -o $@
|
||||
|
||||
# generate compile_commands.json
|
||||
$(CCLSFILE): aoc-c.c Makefile
|
||||
$(BEAR) -- make clean compile
|
||||
|
||||
bear: clean
|
||||
@touch .ccls-root
|
||||
@$(BEAR) -- make compile
|
||||
74
2022/day04/README.org
Normal file
74
2022/day04/README.org
Normal file
@@ -0,0 +1,74 @@
|
||||
** --- Day 4: Camp Cleanup ---
|
||||
Space needs to be cleared before the last supplies can be unloaded from
|
||||
the ships, and so several Elves have been assigned the job of cleaning
|
||||
up sections of the camp. Every section has a unique /ID number/, and
|
||||
each Elf is assigned a range of section IDs.
|
||||
|
||||
However, as some of the Elves compare their section assignments with
|
||||
each other, they've noticed that many of the assignments /overlap/. To
|
||||
try to quickly find overlaps and reduce duplicated effort, the Elves
|
||||
pair up and make a /big list of the section assignments for each pair/
|
||||
(your puzzle input).
|
||||
|
||||
For example, consider the following list of section assignment pairs:
|
||||
|
||||
#+begin_example
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
#+end_example
|
||||
|
||||
For the first few pairs, this list means:
|
||||
|
||||
- Within the first pair of Elves, the first Elf was assigned sections
|
||||
=2-4= (sections =2=, =3=, and =4=), while the second Elf was assigned
|
||||
sections =6-8= (sections =6=, =7=, =8=).
|
||||
- The Elves in the second pair were each assigned two sections.
|
||||
- The Elves in the third pair were each assigned three sections: one got
|
||||
sections =5=, =6=, and =7=, while the other also got =7=, plus =8= and
|
||||
=9=.
|
||||
|
||||
This example list uses single-digit section IDs to make it easier to
|
||||
draw; your actual list might contain larger numbers. Visually, these
|
||||
pairs of section assignments look like this:
|
||||
|
||||
#+begin_example
|
||||
.234..... 2-4
|
||||
.....678. 6-8
|
||||
|
||||
.23...... 2-3
|
||||
...45.... 4-5
|
||||
|
||||
....567.. 5-7
|
||||
......789 7-9
|
||||
|
||||
.2345678. 2-8
|
||||
..34567.. 3-7
|
||||
|
||||
.....6... 6-6
|
||||
...456... 4-6
|
||||
|
||||
.23456... 2-6
|
||||
...45678. 4-8
|
||||
#+end_example
|
||||
|
||||
Some of the pairs have noticed that one of their assignments /fully
|
||||
contains/ the other. For example, =2-8= fully contains =3-7=, and =6-6=
|
||||
is fully contained by =4-6=. In pairs where one assignment fully
|
||||
contains the other, one Elf in the pair would be exclusively cleaning
|
||||
sections their partner will already be cleaning, so these seem like the
|
||||
most in need of reconsideration. In this example, there are =2= such
|
||||
pairs.
|
||||
|
||||
/In how many assignment pairs does one range fully contain the other?/
|
||||
|
||||
To begin, [[file:4/input][get your puzzle input]].
|
||||
|
||||
Answer:
|
||||
|
||||
You can also [Shareon
|
||||
[[https://twitter.com/intent/tweet?text=%22Camp+Cleanup%22+%2D+Day+4+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F4&related=ericwastl&hashtags=AdventOfCode][Twitter]]
|
||||
[[javascript:void(0);][Mastodon]]] this puzzle.
|
||||
104
2022/day04/aoc-c.c
Normal file
104
2022/day04/aoc-c.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/* aoc-c.c: Advent of Code 2022, day 3
|
||||
*
|
||||
* 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 <string.h>
|
||||
|
||||
#include "aoc.h"
|
||||
|
||||
#define NCHARS ('Z' - 'A' + 1 + 'z' - 'a' + 1)
|
||||
|
||||
/* convert item to map position, and vice versa */
|
||||
#define CHAR2POS(c) ((c) > 'Z'? (c) - 'a': (c) - 'A' + 26)
|
||||
#define POS2CHAR(p) ((p) < 26? (p) + 'a': (p) - 26 + 'A')
|
||||
|
||||
/* set a map from char */
|
||||
#define SET_MAP(ex, c) ((ex)[CHAR2POS(c)] = 1)
|
||||
|
||||
static void map_init(char *p)
|
||||
{
|
||||
memset(p, 0, NCHARS);
|
||||
}
|
||||
|
||||
static void map_populate(char *ex, char *p, int len)
|
||||
{
|
||||
map_init(ex);
|
||||
for (; len; --len)
|
||||
SET_MAP(ex, p[len - 1]);
|
||||
}
|
||||
|
||||
/* match map with items list. If dest is NULL, return first match,
|
||||
* otherwise populate dest with matching chars.
|
||||
*/
|
||||
static int map_match(char *dest, char *map, char *items, int ilen)
|
||||
{
|
||||
if (dest)
|
||||
map_init(dest);
|
||||
|
||||
for (int i = 0; i < ilen; ++i) {
|
||||
int pos = CHAR2POS(items[i]);
|
||||
if (map[pos]) { /* already exists */
|
||||
if (dest)
|
||||
dest[pos] = 1; /* fill dest array */
|
||||
else
|
||||
return pos; /* one match only */
|
||||
}
|
||||
}
|
||||
return -1; /* unused if dest != NULL */
|
||||
}
|
||||
|
||||
static int parse(int part)
|
||||
{
|
||||
size_t alloc = 0;
|
||||
char *buf = NULL;
|
||||
ssize_t buflen;
|
||||
int line = 0, res = 0, half;
|
||||
char map[2][NCHARS];
|
||||
|
||||
while ((buflen = getline(&buf, &alloc, stdin)) > 0) {
|
||||
buf[--buflen] = 0;
|
||||
if (part == 1) {
|
||||
half = buflen / 2;
|
||||
/* populate c1 */
|
||||
map_populate(map[0], buf, half);
|
||||
//map_populate(map[1], buf + half, half);
|
||||
/* calculate part 1 */
|
||||
res += map_match(NULL, map[0], buf + half, half) + 1;
|
||||
} else {
|
||||
/* populate c2 */
|
||||
switch (++line % 3) {
|
||||
case 0:
|
||||
res += map_match(NULL, map[1], buf, buflen) + 1;
|
||||
break;
|
||||
case 1:
|
||||
map_populate(map[0], buf, buflen);
|
||||
break;
|
||||
case 2:
|
||||
map_match(map[1], map[0], buf, buflen);
|
||||
break;
|
||||
|
||||
}
|
||||
//if ((++line % 3))
|
||||
// continue;
|
||||
/* calculate part 2 */
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
printf("%s: res=%d\n", *av, parse(parseargs(ac, av)));
|
||||
exit(0);
|
||||
}
|
||||
87
2022/day04/aoc.bash
Executable file
87
2022/day04/aoc.bash
Executable file
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# aoc.bash: Advent of Code 2022, day 3
|
||||
#
|
||||
# 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>
|
||||
|
||||
. common.bash
|
||||
|
||||
export LANG=C
|
||||
declare -a input
|
||||
|
||||
# prio - get priority value for an item
|
||||
# $1: reference of variable for result
|
||||
# $2: string
|
||||
prio() {
|
||||
local -n _val="$1"
|
||||
local _char="${2:0:1}"
|
||||
|
||||
printf -v _val "%d" "'$_char"
|
||||
if [[ $_char > "Z" ]]; then # a-z
|
||||
((_val += (1 - 97) ))
|
||||
else # A-Z
|
||||
((_val += (27 - 65) ))
|
||||
fi
|
||||
}
|
||||
|
||||
# common=chars - get common characters in two strings
|
||||
# $1: reference of variable for result
|
||||
# $2, $3: The two strings
|
||||
common_chars() {
|
||||
local -n _res="$1"
|
||||
|
||||
_res="${2//[^$3]}"
|
||||
}
|
||||
|
||||
parse() {
|
||||
readarray -t input
|
||||
}
|
||||
|
||||
part1() {
|
||||
local line half1 half2 common
|
||||
local -i half i prio
|
||||
declare -ig res=0
|
||||
|
||||
for line in "${input[@]}"; do
|
||||
(( half = ${#line} / 2 ))
|
||||
half1="${line:0:half}"
|
||||
half2="${line:half}"
|
||||
common="${half1//[^${half2}]}"
|
||||
prio prio "${common}"
|
||||
(( res += prio ))
|
||||
done
|
||||
}
|
||||
|
||||
part2() {
|
||||
local one two three common
|
||||
local -i i prio
|
||||
declare -ig res=0
|
||||
|
||||
for (( i = ${#input[@]} - 1; i > 1; i -= 3)); do
|
||||
three=${input[i]}
|
||||
two=${input[i - 1]}
|
||||
one=${input[i - 2]}
|
||||
common_chars common "$three" "$two"
|
||||
common_chars common "$common" "$one"
|
||||
prio prio "${common}"
|
||||
(( res += prio ))
|
||||
done
|
||||
}
|
||||
|
||||
solve() {
|
||||
if (($1 == 1)); then
|
||||
part1
|
||||
else
|
||||
part2
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
exit 0
|
||||
17
2022/day04/aoc.h
Normal file
17
2022/day04/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_ */
|
||||
67
2022/day04/common.bash
Executable file
67
2022/day04/common.bash
Executable file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# common.bash: Advent of Code 2022, common bash 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>
|
||||
|
||||
# shellcheck disable=2034
|
||||
export cmdname=${0##*/}
|
||||
export debug=0
|
||||
export res
|
||||
|
||||
shopt -s extglob
|
||||
set -o noglob
|
||||
|
||||
usage() {
|
||||
printf "usage: %s [-d DEBUG] [-p PART]\n" "$cmdname"
|
||||
exit 1
|
||||
}
|
||||
|
||||
checkargs() {
|
||||
local part=1
|
||||
while getopts p:d: todo; do
|
||||
case "$todo" in
|
||||
d)
|
||||
if [[ "$OPTARG" =~ ^[[:digit:]+]$ ]]; then
|
||||
debug="$OPTARG"
|
||||
else
|
||||
printf "%s: illegal [%s] debug level.\n" "$CMD" "$OPTARG"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
p)
|
||||
if [[ "$OPTARG" =~ ^[12]$ ]]; then
|
||||
part="$OPTARG"
|
||||
else
|
||||
printf "%s: illegal [%s] part.\n" "$CMD" "$OPTARG"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
# Now check remaining argument (backup directory)
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
(( $# > 1 )) && usage
|
||||
return "$part"
|
||||
}
|
||||
|
||||
main() {
|
||||
local -i part
|
||||
|
||||
checkargs "$@"
|
||||
part=$?
|
||||
parse "$part"
|
||||
solve "$part"
|
||||
printf "%s: res=%s\n" "$cmdname" "$res"
|
||||
}
|
||||
49
2022/day04/common.c
Normal file
49
2022/day04/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;
|
||||
}
|
||||
6
2022/day04/input/example.txt
Normal file
6
2022/day04/input/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
1000
2022/day04/input/input.txt
Normal file
1000
2022/day04/input/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user