diff --git a/2022/RESULTS.txt b/2022/RESULTS.txt index 01c9bc7..595e0a1 100644 --- a/2022/RESULTS.txt +++ b/2022/RESULTS.txt @@ -183,7 +183,15 @@ aoc.bash: res=5619 time: 0:00.69 real, 0.69 user, 0.00 sys context-switch: 38+1, page-faults: 0+430 +aoc-c: res=5619 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+158 + +++++++++++++++++ part 2 aoc.bash: res=2376 time: 0:03.25 real, 3.24 user, 0.00 sys context-switch: 46+1, page-faults: 0+340 + +aoc-c: res=2376 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+131 diff --git a/2022/day10/Makefile b/2022/day10/Makefile new file mode 100644 index 0000000..e0add74 --- /dev/null +++ b/2022/day10/Makefile @@ -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 . +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + +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 diff --git a/2022/day10/README.org b/2022/day10/README.org new file mode 100644 index 0000000..093648a --- /dev/null +++ b/2022/day10/README.org @@ -0,0 +1,231 @@ +** --- Day 10: Cathode-Ray Tube --- +You avoid the ropes, plunge into the river, and swim to shore. + +The Elves yell something about meeting back up with them upriver, but +the river is too loud to tell exactly what they're saying. They finish +crossing the bridge and disappear from view. + +Situations like this must be why the Elves prioritized getting the +communication system on your handheld device working. You pull it out of +your pack, but the amount of water slowly draining from a big crack in +its screen tells you it probably won't be of much immediate use. + +/Unless/, that is, you can design a replacement for the device's video +system! It seems to be some kind of +[[https://en.wikipedia.org/wiki/Cathode-ray_tube][cathode-ray tube]] +screen and simple CPU that are both driven by a precise /clock circuit/. +The clock circuit ticks at a constant rate; each tick is called a +/cycle/. + +Start by figuring out the signal being sent by the CPU. The CPU has a +single register, =X=, which starts with the value =1=. It supports only +two instructions: + +- =addx V= takes /two cycles/ to complete. /After/ two cycles, the =X= + register is increased by the value =V=. (=V= can be negative.) +- =noop= takes /one cycle/ to complete. It has no other effect. + +The CPU uses these instructions in a program (your puzzle input) to, +somehow, tell the screen what to draw. + +Consider the following small program: + +#+begin_example +noop +addx 3 +addx -5 +#+end_example + +Execution of this program proceeds as follows: + +- At the start of the first cycle, the =noop= instruction begins + execution. During the first cycle, =X= is =1=. After the first cycle, + the =noop= instruction finishes execution, doing nothing. +- At the start of the second cycle, the =addx 3= instruction begins + execution. During the second cycle, =X= is still =1=. +- During the third cycle, =X= is still =1=. After the third cycle, the + =addx 3= instruction finishes execution, setting =X= to =4=. +- At the start of the fourth cycle, the =addx -5= instruction begins + execution. During the fourth cycle, =X= is still =4=. +- During the fifth cycle, =X= is still =4=. After the fifth cycle, the + =addx -5= instruction finishes execution, setting =X= to =-1=. + +Maybe you can learn something by looking at the value of the =X= +register throughout execution. For now, consider the /signal strength/ +(the cycle number multiplied by the value of the =X= register) /during/ +the 20th cycle and every 40 cycles after that (that is, during the 20th, +60th, 100th, 140th, 180th, and 220th cycles). + +For example, consider this larger program: + +#+begin_example +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop +#+end_example + +The interesting signal strengths can be determined as follows: + +- During the 20th cycle, register =X= has the value =21=, so the signal + strength is 20 * 21 = /420/. (The 20th cycle occurs in the middle of + the second =addx -1=, so the value of register =X= is the starting + value, =1=, plus all of the other =addx= values up to that point: 1 + + 15 - 11 + 6 - 3 + 5 - 1 - 8 + 13 + 4 = 21.) +- During the 60th cycle, register =X= has the value =19=, so the signal + strength is 60 * 19 = =1140=. +- During the 100th cycle, register =X= has the value =18=, so the signal + strength is 100 * 18 = =1800=. +- During the 140th cycle, register =X= has the value =21=, so the signal + strength is 140 * 21 = =2940=. +- During the 180th cycle, register =X= has the value =16=, so the signal + strength is 180 * 16 = =2880=. +- During the 220th cycle, register =X= has the value =18=, so the signal + strength is 220 * 18 = =3960=. + +The sum of these signal strengths is =13140=. + +Find the signal strength during the 20th, 60th, 100th, 140th, 180th, and +220th cycles. /What is the sum of these six signal strengths?/ diff --git a/2022/day10/aoc.h b/2022/day10/aoc.h new file mode 100644 index 0000000..2ef8361 --- /dev/null +++ b/2022/day10/aoc.h @@ -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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +#ifndef _AOC_H_ +#define _AOC_H_ + +extern int parseargs(int ac, char**av); + +#endif /* _AOC_H_ */ diff --git a/2022/day10/common.bash b/2022/day10/common.bash new file mode 100755 index 0000000..5af1e54 --- /dev/null +++ b/2022/day10/common.bash @@ -0,0 +1,68 @@ +#!/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 . +# +# SPDX-License-Identifier: GPL-3.0-or-later + +# shellcheck disable=2034 +export cmdname=${0##*/} +export debug=0 +export res +export LANG=C + +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" +} diff --git a/2022/day10/common.c b/2022/day10/common.c new file mode 100644 index 0000000..a3827b6 --- /dev/null +++ b/2022/day10/common.c @@ -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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#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; +} diff --git a/2022/day10/input/example.txt b/2022/day10/input/example.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/day10/input/example.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop diff --git a/2022/day10/input/example2.txt b/2022/day10/input/example2.txt new file mode 100644 index 0000000..f428b7b --- /dev/null +++ b/2022/day10/input/example2.txt @@ -0,0 +1,3 @@ +noop +addx 3 +addx -5 diff --git a/2022/day10/input/input.txt b/2022/day10/input/input.txt new file mode 100644 index 0000000..734f57f --- /dev/null +++ b/2022/day10/input/input.txt @@ -0,0 +1,137 @@ +noop +noop +addx 5 +addx 3 +addx -2 +noop +addx 5 +addx 4 +noop +addx 3 +noop +addx 2 +addx -17 +addx 18 +addx 3 +addx 1 +noop +addx 5 +noop +addx 1 +addx 2 +addx 5 +addx -40 +noop +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx -24 +addx 31 +addx 2 +addx -33 +addx -6 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx -1 +addx 6 +noop +noop +addx 1 +addx 4 +noop +noop +addx -15 +addx 20 +noop +addx -23 +addx 27 +noop +addx -35 +addx 1 +noop +noop +addx 5 +addx 11 +addx -10 +addx 4 +addx 1 +noop +addx 2 +addx 2 +noop +addx 3 +noop +addx 3 +addx 2 +noop +addx 3 +addx 2 +addx 11 +addx -4 +addx 2 +addx -38 +addx -1 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx -7 +addx 8 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx -25 +addx 26 +addx 2 +addx 8 +addx -1 +addx 2 +addx -2 +addx -37 +addx 5 +addx 3 +addx -1 +addx 5 +noop +addx 22 +addx -21 +addx 2 +addx 5 +addx 2 +addx 13 +addx -12 +addx 4 +noop +noop +addx 5 +addx 1 +noop +noop +addx 2 +noop +addx 3 +noop +noop